Friday, July 5, 2013

Add data to People picker field programatically -Sharepoint 2013

I had a requirement where in the visual webpart had a people picker control .I used the client people picker which is a new and betta control in Sharepoint 2013.This control had to be binded with the data coming from a people picker field of some list.Here is the code to achieve it




SPFieldUserValueCollection reviewers = (SPFieldUserValueCollection)Item["PeoplePickerFieldColumnDisplayName"];
List<PickerEntity> reviewersEntity = new List<PickerEntity>();
foreach (SPFieldUserValue spuserval in reviewers){

SPUser userToassign = spuserval.User;
PickerEntity entity = new PickerEntity();
PeopleEditor pe = new PeopleEditor();
entity.EntityData["AccountName"] = spuserval.User.LoginName;
entity.EntityData["SPUserID"] = spuserval.User.ID;
entity.EntityData["Email"] = spuserval.User.Email;
entity.Key = spuserval.User.LoginName;
entity.Description = spuserval.User.LoginName;
entity.DisplayText = spuserval.User.Name;
entity = pe.ValidateEntity(entity);
entity.IsResolved =true;
 reviewersEntity.Add(entity);
}
PeoplePickerControlName.AddEntities(reviewersEntity);



*"item " is a SPListItem type


in ascx file client people picker looks like

<SharePoint:ClientPeoplePicker runat="server" ID="PeoplePickerControlName" Required="true" ValidationEnabled="true"
                        InitialHelpText="text"
                        VisibleSuggestions="3"
                        Rows="1"
                        AllowMultipleEntities="true"
                        CssClass="ms-long ms-spellcheck-true" /> 

Have a look at other post about  people picker of Sharepoint 2010
People Picker -Sharepoint 2010 

2 comments:

  1. Thanks a lot for the post. Exactly same which I was looking for. Implement same with few modifications

    ReplyDelete
  2. This is awesome, thanks.

    ReplyDelete