none
How to get SPUser from Person/Group Column programmatically

    Question

  • Hello all,

    How do I get an SPUser Object from person/group column ?

    I am querying a person/group column and a look up column (This lookup column (Assistant) is hooked to another list's (Junior) person/group column (Assistant) as well)

    So I get;

    Dim user1 as string = listItem("Manager").ToString()     - gets 12;#john smith
    Dim user2 as String = listItem("Assistant").ToString()   -  gets 22;#paul baker

    Weird Behaviour -

     Dim userValue As String = oListItem("Manager").ToString
     Dim index As Integer = userValue.IndexOf(";")
     Dim id As Integer = Int32.Parse(userValue.Substring(0, index))
     Dim user As SPUser = SPContext.Current.Web.SiteUsers.GetByID(id)

    WORKS FINE FOR "Manager" PERSON/GROUP COLUMN BUT NOT FOR "Assistant" LOOKUP COLUMN WHICH RETURNS THE SAME THING ?

    Now, how do I get a SPUser object from lookup column ?

    Please help and suggest further ...

    • Edited by .Net Frenzy Wednesday, May 12, 2010 10:37 AM
    Wednesday, May 12, 2010 10:16 AM

Answers

  • Yes,  what you have asked is a very good question- what if there are 2 users with the same name.

    Answer is you have to identify what is there unique for all users in the site.  You have to find some ways to get the unique identity of the user.

    So you have to make some change to the above code.

    //Email address is a unique field.

    SPUserCollection users = web.Users;
    SPUser user = users.GetByEmail(email); 

     

     

     


    Regards, Aj (http://www.aj-sharepoint.blogspot.com/) MCTS
    • Marked as answer by .Net Frenzy Thursday, May 13, 2010 10:32 AM
    Thursday, May 13, 2010 7:14 AM
  • Hi .Net Frenzy,

    You can use the code inclused in my reply in this similar thread:
    get user details from peoples and gruops
    http://social.msdn.microsoft.com/Forums/en-SG/sharepointdevelopment/thread/c3786ae8-f2d6-42dc-bf9a-a7895b05ae39

    Be aware that you can specify the login name of the user to the string based indexer of the SPUserCollection (http://msdn.microsoft.com/en-us/library/ms414398.aspx).

    See also this reflected code:
     
    Declaring Type: Microsoft.SharePoint.SPUserCollection
    Assembly: Microsoft.SharePoint, Version=12.0.0.0

    public SPUser get_Item(string loginName)
    {
        SPUser byLoginNoThrow = this.GetByLoginNoThrow(loginName);
        if (byLoginNoThrow == null)
        {
            throw new SPException(SPResource.GetString("CannotFindUser", new object[0]));
        }
        return byLoginNoThrow;
    }

    The login name must be unique.

    Peter

    • Marked as answer by .Net Frenzy Thursday, May 13, 2010 10:32 AM
    Thursday, May 13, 2010 7:46 AM

All replies

  • Hi,

    Do this:

    SPFieldUserValueCollection AssignedTo = (SPFieldUserValueCollection)listItem["AssignedTo"];

    foreach (SPFieldUserValue fuv in AssignedTo)
    {
        SpUser usr = fuv.User;
    }

    Regards,


    Manvir
    Wednesday, May 12, 2010 10:34 AM
  • Hi Manvir,

    Your solution throws an error - but works for 'Manager' not 'Assistant'

    I think it is the LOOKUP column that is causing the problem. When I debug the code in my post with [ oListItem("Assistant").ToString ] - in the debugging window if I type [ ?user ] and enter it says - Nothing

    So it is something with the lookup column not working like a normal person/group column. Somehow, it is loosing its signature.

    Now, when you set up a lookup column of person/group type it does not let you pick up the person/group column as well. WHY . I don't know.

    Possibly the worst day in my programming and sharepoint development career... still looking for a workaround.

    Waiting for more input - calling all MVP's and experts

    Wednesday, May 12, 2010 11:29 AM
  • Hey,

    They are various methods which you have to try out to find the solution and don't be panic :)

    I think the code shd be something like this.

           SPField pplField =  spListItem.Fields["fieldName"]

        string username = listItem("User").ToString()
        SPFieldUser userField = (SPFieldUser)pplField; 
        SPFieldUserValue fieldValue = (SPFieldUserValue) field.GetFieldValue(username);

     http://www.sharepointsteve.com/2009/09/getting-the-spuser-object-out-of-a-person-or-group-list-field/


    Regards, Aj (http://www.aj-sharepoint.blogspot.com/) MCTS
    Wednesday, May 12, 2010 11:43 AM
  • Please verify the update.

     http://www.sharepointsteve.com/2009/09/getting-the-spuser-object-out-of-a-person-or-group-list-field/


    Regards, Aj (http://www.aj-sharepoint.blogspot.com/) MCTS
    Wednesday, May 12, 2010 11:54 AM
  • Ok, Ajith from your code I did the following -

    Dim pplField As SPField = oListItem.Fields("Assistant")
    Dim username As String = oListItem("Assistant").ToString()
    Dim field As SPFieldLookup = DirectCast(pplField, SPFieldLookup)
    Dim fieldValue As SPFieldLookupValue = DirectCast(field.GetFieldValue(username), SPFieldLookupValue)

    Now, when I do fieldValue.toString - it outputs - [ 3;#john smith ] - this is fine but how do i now convert this into SPUser object ?

    THe problem is I am not able to get the SPUser from a lookup column (this lookup is also hooked to Person/group column on other list) 

    If I do -

    current.web.siteUsers.getbyId(3) - exception - user not found
    current.web.users.getbyId(3) - exception - user not found
    dim fuv as SPFieldUserValue = new SPFieldUserValue (current.web,3,John smith)
    dim iUser as SPUser = fuv.User - still exception

    All of the above don't work with lookup column whose signature is of person/group type but works for person/group column

    How to query the lookup column attached to person/group column and get the user I am looking for ?

    (WHY & HOW )

    Wednesday, May 12, 2010 12:03 PM
  • I think you have to split wat its retured as outputs - [ 3;#john smith ]  after adding it to string array

     so code will be something like this :

          StringBuilder str = null;

          str = fieldValue.toString();

          strring str1 = str.split('#');

          string username = str[1].Tostring();

            SPUser user = mySite.SiteUsers["username "]; //john smith


    Regards, Aj (http://www.aj-sharepoint.blogspot.com/) MCTS
    Wednesday, May 12, 2010 12:45 PM
  • Hi Ajith, Many thanks for your input and I do appreciate it.

    Ok, Now imagine we have got two John Smith on FBA enabled sharepoint site. members:john1 - john smith and members:john2-john smith

    Like you said - SPUser user = mySite.SiteUsers["username"];

    Which user will take precedence since we don't know who the actual user is - members:john1 or members:john2

    The ID attached to a user on person/group column is actually the unique signature we need. My problem is

    1. how to get a person/group column from look up column

    2. how to then get a SPUser object using ID

    However, i am looking into your previous post reply SPFieldLookUp and I think this the right way to get the lookup field. I will keep you updated. Many thanks.

    Wednesday, May 12, 2010 3:17 PM
  • Yes,  what you have asked is a very good question- what if there are 2 users with the same name.

    Answer is you have to identify what is there unique for all users in the site.  You have to find some ways to get the unique identity of the user.

    So you have to make some change to the above code.

    //Email address is a unique field.

    SPUserCollection users = web.Users;
    SPUser user = users.GetByEmail(email); 

     

     

     


    Regards, Aj (http://www.aj-sharepoint.blogspot.com/) MCTS
    • Marked as answer by .Net Frenzy Thursday, May 13, 2010 10:32 AM
    Thursday, May 13, 2010 7:14 AM
  • Hi .Net Frenzy,

    You can use the code inclused in my reply in this similar thread:
    get user details from peoples and gruops
    http://social.msdn.microsoft.com/Forums/en-SG/sharepointdevelopment/thread/c3786ae8-f2d6-42dc-bf9a-a7895b05ae39

    Be aware that you can specify the login name of the user to the string based indexer of the SPUserCollection (http://msdn.microsoft.com/en-us/library/ms414398.aspx).

    See also this reflected code:
     
    Declaring Type: Microsoft.SharePoint.SPUserCollection
    Assembly: Microsoft.SharePoint, Version=12.0.0.0

    public SPUser get_Item(string loginName)
    {
        SPUser byLoginNoThrow = this.GetByLoginNoThrow(loginName);
        if (byLoginNoThrow == null)
        {
            throw new SPException(SPResource.GetString("CannotFindUser", new object[0]));
        }
        return byLoginNoThrow;
    }

    The login name must be unique.

    Peter

    • Marked as answer by .Net Frenzy Thursday, May 13, 2010 10:32 AM
    Thursday, May 13, 2010 7:46 AM
  • Thanks, for all your help.

    Indeed this makes sense.

    Thursday, May 13, 2010 10:31 AM
  • Peter, I have always said ' You ROCK' .

    THanks for the input and helpful pointers.

    Thursday, May 13, 2010 10:32 AM
  • Hi .Net Frenzy,

    Thanks for your feedback!

    Peter

    Thursday, May 13, 2010 11:06 AM