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.
- Customizing SharePoint Framework Web Part Properties - Part One
- Customizing SharePoint Framework Web Part Properties - Part Two
- Customizing SharePoint Framework Web Part Properties - Part Three
- Customizing SharePoint Framework Web Part Properties - Part Four
- Customizing SharePoint Framework Web Part Properties - Part Five
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 {
- // loads list name into list dropdown
- this.GetLists();
- }
- private GetLists():void{
- // REST API to pull the list names
- let listresturl: string = this.context.pageContext.web.absoluteUrl + "/_api/web/lists?$select=Id,Title";
- this.LoadLists(listresturl).then((response)=>{
- // Render the data in the web part
- this.LoadDropDownValues(response.value);
- });
- }
- private LoadLists(listresturl:string): Promise<spLists>{
- // Call to site to get the list names
- return this.context.httpClient.get(listresturl).then((response: Response)=>{
- return response.json();
- });
- }
- private LoadDropDownValues(lists: spList[]): void{
- lists.forEach((list:spList)=>{
- // Loads the drop down values
- 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.

Kaushik DudhatPosted Feb 26, 2020, 3:44 AM
I got the error like "error TS2554: Expected 2-3 arguments, but got 1." in "return this.context.httpClient.get(listresturl).then((response: Response)=>{" this line. Please suggest me.
Ravishankar SinghPosted Jan 13, 2017, 2:51 AM
Good Article Nakkeeran :)