Using the people picker how to get the typed/selected users to save them somewhere?

I already know how to create a people picker in an application page, however after the user types 10 users or groups, I need to know the ids or some important info so I can save it in an internal list or something.

I should be able to know if he typed a user or a group also

//First you should validate users (myppl is the people editor control id)

 // Get list (we will create new list item and add these people to one field of that list item)
 SPList myList = SPContext.Current.Web.GetList("site/myListURL");

 // Create list item
 SPContentType itemtype = myList.ContentTypes["MyContentType"];
 SPListItem newItem = myList.Items.Add();
 newItem["ContentTypeId"] = itemtype.Id;

 // This is where we shall gather all principals, to be added later to the list item 
 SPFieldUserValueCollection userPrincipals = new SPFieldUserValueCollection();

 foreach (PickerEntity person in myppl.ResolvedEntities)
  // Add everyone to the site with full permissions 
  SPPrincipal pr = mySPWeb.EnsureUser(person.Key) as SPPrincipal;
  AddPrincipalRoleToSite(pr, SPRoleType.Administrator, myList.ParentWeb);

  // Populate userPrincipals so we can use it below
  SPUser usr = newItem.Web.EnsureUser(person.Key) as SPUser;
  userPrincipals.Add(new SPFieldUserValue(newItem.Web, usr.ID, usr.Name));

 // Finally set the value of the list item field that will contain all the users
 newItem["PersonColumnInternalName"] = ownerPrincipals;

 // All done, save item

public static void AddPrincipalRoleToSite(SPPrincipal entity, SPRoleType role, SPWeb web)
    SPRoleAssignment roleAssignment = new SPRoleAssignment(entity);
    SPRoleDefinition byType = web.RoleDefinitions.GetByType(role);

You might be able to optimize the code within for loop (cast SPPrincipal into SPUser or something), I combined the code from few different projects of mine so it might not be 100% optimal.

| improve this answer | |

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.