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:
stupid post... not helpful this thing only gives system account... not the current user...
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.
its late but thank you, so helpful...
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"
thx :)
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()
give u a like...in chinese is call 以德报怨..:)
Thank you all for keeping this post alive.. :)
helpful
Post a Comment