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

I have a list in SharePoint 2010 that has peoplepicker that I need to save from one to many groups to it. I've figured out how to save users, but stumped on saving many groups to a peoplepicker.

Example:

User selects one (or many) checkboxes. The value of each checkbox has a group name that is a group in my SharePoint site. I need to take these groups and save them to a people picker (inside one of my lists) that is configured to allow multiple entries.

Can someone provide and example or point me to a URL that will explain this?

Below is what I do for users. Is this close for groups? If so, can someone show me what to change to make this work for groups?

...

for (int i = 0; i < userarray.Length; i++)
{
assist = ConvertLoginName(web, userarray[i]);

if (assist != null)
    parsedAssists += assist.User.ID.ToString(CultureInfo.InvariantCulture) + ";#" + assist.User.LoginName.ToString(CultureInfo.InvariantCulture) + ";#";
}


Hashtable ht = new Hashtable();
ht.Add("Assist", parsedAssists);

...

SPFile file = list.RootFolder.Files.Add(fileUpload.FileName, fileUpload.PostedFile.InputStream, ht, false);

private SPFieldUserValue ConvertLoginName(SPWeb web, string userId)
{
    SPUser requireduser = web.EnsureUser(userId);
    SPFieldUserValue uservalue = new SPFieldUserValue(web, requireduser.ID, requireduser.LoginName);
    return uservalue;
}
share|improve this question
add comment

2 Answers

By people picker, do you mean a Person or Group field (aka SPFieldUser?) These can be set to accept people, or, people and groups. Perhaps yours is only accepting people?

You can set this when creating the field in the UI and can probably do so after as well, or, use the SPFieldUser.SelectionMode property.

share|improve this answer
    
Sorry, yes it is a person or group field and it is set to allow both types and for multiple entries also. –  Mike Collins Sep 28 '12 at 13:57
add comment

Indeed you are pretty close to make it work for groups too.

Groups and users are not very different. Both SPGroup and SPUser inherit from SPPrincipal. Unless I'm wrong, you should never end up with a SPUser having the same ID as a SPGroup. It means you should be able to append groups to your parsedAssists string variable using SPGroup ID and group name instead of SPUser ID/LoginName.

I prefer using SPFieldUserValueCollection instead of building strings manually but that's not what you were asking for, that's just my two cents :-)

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.