Start using SharePoint 2013 I noticed that the way for selecting people or groups is changed.
The new way is simple – just ‘Enter name or email address’ without any icons for ‘Check Names’ or ‘Browse’. I guess that the PeoplePicker is changed but NO. PeoplePicker sitll has the same functionality as before.
There is a new control called ClientPeoplePicker.
How to use it:
1. Add this references
<%@ Register TagPrefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
2. Add the following control declaration
<wssawc:ClientPeoplePicker
Required="true"
ValidationEnabled="true"
ID="peoplePicker"
runat="server"
InitialHelpText="<%$Resources:wss,aclinv_PickerIntialHelperText%>"
VisibleSuggestions="3"
Rows="1"
AllowMultipleEntities="false"
CssClass="ms-long ms-spellcheck-true" />
3. Form server side:
a. Set SPUser to the control
b. Get selected userSPUser assignToUser = GetYourSPUser();if (assignToUser != null){PeopleEditor pe = new PeopleEditor();PickerEntity entity = new PickerEntity();entity.Key = assignToUser.LoginName;entity = pe.ValidateEntity(entity);entity.IsResolved = true;peoplePicker.AddEntities(new List<PickerEntity> { entity });}
SPUser user = null;ArrayList resolvedEntities = peoplePicker.ResolvedEntities;foreach (PickerEntity entity in resolvedEntities){string loginName = entity.Key;user = site.RootWeb.EnsureUser(loginName);break;// the picker in my sample is in single mode!}
I developed this code reviewing the mngsiteadmin.aspx - the page for editing Site Collection Administrator. As a rule - always choose the simplest pages from the SharePoint default functionality for reverse enginiring.
Great Article!
ReplyDeleteA quick question..
Do you know how to validate( to make this control a required field) this control on click of a button?
Hi,
DeleteTo make the control required you should:
1. Add Required="true" in the markup
2. Validate onj the server in the following way:
protected void btnSave_Click(Object sender, EventArgs e)
{
peoplePicker.Validate();
if (peoplePicker.IsValid)
{
// the control has a value
}
}
Hi Dimcho,
ReplyDeleteHow can you disable the people picker, Enabled=false does not do the trick :)
Hi,
DeleteIt seems the only way to do it is using javascript.
Server side I was not able to make it.
Hi...
ReplyDeleteI followed your steps.. Once I performed the.. 'Add the following control declaration'.
I started getting the following errors..
The name 'InitializeControl' does not exist in the current context.
The name 'peoplePicker' does not exist in the current context.
Im trying to add the control into a visual webpart.
Is it possible to write a custom FieldType with ParentType User that shows a ClientPeoplePicker instead of the old PeopleEditor Control? How does SharePoint wrap the Controls in each other?
ReplyDeleteHow can i clear resolved entities
ReplyDeleteHi Shujaath Khan,
DeleteI don't find a way to clear the Entities collection on post back.
A workaround I can suggest is: instead of trying to clear the entities if they don't match your business rules, just mark them as not valid and leave the user to delete them:
protected void Button1_Click(object sender, EventArgs e)
{
bool processEvent = true;
peoplePicker.Validate();
if (peoplePicker.IsValid)
{
SPUser user = null;
ArrayList resolvedEntities = peoplePicker.ResolvedEntities;
foreach (PickerEntity entity in resolvedEntities)
{
string loginName = entity.Key;
user = SPContext.Current.Site.RootWeb.EnsureUser(loginName);
// your logic here ....
//if the user is not correct accordint to your business logic
entity.IsResolved = false;
processEvent = false;
}
}
if (processEvent)
{
//Button1 logic ....
}
}
Another way is to inherit the ClientPeoplePicker and overwrite the Validate() method by adding similar logic
Hi,
ReplyDeleteThanks for coming up with this nice article.
We have also written a similar code to get users from saved DB and during page load set them to client people picker control
However, sometime we are seeing that the ID is not getting resolved as expected and the entire SAML token with user ID gets displayed on the people picker control
the sample code which adds users to List
PickerEntity entity = new PickerEntity();
PeopleEditor pe = new PeopleEditor();
entity.EntityData["AccountName"] = userProfile.AccountName;//oSPUser.LoginName;
entity.EntityData["SPUserID"] = userProfile.ID;//oSPUser.ID;
entity.EntityData["Email"] = userProfile[PropertyConstants.WorkEmail].Value;//oSPUser.Email;
entity.Key = userProfile.AccountName;
entity.Description = userProfile.AccountName;
entity.DisplayText = userProfile.DisplayName;
entity = pe.ValidateEntity(entity);
if (entity != null)
{
entity.IsResolved = true;
savedEntity.Add(entity);
Logger.LogError(SPCustomCategory.RequestForm.ToString(),
string.Format("Entity object resolved in People editor {0}", userProfile.AccountName));
}
else
{
entity.IsResolved = true;
savedEntity.Add(entity);
Logger.LogError(SPCustomCategory.RequestForm.ToString(),
string.Format("Entity object not resolved in People editor {0}", userProfile.AccountName));
}
Hi,
ReplyDeleteThanks for coming up with this nice article.
We have also written a similar code to get users from saved DB and during page load set them to client people picker control
However, sometime we are seeing that the ID is not getting resolved as expected and the entire SAML token with user ID gets displayed on the people picker control
the sample code which adds users to List
PickerEntity entity = new PickerEntity();
PeopleEditor pe = new PeopleEditor();
entity.EntityData["AccountName"] = userProfile.AccountName;//oSPUser.LoginName;
entity.EntityData["SPUserID"] = userProfile.ID;//oSPUser.ID;
entity.EntityData["Email"] = userProfile[PropertyConstants.WorkEmail].Value;//oSPUser.Email;
entity.Key = userProfile.AccountName;
entity.Description = userProfile.AccountName;
entity.DisplayText = userProfile.DisplayName;
entity = pe.ValidateEntity(entity);
if (entity != null)
{
entity.IsResolved = true;
savedEntity.Add(entity);
Logger.LogError(SPCustomCategory.RequestForm.ToString(),
string.Format("Entity object resolved in People editor {0}", userProfile.AccountName));
}
else
{
entity.IsResolved = true;
savedEntity.Add(entity);
Logger.LogError(SPCustomCategory.RequestForm.ToString(),
string.Format("Entity object not resolved in People editor {0}", userProfile.AccountName));
}
Hi,
ReplyDeleteThanks for coming up with this nice article.
We have also written a similar code to get users from saved DB and during page load set them to client people picker control
However, sometime we are seeing that the ID is not getting resolved as expected and the entire SAML token with user ID gets displayed on the people picker control
the sample code which adds users to List
PickerEntity entity = new PickerEntity();
PeopleEditor pe = new PeopleEditor();
entity.EntityData["AccountName"] = userProfile.AccountName;//oSPUser.LoginName;
entity.EntityData["SPUserID"] = userProfile.ID;//oSPUser.ID;
entity.EntityData["Email"] = userProfile[PropertyConstants.WorkEmail].Value;//oSPUser.Email;
entity.Key = userProfile.AccountName;
entity.Description = userProfile.AccountName;
entity.DisplayText = userProfile.DisplayName;
entity = pe.ValidateEntity(entity);
if (entity != null)
{
entity.IsResolved = true;
savedEntity.Add(entity);
Logger.LogError(SPCustomCategory.RequestForm.ToString(),
string.Format("Entity object resolved in People editor {0}", userProfile.AccountName));
}
else
{
entity.IsResolved = true;
savedEntity.Add(entity);
Logger.LogError(SPCustomCategory.RequestForm.ToString(),
string.Format("Entity object not resolved in People editor {0}", userProfile.AccountName));
}
Hi,
DeleteIn my code I use the SPClaimProviderManager.Local.ConvertClaimToIdentifier when I want to have the ‘clear’ login name. For example:
PickerEntity pickerEntity = new PickerEntity()
{
Key = SPClaimProviderManager.Local.ConvertClaimToIdentifier(sPFieldUserValue.User.LoginName)
};
Try adding this in you code.
Hi I am facing one issue with this SP Client People Picker..Please help me out to resolve this issue. I have posted in msdn. Please go to:
ReplyDeletehttps://social.msdn.microsoft.com/Forums/office/en-US/0cd0cc35-3fe6-4638-b3eb-545ee0b2a33b/sharepoint-2013-client-people-picker-enter-key-press-issue?forum=sharepointdevelopment#0cd0cc35-3fe6-4638-b3eb-545ee0b2a33b
Hi sorry for posting on the post. i am required to do a form where the user will use the people picker to assign group members to the group. i would like to check how do i save the entries into my SQL DB such that it will ease me populating these values back when user want to edit the form. i have also posted this question on MSDN forum
ReplyDeletehttps://goo.gl/HxvHr4