Develop SharePoint Framework Web Part.
Open a command prompt. Create a directory for SPFx solution.
md spfx-pnp-PeoplePicker
Navigate to the above created directory.
cd sspfx-pnp-PeoplePicker
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit Enter to have default name (spfx-pnpExcel in this case) or type in any other name for your solution.
Selected choice - Hit Enter
Target for the component
Here, we can select the target environment where we are planning to deploy the client web part, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice: SharePoint Online only (latest)
Place of files
We may choose to use the same folder or create a subfolder for our solution.
Selected choice: Same folder
Deployment option
Selecting Y will allow the app to be deployed instantly to all sites and will be accessible everywhere.
Selected choice: N (install on each site explicitly)
Permissions to access web APIs
Choose if the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant.
Selected choice: N (solution contains unique permissions)
Type of client-side component to create
We can choose to create a client-side web part or an extension. Choose web part option.
Selected choice: WebPart
Web part name
Hit Enter to select the default name or type in any other name.
Selected choice: PnpPeoplePicker
Web part description
Hit Enter to select the default description or type in any other value.
Framework to use
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice: React
Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
Once the scaffolding process is completed, lock down the version of project dependencies by running below command.
npm shrinkwrap
In the command prompt, type below command to open the solution in the code editor of your choice.
code .
NPM Packages Used,
On the command prompt, run below command.
npm i @pnp/logging @pnp/common @pnp/odata @pnp/sp --save
npm install --save @pnp/polyfill-ie11
npm install @pnp/spfx-controls-react --save --save-exact
In PnpPeoplePickerWebPart.ts
- import * as strings from 'PnpPeoplePickerWebPartStrings';
- import PnpPeoplePicker from './components/PnpPeoplePicker';
- import { IPnpPeoplePickerProps } from './components/IPnpPeoplePickerProps';
- export interface IPnpPeoplePickerWebPartProps {
- description: string;
- }
- export default class PnpPeoplePickerWebPart extends BaseClientSideWebPart<IPnpPeoplePickerWebPartProps> {
- public render(): void {
- const element: React.ReactElement<IPnpPeoplePickerProps > = React.createElement(
- PnpPeoplePicker,
- {
- context: this.context
- }
- );
- ReactDom.render(element, this.domElement);
- }
- protected onDispose(): void {
- ReactDom.unmountComponentAtNode(this.domElement);
- }
- protected get dataVersion(): Version {
- return Version.parse('1.0');
- }
- protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
- return {
- pages: [
- {
- header: {
- description: strings.PropertyPaneDescription
- },
- groups: [
- {
- groupName: strings.BasicGroupName,
- groupFields: [
- PropertyPaneTextField('description', {
- label: strings.DescriptionFieldLabel
- })
- ]
- }
- ]
- }
- ]
- };
- }
- }
- import { IPnPPeoplePickerState } from './IPnPPeoplePickerState';
- import { PeoplePicker, PrincipalType } from "@pnp/spfx-controls-react/lib/PeoplePicker";
- import "@pnp/polyfill-ie11";
- import { autobind ,DefaultButton} from 'office-ui-fabric-react';
- import { sp, Web } from '@pnp/sp';
- var managerlistid: number;
- export default class PnpPeoplePicker extends React.Component<IPnpPeoplePickerProps, IPnPPeoplePickerState> {
- constructor(props: IPnpPeoplePickerProps, state: IPnPPeoplePickerState) {
- super(props);
- this.state = {
- addUsers: [],
- defaultmyusers: []
- };
- this.fetchdatas = this.fetchdatas.bind(this);
- this.fetchdatas();
- }
- private async fetchdatas() {
- const web = new Web(this.props.context.pageContext.web.absoluteUrl);
- const list1 = web.lists.getByTitle("Program");
- var myemail = [];
- var mynewid = [];
- var FetchProjectDetails = [];
- await list1.items.getById(1).select('Id,Mast/Id,Mast/Title,Mast/Name,Mast/EMail').expand('Mast').get().then(r => {
- console.log(r);
- managerlistid = r.Id;
- for (let i = 0; i < r.Mast.length; i++) {
- myemail.push(r.Mast[i].EMail);
- mynewid.push({
- id: r.Mast[i].Id,
- managername: r.Mast[i].Name
- });
- }
- this.setState({ defaultmyusers: myemail });
- });
- }
- public render(): React.ReactElement<IPnpPeoplePickerProps> {
- return (
- <div>
- <div id="Mast">
- <PeoplePicker
- context={this.props.context}
- titleText="Mast"
- personSelectionLimit={3}
- groupName={""} // Leave this blank in case you want to filter from all users
- showtooltip={true}
- isRequired={true}
- disabled={false}
- ensureUser={true}
- showHiddenInUI={false}
- principalTypes={[PrincipalType.User]}
- defaultSelectedUsers={this.state.defaultmyusers}
- resolveDelay={1000} />
- </div>
- <div id="Client">
- <PeoplePicker
- context={this.props.context}
- titleText="Client"
- personSelectionLimit={3}
- groupName={""} // Leave this blank in case you want to filter from all users
- showtooltip={true}
- isRequired={true}
- disabled={false}
- ensureUser={true}
- showHiddenInUI={false}
- principalTypes={[PrincipalType.User]}
- selectedItems={this._getPeoplePickerclient.bind(this)}
- resolveDelay={1000} />
- <DefaultButton
- data-automation-id="addSelectedUsers"
- title="Add Selected Users"
- onClick={this.addSelectedUsers}>
- Add Project
- </DefaultButton>
- </div>
- </div>
- );
- }
- private _getPeoplePickerclient(items: any[]) {
- console.log('Items:', items);
- this.setState({ addUsers: items });
- }
- @autobind
- private addSelectedUsers(): void{
- var peoplepicarray = [];
- for (let i = 0; i < this.state.addUsers.length; i++) {
- peoplepicarray.push(this.state.addUsers[i]["id"]);
- }
- var Clientdata ={
- "ClientId": { "results": peoplepicarray }
- };
- sp.web.lists.getByTitle("Program").items.add(Clientdata).then(i => {
- console.log("done");
- });
- }
- }
IPnPPeoplePickerState.ts
- export interface IPnPPeoplePickerState {
- addUsers: string[];
- defaultmyusers?:any[];
- }
- import { WebPartContext } from '@microsoft/sp-webpart-base';
- export interface IPnpPeoplePickerProps {
- context: WebPartContext;
- }
Here, in the first Peoplepicker I have bound the values taken from the list (setting default value).
The second peoplepicker is for initialization and saving peoplepicker values to the list.
My list name is program and I have 2 peoplepicker columns, namely Mast and Client, where I have to fetch Mast values from the list and bind in the first peoplepicker
In the second peoplepicker by clicking addSelectedUsers button the values are stored in the client column of the list where I have entered users.
Happy Coding :)

Join the conversation! Your thoughts help the community grow.