SPWeb.CurrentUser returns SHAREPOINT\System not the real user name.

If you try to get the current user login name or name in SharePoint and if it shows SHAREPOINT\System account, that’s because the current logged user is an admin account and is probably the one used in the application pool of the site. Also, you are probably getting the current user from an elevated SPWeb inside a RunWithElevatedPrivileges code. You can use the snippet below to get the real user:


SPWeb site = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
  using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
  {
    using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))
    {
      //Don't use the ElevatedSite.CurrentUser
      string currUser = site.CurrentUser;
    }
  }
});

This will show the real login name instead of SHAREPOINT\System

Bear in mind that the code above is just a snippet and to show you the idea. Please make sure you use the SharePoint best practices coding for disposing objects when using the snippet above.

Hope that helps !

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s