Introduction
In this article, you will learn how to populate cascading dropdown options in SharePoint Framework (SPFx) web part properties pane.
In my SPFx article series, you can learn about SPFx introduction, prerequisites, steps for setting up the environment, developing, and testing the web parts, using the local environments.
The below articles will help you with basic SPFx web part properties pane customization.
In my
previous article, you have seen how to add and bind values dynamically to the dropdown field of web part property pane. In this article, you will see how to populate cascading dropdown fields with dynamic SharePoint list content. I have used two dropdowns in this sample.
Declare Drop down Fields/Properties
The dropdown values appended to the field is of type IPropertyPaneDropdownOption. The variables "listDropDownOptions" and "itemDropDownOptions" of same type are declared inside the class.
The below snippet shows the property declaration.
- private listDropDownOptions: IPropertyPaneDropdownOption[] =[];
- private itemDropDownOptions: IPropertyPaneDropdownOption[] = [];
Define Properties
The properties are defined using propertyPaneSettings() method. The below snippet shows the properties defined.
- protected get propertyPaneSettings(): IPropertyPaneSettings {
-
- return {
- pages: [
- {
- header: {
- description: strings.PropertyPaneDescription,
- },
- groups: [
- {
- groupName:"Lists",
- groupFields:[
- PropertyPaneDropdown('listDropDown',{
- label: "Select List To Display on the page",
- options:this.listDropDownOptions,
- isDisabled: false
- }),
- PropertyPaneDropdown('ItemsDropDown',{
- label: "Select Item to display",
- options: this.itemDropDownOptions,
- isDisabled: false
-
- })
- ]
- }
- ]
- }
- ]
- };
- }
Note - There are two properties defined. One dropdown is for list source and the other dropdown is for listing down the items of the list. As you can see, the values are not appended directly, rather variable of type IPropertyPaneDropdownOption is assigned.
Load First Dropdown (List Dropdown)
Initially, on the property pane load, all the lists available in the SharePoint site should be listed in the list dropdown. Next, we need to load the list names on the dropdown field dynamically.
To load the values, onPropertyPaneConfigurationStart method is used. It loads only when the property pane is opened. So, this method will be used in the current sample.
Note - There is one more way of loading the dropdown values, explained in the
previous article.
The below snippet shows the methods to load the list names into list dropdown. The custom function is written to load the SharePoint list names into the dropdown property. Here, this.listDropDownOptions property is loaded with the list names, which will reflect in the list dropdown property.
- protected onPropertyPaneConfigurationStart(): void {
-
- this.GetLists();
-
- }
-
- private GetLists():void{
-
- let listresturl: string = this.context.pageContext.web.absoluteUrl + "/_api/web/lists?$select=Id,Title";
-
- this.LoadLists(listresturl).then((response)=>{
-
- this.LoadDropDownValues(response.value);
- });
- }
-
- private LoadLists(listresturl:string): Promise<spLists>{
-
- return this.context.httpClient.get(listresturl).then((response: Response)=>{
- return response.json();
- });
- }
-
- private LoadDropDownValues(lists: spList[]): void{
- lists.forEach((list:spList)=>{
-
- this.listDropDownOptions.push({key:list.Title,text:list.Title});
- });
- }
Load Second Dropdown (Items Dropdown)
Then, once the list name is selected, the items dropdown property should be populated. This can be done by overriding onPropertyChange method. The method takes two parameters (property path and new value). The function should be executed only when the list dropdown property is changed.
The below snippet shows the methods to load the items (item Title field) into list dropdown. The custom function is written to load the SharePoint item titles into the items dropdown property. Here, this.itemsDropDownOptions property is loaded with the item titles, which will reflect in the item dropdown property.
- protected onPropertyChange(propertyPath: string, newValue: any):void{
- if(propertyPath === "listDropDown"){
-
- super.onPropertyChange(propertyPath,newValue);
-
- this.properties.ItemsDropDown = undefined;
- this.onPropertyChange('ItemsDropDown', this.properties.ItemsDropDown);
-
- this.GetItems();
- }
- else {
-
- super.onPropertyChange(propertyPath, newValue);
- }
- }
-
- private GetItems(): void{
-
- if(this.properties.listDropDown != undefined){
- let url: string = this.context.pageContext.web.absoluteUrl + "/_api/web/lists/getbytitle('"+this.properties.listDropDown+"')/items?$select=ID,Title,Created,Author/Title&$expand=Author";
-
- this.GetItemsDropDown(url).then((response)=>{
-
- this.LoadItemsDropDown(response.value);
- });
- }
- }
-
- private GetItemsDropDown(listresturl:string): Promise<spListItems>{
-
- return this.context.httpClient.get(listresturl).then((response: Response)=>{
- return response.json();
- });
- }
-
- private LoadItemsDropDown(listitems: spListItem[]): void{
-
- this.itemDropDownOptions = [];
- if(listitems != undefined){
- listitems.forEach((listItem:spListItem)=>{
- this.itemDropDownOptions.push({key:listItem.ID,text:listItem.Title});
- });
- }
- }
Render Web Part
The data displayed on the web part is rendered using render method. The web part should be rendered based on the values selected from two dropdowns. The custom functions are written inside render() method to display the list items based on the list name and items selected from the dropdowns.
The below snippet shows the functions to render the data on the web part.
- public render(): void {
-
- this.domElement.innerHTML = `
- <div class="${styles.listItemsForm}">
- <div class="${styles.Table}">
- <div class="${styles.Heading}">
- <div class="${styles.Cell}">Title</div>
- <div class="${styles.Cell}">Created</div>
- <div class="${styles.Cell}">Author</div>
- </div>
- </div>
- </div>`;
- console.log("Render");
-
- this.LoadData();
- }
-
- private LoadData(): void{
- if(this.properties.listDropDown != undefined && this.properties.ItemsDropDown != undefined){
- let url: string = this.context.pageContext.web.absoluteUrl + "/_api/web/lists/getbytitle('"+this.properties.listDropDown+"')/items?$select=Title,Created,Author/Title&$expand=Author&$filter=ID eq "+this.properties.ItemsDropDown;
- this.GetListData(url).then((response)=>{
-
- this.RenderListData(response.value);
- });
- }
- }
-
- private GetListData(url: string): Promise<spListItems>{
-
- return this.context.httpClient.get(url).then((response: Response)=>{
- return response.json();
- });
- }
-
- private RenderListData(listItems: spListItem[]): void{
- let itemsHtml: string = "";
-
- listItems.forEach((listItem: spListItem)=>{
- itemsHtml += `<div class="${styles.Row}">`;
- itemsHtml += `<div class="${styles.Cell}"><p>${listItem.Title}</p></div>`;
- itemsHtml += `<div class="${styles.Cell}"><p>${listItem.Created}</p></div>`;
- itemsHtml += `<div class="${styles.Cell}"><p>${listItem.Author.Title}</p></div>`;
-
- itemsHtml += `</div>`;
- });
- this.domElement.querySelector("."+styles.Table).innerHTML +=itemsHtml;
- }
Interfaces
The interfaces required for the sample can be found below.
- export interface spListItems{
- value: spListItem[];
- }
- export interface spListItem{
- Title: string;
- ID: string;
- Created: string;
- Author: {
- Title: string;
- };
- }
-
- export interface spList{
- Title:string;
- id: string;
- }
- export interface spLists{
- value: spList[];
- }
The below snapshot shows the web part with the cascading dropdown fields on SPFx web part properties pane.
Summary
Thus, you have learned how to build the cascading dropdown and populate the values dynamically on the SharePoint Framework web part properties pane.