0

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);
4

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);
1

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);
0

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;
0

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.

-1

You can also compare their id.

  • While this may theoretically answer the question, we prefer inclusion of the essential parts of the answer here, for example a line or two of code to show how this would be done. Thanks. [mod] – SPDoctor Mar 7 '13 at 12:21
-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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