- Published on
Office UI Fabric People Picker in Property Pane – SharePoint Framework (SPFx)
This article demonstrates how to implement an Office UI Fabric People Picker inside the Property Pane of a SharePoint Framework (SPFx) web part.
This control allows users to easily search and select people within SharePoint while editing web part properties.
📦 Install required dependencies
Install Office UI Fabric and SPFx property controls libraries:
npm install @microsoft/sp-http --save
npm install office-ui-fabric-react --save
npm install @pnp/spfx-property-controls --save
💻 Implementation
Below is the main React component that implements the People Picker inside the property pane using Office UI Fabric.
import * as React from 'react';
import { IPersonaProps } from 'office-ui-fabric-react/lib/Persona';
import { IPropertyFieldMessageBarPropsInternal } from './PropertyPaneControl';
import { NormalPeoplePicker } from 'office-ui-fabric-react/lib/Pickers';
import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http';
import { autobind } from 'office-ui-fabric-react/lib//Utilities';
export default class customtext extends React.Component<IPropertyFieldMessageBarPropsInternal, {}> {
public render(): React.ReactElement<{}> {
return (
<div>
<NormalPeoplePicker onResolveSuggestions={this._onFilterChanged} resolveDelay={200} onChange={this._onChange1}/>
</div>
);
}
private _onChange1 = (items?:IPersonaProps[]) => {
this.props.onPropertyChange(items);
};
@autobind
private _onFilterChanged(filterText: string) {
if (filterText) {
if (filterText.length > 2) {
return this.searchPeople(filterText);
}
} else {
return [];
}
}
private searchPeople(terms: string): IPersonaProps[] | Promise<IPersonaProps[]> {
return new Promise<IPersonaProps[]>((resolve, reject) =>
this.props.spcontect.spHttpClient.get(`${this.props.spcontect.pageContext.web.absoluteUrl}/_api/search/query?querytext='*${terms}*'&rowlimit=10&sourceid='b09a7990-05ea-4af9-81ef-edfab16c4e31'`,
SPHttpClient.configurations.v1,
{
headers: {
'Accept': 'application/json;odata=nometadata',
'odata-version': ''
}
}).then((response: SPHttpClientResponse): Promise<{ PrimaryQueryResult: any }> => {
return response.json();
}).then((response: { PrimaryQueryResult: any }): void => {
let relevantResults: any = response.PrimaryQueryResult.RelevantResults;
let resultCount: number = relevantResults.TotalRows;
let people = [];
if (resultCount > 0) {
relevantResults.Table.Rows.forEach(function (row) {
let persona: IPersonaProps = {};
row.Cells.forEach(function (cell) {
if (cell.Key === 'JobTitle')
persona.secondaryText = cell.Value;
if (cell.Key === 'PictureURL')
persona.imageUrl = cell.Value;
if (cell.Key === 'PreferredName')
persona.primaryText = cell.Value;
});
people.push(persona);
});
}
resolve(people);
}, (error: any): void => {
reject();
}));
}
}
🧠 How it works
- The NormalPeoplePicker component is rendered inside the property pane.
- The
_onFilterChanged
method fetches user suggestions dynamically via the SharePoint Search REST API. - The
_onChange1
method triggers whenever a user is selected, updating the property value accordingly.
🚀 Deploy the solution
Once your implementation is ready, build and deploy the solution:
gulp build
gulp bundle --ship
gulp package-solution --ship
Upload the .sppkg
file to the App Catalog and deploy it. You can then use the custom People Picker field in the property pane.
📂 GitHub Source
View full SPFx project on GitHub:Office UI Fabric People Picker in Property Pane – SharePoint Framework (SPFx)
Author
- Ravichandran@Hi_Ravichandran