Question

I am using sp2010. I get the current user from the context. And I would like to check if it is equal to the creator of an list item. But the code says it is not equal. Why? Is there some other solution to equal 2 spusers?

// check if current user is creator
SPUser creator = new SPFieldUserValue(web, this.ListItem["Author"].ToString()).User;
SPUser currentUser = SPContext.Current.Web.CurrentUser;
CurrentUserIsExpertiseCreator = creator.Equals(currentUser);
Was it helpful?

Solution 2

This is the answer in code:

 SPUser creator = new SPFieldUserValue(web, this.ListItem["Author"].ToString()).User;
                                SPUser currentUser = SPContext.Current.Web.CurrentUser;
                                CurrentUserIsExpertiseCreator = creator.Sid.Equals(currentUser.Sid);

OTHER TIPS

try this:

            SPUser creator = new SPFieldUserValue(web, this.ListItem["Author"].ToString()).User;
            SPUser currentUser = SPContext.Current.Web.CurrentUser;
            CurrentUserIsExpertiseCreator = creator.Name.Equals(currentUser.Name);

instead of comparing the spuser object compare against the name (string)

or

           CurrentUserIsExpertiseCreator = creator.Name.CompareTo(currentUser.Name);

I would use recommend and the SPUser property ID or LoginName as Name can be the same for different site users, which I suppose is not the expected behaviour.

// check if current user is creator
SPUser creator = new SPFieldUserValue(web, this.ListItem["Author"].ToString()).User;
SPUser currentUser = SPContext.Current.Web.CurrentUser;

CurrentUserIsExpertiseCreator = creator.ID == currentUser.ID;
// or
CurrentUserIsExpertiseCreator = creator.LoginName == currentUser.LoginName;

I cannot use solution by ola, because in some cases I have empty SIDs. Answer by Ali Jafer may be good but no so safe because of namesake. I found another solution for me.

    SPUser u1, u2;
    bool equal = u1.UserId.NameId.ToLower() == u2.UserId.NameId.ToLower();

I had to use ToLower because in my project these strings can differ in case.

You can also compare their id.

I have fix it to compare the SID id from both spuser objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top