Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have an administator, which also is Farm Administrator in my Sharepoint environment. It is synced via UPSA and have all the properties I need do display content the right way. However - when I check the admin user in code behind it gets magically transferred to be SHAREPOINT\system instead of DOMAIN\Administrator... why is that?

Code behind:

try
{
    SPWeb currentWeb = SPContext.Current.Web;
    SPUser currentUser = currentWeb.CurrentUser;
    // currentUser is now SHAREPOINT\System

The Code behind is in a regular WebControlCode. Regular non-admin users get the correct currentUser attribute.

share|improve this question
1  
where is this code-behind? –  TZHX Nov 28 '12 at 13:36
    
In our own developed sharepoint solution - called MyDepartment.cs. It's there to add a link to "My Department" through a user property: Department - to link correct. –  Benny Skogberg Nov 28 '12 at 13:38
1  
sorry, what I mean is, when it runs on the server, what is it running as? A regular web part? A sandboxed web part? Part of a timer job? –  TZHX Nov 28 '12 at 13:39
    
I am taking a stab in the dark here but.. I presume it is because the code you are running is in SPSecurity.RunWithElevatedPrivileges? When running with this, the user becomes SHAREPOINT\System. –  Hugh Wood Nov 28 '12 at 13:40
1  
If you're not using RunWithElevatedPrivileges, does this user have a web application user policy set to "Account operates as System"? While I'm not 100% sure which takes precedence, I'd assume a user policy may supersede the user where system accounts are concerned. –  webdes03 Nov 28 '12 at 14:33
show 2 more comments

1 Answer 1

up vote 1 down vote accepted

Use something like this -

private SPUser GetCurrentUser()
{    
    if (SPContext.Current.Web.CurrentUser.LoginName.Equals("SHAREPOINT\\system"))     
        return SPContext.Current.Web.SiteUsers[HttpContext.Current.User.Identity.Name];     

    return SPContext.Current.Web.CurrentUser;
}

and then use it in your code -

SPUser currentUser = GetCurrentUser();

If you are unsure about SPContext.Current.Web you may pass the SPWeb object as paramater

Hope this helps :)

share|improve this answer
    
if I change CurrentUser.ToString() to CurrentUser.LoginName it works like a charm! Thanx for your answer! –  Benny Skogberg Nov 28 '12 at 14:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.