Searched for difference between People Editor Control
SharePoint:PeopleEditor Vs
SharePoint:ClientPeoplePicker in SharePoint 2013 Server Object Model
Requirement: Need to add Users and SPGroup using server
object model through Visual Webpart.
Googled and find the below code snippet from different site
and consolidate below, May be useful to someone.
Using SharePoint:PeopleEditor
control ( Supports SharePoint Version 2010 and 2013)
ASCX File
<SharePoint:PeopleEditor ID="pplCC" runat="server" class="ms-authoringcontrols" SelectionSet="User,SPGroup" />
Code File
Setting Values to the Control from the list item
SPFieldUserValueCollection userCol = new SPFieldUserValueCollection(SPContext.Current.Web, item["Attendees"].ToString());
SetUserCollectionWithPeopleEditor(pplCC,
userCol);
//set
People editor control with the SP user collection
public void SetUserCollectionWithPeopleEditor(PeopleEditor editor,
SPFieldUserValueCollection userCollection)
{
System.Collections.ArrayList userList = new System.Collections.ArrayList();
if (null != userCollection)
{
foreach (SPFieldUserValue user in userCollection)
{
PickerEntity
tempEntity = new PickerEntity();
if (user.User != null)
{
tempEntity.Key =
user.User.LoginName;
}
else
{
tempEntity.Key =
user.LookupValue;
}
userList.Add(tempEntity);
}
editor.UpdateEntities(userList);
}
}
Add or Update Values from Control to the list
if (pplCC.Accounts.Count > 0)
{
oSPListItem["Attendees"] =
GetPeople(pplCC, web);
// GetUsersFomPeoplePicker("", web, pplCC);
}
//set
values People editor control
private SPFieldUserValueCollection GetPeople(PeopleEditor people, SPWeb web)
{
SPFieldUserValueCollection values = new SPFieldUserValueCollection();
if (people.ResolvedEntities.Count > 0)
{
for (int counter = 0; counter < people.ResolvedEntities.Count;
counter++)
{
PickerEntity user =
(PickerEntity)people.ResolvedEntities[counter];
switch ((string)user.EntityData["PrincipalType"])
{
case "User":
SPUser spUser =
web.EnsureUser(user.Key);
SPFieldUserValue
userValue = new SPFieldUserValue(web, spUser.ID, spUser.Name);
values.Add(userValue);
break;
case "SharePointGroup":
SPGroup siteGroup =
web.SiteGroups[user.EntityData["AccountName"].ToString()];
SPFieldUserValue
groupValue = new SPFieldUserValue(web, siteGroup.ID, siteGroup.Name);
values.Add(groupValue);
break;
default:
SPUser webUser =
web.EnsureUser(user.Key);
SPFieldUserValue
spuserValue = new SPFieldUserValue(web, webUser.ID, webUser.Name);
values.Add(spuserValue);
break;
}
}
}
return values;
}
Using SharePoint:
ClientPeoplePicker control ( Supports
only in SharePoint 2013)
ASCX File
<SharePoint:ClientPeoplePicker
Required="true"
ValidationEnabled="true"
ID="pplPickerSiteRequestor"
UseLocalSuggestionCache="true"
PrincipalAccountType="User,SPGroup"
runat="server"
VisibleSuggestions="3"
Rows="3"
AllowMultipleEntities="true"
CssClass="ms-long
ms-spellcheck-true user-block"
ErrorMessage="*"
/>
Code File
Setting Values to the Control from the list item
SPFieldUserValueCollection userCol = new SPFieldUserValueCollection(SPContext.Current.Web, item["Attendees"].ToString());
SetUserCollectionWithPeopleEditorCLIENT(pplPickerSiteRequestor,
userCol);
//set
People editor control along with the SP user collection to update the list
public void SetUserCollectionWithPeopleEditorCLIENT(ClientPeoplePicker editor,
SPFieldUserValueCollection userCollection)
{
System.Collections.ArrayList userList = new System.Collections.ArrayList();
if (null != userCollection)
{
foreach (SPFieldUserValue user in userCollection)
{
PeopleEditor pe = new PeopleEditor();
PickerEntity
tempEntity = new PickerEntity();
if (user.User != null)
{
tempEntity.Key =
user.User.LoginName;
tempEntity = pe.ValidateEntity(tempEntity);
}
else
{
tempEntity.Key =
user.LookupValue;
}
userList.Add(tempEntity);
editor.AddEntities(new List<PickerEntity> {
tempEntity });
}
}
}
Add or Update Values from Control to the list
//FOR
UPDATE client people editor
if (pplPickerSiteRequestor.ResolvedEntities.Count > 0)
{
oSPListItem["Attendees"] =
GetPeopleCLIENT(pplPickerSiteRequestor, web);
}
//set
People editor control along with the SP user collection to update the list
private SPFieldUserValueCollection GetPeopleCLIENT(ClientPeoplePicker people, SPWeb web)
{
SPFieldUserValueCollection values = new SPFieldUserValueCollection();
if (people.ResolvedEntities.Count > 0)
{
for (int counter = 0; counter < people.ResolvedEntities.Count;
counter++)
{
PickerEntity user =
(PickerEntity)people.ResolvedEntities[counter];
switch ((string)user.EntityData["PrincipalType"])
{
case "User":
SPUser spUser =
web.EnsureUser(user.Key);
SPFieldUserValue
userValue = new SPFieldUserValue(web, spUser.ID, spUser.Name);
values.Add(userValue);
break;
case "SharePointGroup":
SPGroup siteGroup =
web.SiteGroups[user.EntityData["AccountName"].ToString()];
SPFieldUserValue
groupValue = new SPFieldUserValue(web, siteGroup.ID, siteGroup.Name);
values.Add(groupValue);
break;
default:
SPUser webUser =
web.EnsureUser(user.Key);
SPFieldUserValue
spuserValue = new SPFieldUserValue(web, webUser.ID, webUser.Name);
values.Add(spuserValue);
break;
}
}
}
return values;
}
Many thanks to all who shared the knowledge.