none
Verifying info from a Person in a SharePoint List... RRS feed

  • Question

  • I'm fairly new to SharePoint development and I've run into an issue. I'd like my application to look at my SPUser object and verify that it is the same user as a Person object in a SharePoint List.

    Code Snippet

    SPListItem oItem = assocList.GetItemById(1);


    What property in oItem can I compare to my SPUser object? Feel free to berate me if this is a bad way to go about this.
    Thursday, February 21, 2008 3:42 PM

Answers

  • Nevermind, figured it out. Feel free to examine this code and tell me where I'm going wrong but it seems to do the trick.

    Code Snippet
    SPUser test = GetSPUser(oItem, assocList.Fields.GetField("AssociateName"));

    if (user.Sid.Equals(test.Sid))
    {
    ...
    }

    private SPUser GetSPUser(SPListItem item, SPField field)
    {
    string currentValue = item[field.Title].ToString();
    SPFieldUser userField = (SPFieldUser)field;
    SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(currentValue);

    return fieldValue.user;
    }


    Thursday, February 21, 2008 6:17 PM

All replies

  • Good question.

    I believe you are correct that you can not directly compare them.  The one in the person field is data type SPFieldUser:
    http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spfielduser.aspx

    There is always a little trickery when using it -- For example, here is how you might create an SPUser out of an SPFieldUser  (working with AssignedTo field of a list task)

    SPFieldUser assignedTo = (SPFieldUser)task.Fields[SPBuiltInFieldId.AssignedTo];

    SPFieldUserValue user = (SPFieldUserValue) assignedTo.GetFieldValue(task[SPBuiltInFieldId.AssignedTo].ToString());

    SPUser userObject = user.User;
    Thursday, February 21, 2008 4:07 PM
  • Maybe I'm not accessing the fields in the List properly because I can't get to the SPFieldUser object. Using your code example I tried something like this...

    Code Snippet

    SPFieldUser usr = (SPFieldUser)assocList.Fields[SPBuiltInFieldId.AssignedTo];


    (assocList is an SPList, which I assume, is the same as the "list task" that you mentioned.)

    That line gives me this error: Invalid field name. {guid}

    So where am I going wrong? Smile
    Thursday, February 21, 2008 5:35 PM
  • Nevermind, figured it out. Feel free to examine this code and tell me where I'm going wrong but it seems to do the trick.

    Code Snippet
    SPUser test = GetSPUser(oItem, assocList.Fields.GetField("AssociateName"));

    if (user.Sid.Equals(test.Sid))
    {
    ...
    }

    private SPUser GetSPUser(SPListItem item, SPField field)
    {
    string currentValue = item[field.Title].ToString();
    SPFieldUser userField = (SPFieldUser)field;
    SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(currentValue);

    return fieldValue.user;
    }


    Thursday, February 21, 2008 6:17 PM
  • Works for me also!

    Well done - especially as I can't find any info about SPFieldUser and SPFieldUserValue in the SDK or on MSDN!

    Cheers,

    Nigel
    Friday, April 4, 2008 12:39 PM
  • Previously I used this code to get user name from a "Person of group" field from a sharepoint list.
    string onBehalfOf = properties.AfterProperties["On_x0020_Behalf_x0020_Of"].ToString();  
    int startingIndex = onBehalfOf.IndexOf("#") + 1;  
    int endingIndex = onBehalfOf.Length - 4;  
     
    string onBehalfName = onBehalfOf.Substring(startingIndex, endingIndex); 
    It worked fine in my Virtual PC, but when I uploaded into the server, its not working. The string "onBehalfOf" is returning only number. Previously it was getting value like this: "11;#Jubair Bin Kibria", but in the server, its returning: "11". so program crashes when it goes to do the substring.


     I used this code according to your suggestion:
    string currentValue= properties.AfterProperties["On_x0020_Behalf_x0020_Of"].ToString();  
    SPFieldUser userField= (SPFieldUser)properties.OpenWeb().Lists["Leave Letter On Behalf"].Fields.GetField("On_x0020_Behalf_x0020_Of");  
    SPFieldUserValue fieldValue= (SPFieldUserValue)userField.GetFieldValue(currentValue);  
    String onBehalfName = fieldValue.LookupValue;  
     
    but same thing is happening. in this case, "onBehalfName" is getting an empty ("") string.

    Plz help.
    Wednesday, July 23, 2008 5:14 AM
  •  Hey, finally it worked. I did it in the following way:

    string currentValue= properties.AfterProperties["On_x0020_Behalf_x0020_Of"].ToString();  
    SPFieldUser userField= (SPFieldUser)properties.OpenWeb().Lists["Leave Letter On Behalf"].Fields.GetField("On_x0020_Behalf_x0020_Of");  
    SPFieldUserValue fieldValue= (SPFieldUserValue)userField.GetFieldValue(currentValue);  
    string onBehalfName = fieldValue.User.Name;  
     
    Wednesday, July 23, 2008 8:30 AM
  • Good Morning,
    I need something similar, get the user mail from "assigned to" field of a task list.
    Somebody has this code converted to Visual Basic code?
    I can't find some example in MSDN, Technet...

    SPUser test = GetSPUser(oItem, assocList.Fields.GetField("AssociateName"));

    if (user.Sid.Equals(test.Sid))
    {
    ...}

    private SPUser GetSPUser(SPListItem item, SPField field)
    {
    string currentValue = item[field.Title].ToString();
    SPFieldUser userField = (SPFieldUser)field;
    SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(currentValue);
    return fieldValue.user;
    }

    Thanks very much,
    Pablo
    Monday, October 27, 2008 12:03 PM