Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

So I created a custom list with a person field. The person field has multiple names of people. What I want to do is get an Item' [person field] data and put it into a SPUserCollection, I tried to do something like this:

SPUserCollection users = (properties.Web.Lists[listName].GetItemById(properties.ListItemId)[people]); 
foreach(spUser in users)
{
my awesome code;
}

anyone know how to solve this, thanks!

share|improve this question
    
I don't know what properties is, but if it is SPItemEventProperties then I would like to note that you can do properties.ListItem. And now that Egor pointed out you get SPFieldUserValueCollection, give you have a SPFieldUser field I will just leave this as a comment :) –  eirikb Jan 15 '13 at 6:53
add comment

2 Answers

up vote 1 down vote accepted
SPFieldUserValueCollection userVals = (SPFieldUserValueCollection)item["UserName"];
foreach (SPFieldUserValue userVal in userVals)
{
// your code
  SPUser user = userVal.User;
}

Hope it helps..

share|improve this answer
add comment

If your person field is of type User or group with multiple values allowed, then its value in code is represented by SPFieldUserValueCollection class.

Below is my code example. This is an extension method, which takes SPListItem and Guid of field to convert it to array of SPUser. You can chage Guid to the name of the field if you need to:

    public static SPUser[] GetUserCollectionField(this SPListItem Item, Guid FieldId)
    {
        SPFieldUserValueCollection UserValue = Item[FieldId] as SPFieldUserValueCollection;
        if (UserValue == null)
            return null;
        return UserValue.Select(u => u.User).ToArray();
    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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