Introduction
In this article, we will learn how to implement listpicker and associated fieldpicker in propertypane of SPFx webpart component.
Steps
Open a command prompt and create a directory for the SPFx solution.
md spfx-ListAndFieldPicker
Navigate to the above created directory.
cd spfx-ListAndFieldPicker
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit enter for the default name (spfx-ListAndFieldPicker 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 be accessible everywhere.
Selected choice - N (install on each site explicitly).
Permissions to access web APIs
Choose if the components in the solution require permission 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 the web part option.
Selected choice - WebPart
Web part name
Hit enter to select the default name or type in any other name.
Selected choice - ListAndFieldPicker
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
The 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 the below command,
npm shrinkwrap
In the command prompt, type the below command to open the solution in the code editor of your choice.
npm install @pnp/spfx-property-controls --save --save-exact
Needed Props
for Listpicker,
lists: string | string[];
for FieldPicker,
list: string; // Stores the list ID
column: string; // Stores the single column property (property can be configured
multiColumn: string; // Stores the multi column property (property can be configured)
Initialization model for list picker,
PropertyFieldListPicker('lists', {
label: 'Select a list',
selectedList: this.properties.lists,
includeHidden: false,
orderBy: PropertyFieldListPickerOrderBy.Title,
disabled: false,
onPropertyChange: this.onPropertyPaneFieldChanged.bind(this),
properties: this.properties,
context: this.context,
onGetErrorMessage: null,
deferredValidationTime: 0,
key: 'listPickerFieldId'
})
Initialization model for column picker,
//single column
PropertyFieldColumnPicker('column', {
label: 'Select a column',
context: this.context,
selectedColumn: this.properties.column,
listId: this.properties.singleListFiltered,
disabled: false,
orderBy: PropertyFieldColumnPickerOrderBy.Title,
onPropertyChange: this.onPropertyPaneFieldChanged.bind(this),
properties: this.properties,
onGetErrorMessage: null,
deferredValidationTime: 0,
key: 'columnPickerFieldId',
displayHiddenColumns: false,
columnReturnProperty: IColumnReturnProperty["Internal Name"]
})
// Multi column
PropertyFieldColumnPicker('multiColumn', {
label: 'Select columns',
context: this.context,
selectedColumn: this.properties.multiColumn,
listId: this.properties.singleListFiltered,
disabled: false,
orderBy: PropertyFieldColumnPickerOrderBy.Title,
onPropertyChange: this.onPropertyPaneFieldChanged.bind(this),
properties: this.properties,
onGetErrorMessage: null,
deferredValidationTime: 0,
key: 'multiColumnPickerFieldId',
displayHiddenColumns: false,
columnReturnProperty: IColumnReturnProperty.Title,
multiSelect: true
})
In some version of spfx you will face context error in the line,
context: this.context
You can resolve that using the below replacement of code,
context: this.context as any,
Full Code
In IListAndFieldPickerProps.ts,
import { WebPartContext } from "@microsoft/sp-webpart-base";
export interface IListAndFieldPickerProps {
description: string;
context:WebPartContext;
lists: string ;
multiColumn:string;
}
In ListAndFieldPickerWebPart.ts,
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
IPropertyPaneConfiguration,
PropertyPaneTextField
} from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
import * as strings from 'ListAndFieldPickerWebPartStrings';
import ListAndFieldPicker from './components/ListAndFieldPicker';
import { IListAndFieldPickerProps } from './components/IListAndFieldPickerProps';
import { PropertyFieldListPicker, PropertyFieldListPickerOrderBy } from '@pnp/spfx-property-controls/lib/PropertyFieldListPicker';
import { PropertyFieldColumnPicker, PropertyFieldColumnPickerOrderBy } from '@pnp/spfx-property-controls/lib/PropertyFieldColumnPicker';
export interface IListAndFieldPickerWebPartProps {
description: string;
lists: string;
list: string;
column: string;
multiColumn: string;
}
enum IColumnReturnProperty{
Id = 0,
Title = 1,
"Internal Name" = "InternalName"
}
export default class ListAndFieldPickerWebPart extends BaseClientSideWebPart<IListAndFieldPickerWebPartProps> {
public render(): void {
const element: React.ReactElement<IListAndFieldPickerProps> = React.createElement(
ListAndFieldPicker,
{
description: this.properties.description,
context:this.context ,
lists:this.properties.lists,
multiColumn:this.properties.multiColumn
}
);
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
}),PropertyFieldListPicker('lists', {
label: 'Select a list',
selectedList: this.properties.lists,
includeHidden: false,
orderBy: PropertyFieldListPickerOrderBy.Title,
disabled: false,
onPropertyChange: this.onPropertyPaneFieldChanged.bind(this),
properties: this.properties,
context: this.context as any,
onGetErrorMessage: null,
deferredValidationTime: 0,
key: 'listPickerFieldId'
}),
PropertyFieldColumnPicker('multiColumn', {
label: 'Select columns',
context: this.context as any,
selectedColumn: this.properties.multiColumn,
listId: this.properties.lists,
disabled: false,
orderBy: PropertyFieldColumnPickerOrderBy.Title,
onPropertyChange: this.onPropertyPaneFieldChanged.bind(this),
properties: this.properties,
onGetErrorMessage: null,
deferredValidationTime: 0,
key: 'multiColumnPickerFieldId',
displayHiddenColumns: false,
columnReturnProperty: IColumnReturnProperty['Internal Name'],
multiSelect: true
})
]
}
]
}
]
};
}
}
You can refer to these two links for the complete properties of the controls.
Expected output
You get the values in your tsx file as,
Conclusion
That's all, we have successfully learned about the implementation of Listpicker and associated column picker in spfx propertypane, Happy Coding :)