In this blog, we will learn to integrate PnP people picker control in SharePoint framework (SPFx).
People picker is used to select one or more entities. Pickers are used to manage a group of people by selecting from the list of people within a group. You can make use of "defaultSelectedUsers" property when some people are already been selected.
In this approach, we are restricting the people picker control to accept single value.
Below steps explain how to integrate people picker control in your spfx solution, and how to insert, retrieve people picker values in your spfx solution.
Steps Involved
Install "@pnp/spfx-controls-react" from node package manager as shown below.
- PS C:\XXXX\SPFxSolutions\SpFxRichTextEditor>npm install --save @pnp/spfx-controls-react
Import "@pnp/spfx-controls-react/lib/PeoplePicker" to your tsx file.
- import { PeoplePicker, PrincipalType } from "@pnp/spfx-controls-react/lib/PeoplePicker";
- export interface IActionItemState {
- Assignee:number[];
- AssignedTo: string;
- }
- constructor(props: IActionItemProps) {
- super(props);
- this._getAssigneePeoplePickerItems = this._getAssigneePeoplePickerItems.bind(this);
- this.state = {
- Assignee:[],
- AssignedTo: ""
- };
- }
- public render(): React.ReactElement<IActionItemProps> {
- return (
- <div>
- <PeoplePicker
- context={this.props.ctx}
- titleText="Assigned To"
- personSelectionLimit={1}
- groupName={""} // Leave this blank in case you want to filter from all users
- showtooltip={false}
- isRequired={true}
- selectedItems={this._getAssigneePeoplePickerItems}
- principalTypes={[PrincipalType.User]}
- ensureUser={true}
- defaultSelectedUsers={[this.state.AssignedTo]}
- />
- </div>
- );
- }
personSelectionLimit={1} - restricts single user to be selected in the picker.
groupName = {""} - Leave this blank in case you want to filter from all the users, otherwise you can specify the group name, for eg : {"TestSharePointGroup"}
Note
Make sure your context is defined in the "webpart.ts" file as shown below,
Make sure your context is defined in the "webpart.ts" file as shown below,
- public render(): void {
- const element: React.ReactElement<ISpFxRichTextEditorProps > = React.createElement(
- SpFxRichTextEditor,
- {
- spHttpClient: this.context.spHttpClient,
- siteUrl: this.context.pageContext.web.absoluteUrl,
- ctx: this.context,
- }
- );
- }
- /*Get People Picker selected value */
- private _getAssigneePeoplePickerItems(items: any[]) {
- try {
- this.state.Assignee.length = 0;
- let tempTeamAssArr = [];
- for (let item in items) {
- tempTeamAssArr.push(items[item].id);
- }
- this.setState({
- Assignee: tempTeamAssArr
- });
- } catch (error) {
- console.log("Error in _getAssigneePeoplePickerItems : ", error);
- }
- }
To insert people picker values to sharepoint list follow the below steps.
Note
In the below example, I have assigned "this.state.Assignee[0]" to "AssignedToId". Usually, while inserting values to SharePoint people or group, you should assign values to its id instead of actual column name.
In the below example, I have assigned "this.state.Assignee[0]" to "AssignedToId". Usually, while inserting values to SharePoint people or group, you should assign values to its id instead of actual column name.
For Example:
If your column name in SharePoint list is "AssignedTo" then you have to pass values to its id like "AssignedToId".
If your column name in sharepoint list is "CopyTo" then you have to pass values to its id like "CopyToId" as shown below.
- public addItems(requester: SPHttpClient, siteUrl: string, listName: string): Promise<any[]>{
- try{
- const body: string = JSON.stringify({
- __metadata: { 'type': 'SP.Data.ActionItemFormListItem' },
- AssignedToId: this.state.Assignee[0]
- });
- return requester.post(`${siteUrl}/_api/web/lists/getbytitle('${listName}')/items`,
- SPHttpClient.configurations.v1,
- {
- headers: {
- "Accept": "application/json;odata=verbose",
- 'Content-type': 'application/json;odata=verbose',
- "odata-version": ""
- },
- body: body
- })
- .then((response: SPHttpClientResponse) => {
- return response.json();
- })
- .then((json) => {
- return(json);
- });
- }catch(error){
- console.log("Error in addItems : ", error );
- }
- }
Usage
- this.addItems(this.props.spHttpClient, this.props.siteUrl, "ActionItemForm")
- .then((items: any[]) => {
- console.log("inserted sucessfully!!");
- });
To retrieve people picker values from sharepoint list and to bind values to people picker control follow the below steps.
- private async GetItem(itemID: string) {
- try {
- var redirectionEmailURL = this.props.siteUrl + "/_api/web/lists/getbytitle('ActionItemForm')/Items?$filter=Id eq '" + itemID + "'&$select=AssignedTo/ID,AssignedTo/EMail,AssignedTo/Title&$expand=AssignedTo";
- const responseEmail = await this.props.ctx.spHttpClient.get(redirectionEmailURL, SPHttpClient.configurations.v1);
- const responseEmailJSON = await responseEmail.json();
- if (responseEmailJSON.value !== null) {
- var resultJSON = responseEmailJSON.value[0];
- if (resultJSON.AssignedTo != null) {
- this.setState({ AssignedTo: resultJSON.AssignedTo.EMail });
- }
- }
- } catch (error) {
- console.log("Error in GetItem : " + error);
- }
- }
Usage
parameter - Item Id has to be passed as a parameter to retrieve the specific item picker value.
- this.GetItem(oListItemID);
Please feel free to share your comments.
Hope this helps!!!!!

Join the conversation! Your thoughts help the community grow.