The SharePoint people picker client-side control is a bit of an opaque beast, many times when you’re doing a client-side customisation you might want to Add or Remove person or group entities or otherwise examine the list of entities the user has selected.
The good news is that there is an API of sorts found in clientpeoplepicker.js, as long as you can target the specific people picker control you’re interested in.
The samples that follow are written in TypeScript but are easily converted to JavaScript, see the GIST at the end of the post for the complete module.
Get a People Picker Control Instance.
public static getControl(internalColumnName) : SPClientPeoplePicker {
if (!internalColumnName || !internalColumnName.length) {
return null;
}
var ppTopLevelDiv:JQuery = $(".sp-peoplepicker-topLevel[id^='" + internalColumnName + "_']");
if (!ppTopLevelDiv.length) return null;
var control = SPClientPeoplePicker.SPClientPeoplePickerDict[ppTopLevelDiv[0].id];
return control;
}
This yields the SharePoint people picker control for the (Internal) column name specified.
Clear all Entities from a People Picker Control.
public clear(): void {
if (!this._control || !this._control.ResolvedListElementId) return;
$(document.getElementById(this._control.ResolvedListElementId))
.children()
.each( (i:number, el:HTMLElement ) => {
this._control.DeleteProcessedUser(el);
});
}
public addResolvedEntity(entityKey: string, search?: boolean): void {
if ( (!entityKey || !entityKey.length) ||
(!this._control || !this._control.ResolvedListElementId) )return;
this._control.AddUserKeys(entityKey, search);
}
The supplied entityKey is either a user email address (Office365) or user account name (On-Premises) or a SharePoint Group name
Add an Unresolved Entity to a People Picker Control.
public addUnresolvedEntity(entityText: string): void {
if ( (!entityText || !entityText.length) ||
(!this._control || !this._control.ResolvedListElementId) )return;
var ppUserInput = $(document.getElementById(this._control.EditorElementId));
ppUserInput.val(entityText);
this._control.AddUnresolvedUserFromEditor(true);
}
This is equivalent to the user typing into the edit box
Return all Entity Info from the People Picker Control.
public getEntities(): ISPClientPeoplePickerEntity[] {
var userInfo = this._control.GetAllUserInfo();
return userInfo || ([] as ISPClientPeoplePickerEntity[]);
}
GIST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Published by