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 );
});
Comments
-
Kate almost 4 years
How to get current user name using JavaScript in Script Editor web part?
-
GoldBishop about 10 yearsvery useful for acquiring the Web User information.
-
GoldBishop about 10 yearsMaybe 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 about 10 yearsThis 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 about 10 yearsActually, 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 over 9 yearsIf you use SP2010 like me, it won't work for you because
_spPageContextInfo.webAbsoluteUrl
doesn't work on SP2010. check here -
Alex over 9 yearsThis 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 about 9 yearsi'm getting this JS error. Uncaught ReferenceError: L_Menu_BaseUrl is not defined
-
Peter about 8 yearsThe request was to get the username. This just returns the user-id.
-
Alexei - check Codidact about 7 yearsConsider adding a little explanation along with the code.
-
jdgregson almost 7 yearsIf you're using a script editor webpart, you can just use
_spPageContextInfo.userDisplayName
or_spPageContextInfo.userDisplayName
to get the username or email address. -
jdgregson almost 7 yearsIf 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 over 6 yearsThis 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 almost 6 yearshow to get context ?
-
Lzh almost 6 yearsSP.ClientContext.get_current();
-
Jeremy Thompson over 4 yearsSome of the properties dont work depending on your version of SPS: see webcache.googleusercontent.com/…
-
Seyed Reza Dadrezaei over 4 yearsthis code need admin permossion. i want to show login name to every client.