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 am trying get the Modified By column value. And I want to check my PeoplePicker control contains the value same as the Modified By column value.

I am stuck in the problem that Modified By column's value is System Account and that user is e.g. SPFarm and I am passing that SPFarm in my PeoplePicker control and trying to compare that both values are same or not.

But it is not working. When I get the value of Modified By column it returns "SharePoint\\System" and my PeoplePicker control returs "id|spfarm".

I am using SharePoint 2013...
How can I compare both fields...?

Any suggestions will be appreciated..! :)

share|improve this question
2  
Instead of comparing the LoginNames, compare the ID's. –  JP Roelofse Feb 17 at 14:13
    
@JPRoelofse I have tried to compare ID's but the ID which I am getting are different. I have user SPFarm as system account and same user I am passing in SPUser but both the id's I am getting are different. Is there any other way? I am using SharePoint 2013. –  Rahul Gokani Feb 20 at 5:33
add comment

3 Answers

You can compare the token value of the account to determine if it is equal to the one of the system account. Note that in the following you get the SPSite object under elevated privileges and that comparing tokens only works when the actual account in SharePoint is already listed as the system account.

SPUser user = <<your code here>>
SPSite site = <<your code here>>
SPUserToken systemAccountToken = site.SystemAccount.UserToken;
bool userIsSystemAccount = user.UserToken.CompareUser(systemAccountToken);

Delving a bit deeper in the FarmCredentialManagement page in Central Administration site there is another way to get the actual user behind the SharePoint\\System account. The timer service in SharePoint runs under the Farm Account. Getting the ProcessIdentity for this service gives you direct access to the UserName of the Farm Account which you can compare with the user from your people picker.

SPTimerService timerservice = site.WebApplication.Farm.TimerService;
SPProcessIdentity pi = timerservice.ProcessIdentity;
string farmAccount = pi.UserName;
share|improve this answer
    
I tried your code but it is not working. My System user is spfarm and I am passing in SPUser is also spfarm. But both tokens I am getting is different. –  Rahul Gokani Feb 20 at 4:53
    
Is it because I am using SharePoint 2013? –  Rahul Gokani Feb 20 at 5:34
    
Hi Rahul, I edited my reply a bit. Can you check with the last part if that resolves the issue for you? You should be able to just compare the usernames like that. –  Steven Feb 24 at 8:40
    
Thank you Steven I'll try that and will replay you..! –  Rahul Gokani Feb 24 at 11:21
add comment

I'll take a wild guess here and assume you are comparing user under elevated privileges. To get "true" user, you have to get him from SPContext. Something like

int userID = SPContext.Current.Web.CurrentUser.ID;

To get SHAREPOINT/System account you can use following code

int systemID = SPContext.Current.Web.SystemAccount.ID;

Then simply compare both integers

if (userID == systemID){
    //code
}
share|improve this answer
    
CurrentUser.ID will work but I am fetching User from my list's Modified By column. So I need to compare User entered to my people picker control and list's modified by column. If modified by column contains System Account, it is not comparing both values. –  Rahul Gokani Feb 20 at 8:27
    
@RahulGokani So, if I understand correctly, problem is if the list was modified by System account, you are not sure with what you should compare it? –  StupidOne Feb 20 at 9:34
    
Yes. For now I know that spfarm is my system account. But when I pass spfarm in people picker control it leaves no clue from that i can identify that it is system account. And when I login with spfarm my list's entries have system account in there column. –  Rahul Gokani Feb 20 at 9:42
    
@RahulGokani Is this of any help? –  StupidOne Feb 20 at 12:34
    
No I can't do this. I have to do using code only. –  Rahul Gokani Feb 20 at 12:43
add comment

You can compare the user ID with the SystemAccount.ID

Exemple :

SPUser yourUser = 'your code here';
if (yourUser.ID == web.Site.SystemAccount.ID)
share|improve this answer
    
I also have tried this before. But the ID are not same. I don't know why. Is there any other way?" –  Rahul Gokani Feb 20 at 4:53
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.