SharePoint 2013 get current user using JavaScript

126,925

Solution 1

Here is the code that worked for me:

<script src="/SiteAssets/jquery.SPServices-2013.02a.js" type="text/javascript"></script>
<script src="/SiteAssets/jquery.js" type="text/javascript"></script>

<script type="text/javascript">
  var userid= _spPageContextInfo.userId;
  var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userid + ")";
  var requestHeaders = { "accept" : "application/json;odata=verbose" };
  $.ajax({
    url : requestUri,
    contentType : "application/json;odata=verbose",
    headers : requestHeaders,
    success : onSuccess,
    error : onError
  });

  function onSuccess(data, request){
    var loginName = data.d.Title;
    alert(loginName);
  }

  function onError(error) {
    alert("error");
  }
</script>

Solution 2

I found a much easier way, it doesn't even use SP.UserProfiles.js. I don't know if it applies to each one's particular case, but definitely worth sharing.

//assume we have a client context called context.
var web = context.get_web();
var user = web.get_currentUser(); //must load this to access info.
context.load(user);
context.executeQueryAsync(function(){
    alert("User is: " + user.get_title()); //there is also id, email, so this is pretty useful.
}, function(){alert(":(");});

Anyways, thanks to your answers, I got to mingle a bit with UserProfiles, even though it is not really necessary for my case.

Solution 3

If you are in a SharePoint Page just use:

_spPageContextInfo.userId;

Solution 4

How about this:

$.getJSON(_spPageContextInfo.webServerRelativeUrl + "/_api/web/currentuser")
.done(function(data){ 
    console.log(data.Title);
})
.fail(function() { console.log("Failed")});

Solution 5

To get current user info:

jQuery.ajax({
    url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/currentuser",
    type: "GET",
    headers: { "Accept": "application/json;odata=verbose" }
}).done(function( data ){
    console.log( data );
    console.log( data.d.Title );
}).fail(function(){
    console.log( failed );
});
Share:
126,925
Kate
Author by

Kate

Project Systems Analyst / Programmer

Updated on July 23, 2020

Comments

  • Kate
    Kate almost 4 years

    How to get current user name using JavaScript in Script Editor web part?

  • GoldBishop
    GoldBishop about 10 years
    very useful for acquiring the Web User information.
  • GoldBishop
    GoldBishop about 10 years
    Maybe its just me, but i could not get this to work with ClientWebApp's. Started with a clean slate, and reverifying. Which deployment patterns would this design be best suited for? In regards to Auto, SharePoint, or Provider hosted WebApps.
  • Dorrene Brown
    Dorrene Brown about 10 years
    This code was written for a script editor web part -- are you trying to implement the same thing in an app for SharePoint? If so, the code would be a little different as you wouldn't have the same context that a script editor web part has.
  • GoldBishop
    GoldBishop about 10 years
    Actually, i was able to get the User from a WebApp, just the same as the Script Editor. The Context, should always be the same, just depends which door you open to get it. Think of the context just like the EF context framework. Having other issues, related to WebApp and cross-domain. Back burnered that development and implemented a WebPart, to get what i need done until we can resolve the x-domain.
  • mohsenof
    mohsenof over 9 years
    If you use SP2010 like me, it won't work for you because _spPageContextInfo.webAbsoluteUrl doesn't work on SP2010. check here
  • Alex
    Alex over 9 years
    This worked for me too, thanks ! I couldn't use the ClientContext object, as it does not triggered any errors, but the JS process was blocked to creation of the ClientContext.
  • CidaoPapito
    CidaoPapito about 9 years
    i'm getting this JS error. Uncaught ReferenceError: L_Menu_BaseUrl is not defined
  • Peter
    Peter about 8 years
    The request was to get the username. This just returns the user-id.
  • Alexei - check Codidact
    Alexei - check Codidact about 7 years
    Consider adding a little explanation along with the code.
  • jdgregson
    jdgregson almost 7 years
    If you're using a script editor webpart, you can just use _spPageContextInfo.userDisplayName or _spPageContextInfo.userDisplayName to get the username or email address.
  • jdgregson
    jdgregson almost 7 years
    If you're using a script editor webpart, you can just use _spPageContextInfo.userDisplayName or _spPageContextInfo.userDisplayName to get the username or email address.
  • Martin Braun
    Martin Braun over 6 years
    This is the right approach to get even custom properties that are added to the user profile later. Use personProperties.get_userProfileProperties() to get a full object with all properties, including the custom ones that cannot be gotten by using a getter function on it.
  • aykut aydoğan
    aykut aydoğan almost 6 years
    how to get context ?
  • Lzh
    Lzh almost 6 years
    SP.ClientContext.get_current();
  • Jeremy Thompson
    Jeremy Thompson over 4 years
    Some of the properties dont work depending on your version of SPS: see webcache.googleusercontent.com/…
  • Seyed Reza Dadrezaei
    Seyed Reza Dadrezaei over 4 years
    this code need admin permossion. i want to show login name to every client.