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. {  

  3. return {      

  4. pages: [        

  5. {          

  6. header: {            

  7. description: strings.PropertyPaneDescription          

  8. },          

  9. groups: [            

  10. {              

  11. groupName: strings.BasicGroupName,              

  12. groupFields: [                

  13. PropertyPaneTextField('description', 

  14. {                  

  15. label: strings.DescriptionFieldLabel                

  16. })              

  17. ]            

  18. }          

  19. ]        

  20. }      

  21. ]    

  22. };  

  23. }  


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. {  

  7. if (xmlhttp.status == 200) {  

  8. var data = JSON.parse(xmlhttp.responseText);  

  9. if (data != null) {                      

  10. self.dynamicProps = [];                        

  11. data.d.results.forEach(function(item){  

  12. / /self.dynamicProps.push({key: item.ID, text: item.Title});                       

  13. self.dynamicProps.push(                          

  14. PropertyPaneCheckbox(item.ID,                          

  15. {                            

  16. text:item.Title                          

  17. })                           

  18. );                      

  19. });                      

  20. self.context.propertyPane.refresh();                    

  21.  }                

  22. else

  23. if (xmlhttp.status == 400) {                    

  24. console.log('There was an error 400');                 

  25. else {                    

  26. console.log('something else other than 200 was returned');                

  27. }             

  28. }        

  29. };  

  30. var inputUrl = "https://nakkeerann.sharepoint.com/sites/spfx/_api/web/lists/getByTitle('TestList')/items";       

  31.  xmlhttp.open("GET", inputUrl, true);        

  32. xmlhttp.setRequestHeader("Accept","application/json; odata=verbose");        

  33. xmlhttp.send();          

  34. }  


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. {  

  3. return 

  4. {      

  5. pages: [        

  6. {          

  7. header: {           

  8.  description: "SPFx Properties"         

  9. },          

  10. groups: [            

  11. {              

  12. groupName: "Dynamic Properties",              

  13. groupFields:    this.dynamicProps             

  14. }          

  15. ]        

  16. }      

  17. ]    

  18. };  

  19. }  


The below snapshot shows the SPFx web part with the dynamically added pane properties.



Recent Posts

See All