Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillipharding/4bebd5dc6f35a1b815d6 to your computer and use it in GitHub Desktop.
Save phillipharding/4bebd5dc6f35a1b815d6 to your computer and use it in GitHub Desktop.
A typescript SharePoint client peoplepicker control helper
"use strict";
/**
* To debug in browser console;
*
* require(['lib/sppeoplepickerhelper'], function(sppeoplepickerhelper) { window.PeoplePickerHelper = sppeoplepickerhelper; } );
* var sme = new PeoplePickerHelper('SME');
*
*/
/**
* PeoplePickerHelper
*/
class PeoplePickerHelper {
private _internalColumnName:string;
private _control:SPClientPeoplePicker;
/** constructor
* @param {any} internalColumnName
*/
constructor(internalColumnName) {
this._internalColumnName = internalColumnName;
this._control = PeoplePickerHelper.getControl(this._internalColumnName);
}
/** retrieves the people picker control
* @returns any
*/
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;
}
/**
* removes all entities from the 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);
});
}
/**
* removes an entity from the people picker control
* @param {string} entityKey - user email address or SP group name
* @returns boolean - true if an entity was removed
*/
public removeEntity(entityKey : string): boolean {
if ( (!entityKey || !entityKey.length) ||
(!this._control || !this._control.ResolvedListElementId) )return;
var removed: boolean = false;
$(document.getElementById(this._control.ResolvedListElementId))
.children()
.each( (i:number, el:HTMLElement ) => {
if ((el.id.indexOf('membership|'+entityKey+'_') !== -1) ||
(el.id.indexOf('_'+entityKey+'_') !== -1) ) {
this._control.DeleteProcessedUser(el);
removed = true;
}
});
return removed;
}
/**
* adds an unresolved entity to the people picker control
* @param {string} entityText
*/
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);
}
/**
* adds a resolved entity to the people picker control
* @param {string} entityKey - user email address or SP group name
* @param {boolean} search?
*/
public addResolvedEntity(entityKey: string, search?: boolean): void {
if ( (!entityKey || !entityKey.length) ||
(!this._control || !this._control.ResolvedListElementId) )return;
this._control.AddUserKeys(entityKey, search);
}
/**
* returns all resolved user information
* @returns ISPClientPeoplePickerEntity[]
*/
public getEntities(): ISPClientPeoplePickerEntity[] {
var userInfo = this._control.GetAllUserInfo();
return userInfo || ([] as ISPClientPeoplePickerEntity[]);
}
/**
* indicates if the supplied entity is selected in the people picker control
* @param {string} entityKey - user email address or SP group name
* @returns boolean
*/
public isEntitySelected(entityKey: string) : boolean {
if (!entityKey || !entityKey.length) return false;
var r:RegExp = new RegExp('^'+entityKey+'$', "gi");
var isMatched: boolean = false;
var entities: ISPClientPeoplePickerEntity[] = this.getEntities();
$.each(entities, (index:number, entity:any) => {
if (entity.EntityData) {
if (entity.EntityData.Email && entity.EntityData.Email.match(r)) {
/** matched a user */
isMatched = true;
return false; /** break the loop */
} else if (entity.EntityData.SPGroupID && entity.EntityData.AccountName && entity.EntityData.AccountName.match(r)) {
/** matched a SP Group */
isMatched = true;
return false; /** break the loop */
}
}
});
return isMatched;
}
}
/** for ES6 use
export default PeoplePickerHelper;
*/
/** for ES5 compatible use with RequireJS */
export = PeoplePickerHelper;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment