Render SPFx Properties Pane Based On List Data

 Introduction

In this article, let us see how to render the SharePoint framework web part properties pane dynamically using SharePoint list data.

Here in the sample, I am using the solution created for my previous sample. It was created with react JS framework.

Property Configuration 

All types of SPFx solutions contain the main web part TypeScript file (.ts). It contains the class with render method and property configuration method (getPropertyPaneConfiguration).

The property configuration method contains the default return statement. It returns the configuration which has pages to be rendered on the property pane along with the groups and the respective properties on the groups. The following is the default code. It contains

  • One page configuration with the page header
  • One group with group name and a text property
  1.  protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {  
  2.   return {  
  3.     pages: [  
  4.       {  
  5.         header: {  
  6.           description: strings.PropertyPaneDescription  
  7.         },  
  8.         groups: [  
  9.           {  
  10.             groupName: strings.BasicGroupName,  
  11.             groupFields: [  
  12.               PropertyPaneTextField('description', {  
  13.                 label: strings.DescriptionFieldLabel  
  14.               })  
  15.             ]  
  16.           }  
  17.         ]  
  18.       }  
  19.     ]  
  20.   };  
  21. }  

Here, in this sample, let us look at dynamically adding the list items in the properties pane of SharePoint Framework web part. Let us consider adding the check boxes to the properties pane.

Property pane configuration start method

The data to be pulled can be retrieved using the onPropertyPaneConfigurationStart method. The data from the list will not be available for rendering in the property configuration method, even though start method triggers before the configuration method. This is because the data to be retrieved from the list works asynchronously. The properties pane should be refreshed using the this.context.propertyPane.refresh() method in the code, which again triggers the properties configuration method. The property pane re-renders once the refresh method was called.

So, the property pane configuration start method will contain the code to retrieve the list items and dynamically build the check box properties and push it to the global array. The global array is then assigned to the group fields property in the property pane configuration method.

The configuration start method is shown below.
  1. protected onPropertyPaneConfigurationStart(): void {    
  2.   var self = this;  
  3.       var xmlhttp = new XMLHttpRequest();  
  4.       xmlhttp.onreadystatechange = function () {  
  5.           if (xmlhttp.readyState == XMLHttpRequest.DONE) {  
  6.               if (xmlhttp.status == 200) {  
  7.                   var data = JSON.parse(xmlhttp.responseText);  
  8.                   if (data != null) {  
  9.                     self.dynamicProps = [];  
  10.                       data.d.results.forEach(function(item){  
  11.                       //self.dynamicProps.push({key: item.ID, text: item.Title});  
  12.                       self.dynamicProps.push(    
  13.                       PropertyPaneCheckbox(item.ID,  
  14.                         {  
  15.                           text:item.Title  
  16.                         })     
  17.                       );  
  18.                     });  
  19.                     self.context.propertyPane.refresh();  
  20.                     
  21.                   }  
  22.               } else if (xmlhttp.status == 400) {  
  23.                   console.log('There was an error 400');  
  24.                     
  25.               } else {  
  26.                   console.log('something else other than 200 was returned');  
  27.               }  
  28.                 
  29.           }  
  30.       };  
  31.   
  32.       var inputUrl = "https://nakkeerann.sharepoint.com/sites/spfx/_api/web/lists/getByTitle('TestList')/items";  
  33.       xmlhttp.open("GET", inputUrl, true);  
  34.       xmlhttp.setRequestHeader("Accept","application/json; odata=verbose");  
  35.       xmlhttp.send();          
  36. }  
The property pane configuration method contains the return statement with pane pages and groups with properties. The configuration method is shown below.
  1. protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {  
  2.   return {  
  3.     pages: [  
  4.       {  
  5.         header: {  
  6.           description: "SPFx Properties"  
  7.         },  
  8.         groups: [  
  9.           {  
  10.             groupName: "Dynamic Properties",  
  11.             groupFields:   
  12.               this.dynamicProps  
  13.               
  14.           }  
  15.         ]  
  16.       }  
  17.     ]  
  18.   };  
  19. }   
The below snapshot shows the SPFx web part with the dynamically added pane properties.
 

Up Next
    Ebook Download
    View all
    Learn
    View all