0

I was using GetDataTable() function to get the list data from a list "Orders". Orders table has a multiple enabled people picker. When the data table is selected, the people picker value(PersonOne; PersonTwo;) is retrieved as "personOne;#34;#personTwo", where 34 is the principal id of PersonTwo. I can't figure out the format of the values retrieved. I have attached the code below.

spWeb.Lists["Orders"].Items.GetDataTable()
  • The problem is that principal id for personOne is missing? – Cecilia Jan 9 '17 at 11:49
  • yes. It is returned as a single string "personOne;#34;#personTwo". If three people values are there, then it is retrieved as "personOne;#34;#personTwo;#26;#personThree" where 34 and 26 are the principal IDs of personTwo and personThree respectively. – Naveen Prabu Selvaraj Jan 9 '17 at 12:02
  • found this to be useful. but still keen to know the reason for this behavior. – Naveen Prabu Selvaraj Jan 9 '17 at 12:50
  • But, do you need to extract users id or users account name?. Please, share the entire code. – Cecilia Jan 9 '17 at 13:43
  • I wanted to extract the usernames alone. I did it as a proof of concept and checked the properties of the rows returned in that DataTable object returned. In that, the people picker value was in the format mentioned above. You'll get some idea if you look into this link – Naveen Prabu Selvaraj Jan 10 '17 at 12:23
0

You can retrieve the user field value using the SPFieldUserValueCollection class. This is an example of what you can do:

using (SPSite spSite = new SPSite("siteUrl"))
{
    using (SPWeb spWeb = spSite.OpenWeb())
    {
        //spWeb.Lists["Orders"].Items.GetDataTable();

        var listName = "Orders";
        var userFieldName = "<YourMultivalueUserFieldName>";

        var items = spWeb.Lists[listName].Items;

        // Loop on list items
        foreach (SPListItem item in items)
        {
            // Get the user field
            var field = item.ParentList.Fields.GetFieldByInternalName(userFieldName) as SPFieldUser;

            // Check if the field allows multiple values
            if (field.AllowMultipleValues)
            {
                SPFieldUserValueCollection userValueCollection = new SPFieldUserValueCollection(spWeb, item[userFieldName].ToString());

                // Loop throw user values
                foreach (SPFieldUserValue userValue in userValueCollection)
                {
                    // Show user field value properties
                    Console.WriteLine(userValue.LookupId);
                    Console.WriteLine(userValue.LookupValue);
                    // Show user properties
                    Console.WriteLine(userValue.User.Email);
                    Console.WriteLine(userValue.User.LoginName);
                }
            }
            else
            {
                SPFieldUserValue userValue = new SPFieldUserValue(spWeb, item[userFieldName].ToString());

                // Show user field value properties
                Console.WriteLine(userValue.LookupId);
                Console.WriteLine(userValue.LookupValue);
                // Show user properties
                Console.WriteLine(userValue.User.Email);
                Console.WriteLine(userValue.User.LoginName);
            }
        }
    }
}

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.