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

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

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted
//First you should validate users (myppl is the people editor control id)
myppl.Validate();

if(myppl.IsValid)
{  
 // 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
 newItem.Update(); 
}

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

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.

share|improve this answer
add comment

How do you create a People Picker in a custom page using Client Object Model? I need the ability to have users select people and put them into groups on custom pages I build. The idea is to hide that they are using a sharepoint server behind the scenes...

share|improve this answer
    
Welcome Exclusive to SharePoint StackExchange, you're question has been posted as an answer. StackExchange isn't a forum, it is a Quqestions and answer site, please refer to the full FAQ[1] on how to use SharePoint StackExchange. ENJOY [1]: sharepoint.stackexchange.com/faq –  Hugh Wood Nov 29 '12 at 9:57
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.