add

Wednesday, April 15, 2009

How to get current login SPUSer in sharepoint

Many times we require to get the logged in user in sharepoint.
We can easily get the SPUser object of the current logged in user:

SPUser user=SPContext.Current.Web.CurrentUser;

This will give the current logged in user. Now you can get user's email, groupName, Name from various properties like:
user.LoginName, user.Email etc

Cheers!!

9 comments:

Anonymous said...

stupid post... not helpful this thing only gives system account... not the current user...

Anonymous said...

Ummm... no, you're stupid. You're code probably is running in an elevated context which would return you're app pool account (aka System).

SPSecurity.CodeToRunElevated elevatedSubmit = new SPSecurity.CodeToRunElevated(delegate
{

SPUser user = SPContext.Current.Web.CurrentUser; });
SPSecurity.RunWithElevatedPrivileges(elevatedSubmit);

The above code will always return the App Pool account because it's impersonating.

Honestly don't know why I'm helping you because you're rude as hell.

Anonymous said...

its late but thank you, so helpful...

Anonymous said...

Do you know how to do this in client object model ?

I can't return the current user of an sharepoint 2013 autohosted app .
the code always returns the administrator when i return the current user.

Here is the code i'm using:

ClientContext context = new ClientContext("full url");
context.Load(context.Web.CurrentUser);
context.ExecuteQuery();
User user = context.Web.CurrentUser; //always returns "url/Administrator"

Anonymous said...

thx :)

Will said...

Thanks for sharing! This is SharePoint, after all! This is what I was looking for to get the SPUser object.

And, for the Anonymous poster from April 2, 2013, you should use the current context to get the currently logged in user.

If you just need to get the User identity object:
System.Web.HttpContext.Current.User.Identity

You can also get the windows process identity like so:
WindowsIdentity.GetCurrent()



Anonymous said...

give u a like...in chinese is call 以德报怨..:)

Varun Sharma said...

Thank you all for keeping this post alive.. :)

Anonymous said...

helpful