Create custom property in SharePoint Framework - SPFx web part pane

In 2 steps instantly create custom property in SharePoint Framework โ€“ SPFx web part pane

3 comments

Loading

In this โ€œCreate custom property in SharePointโ€ article, we will learn about how we can add a custom additional property to the SharePoint Framework -SPFx web part in the web part configuration pane. This is my third article in the SPFx framework learning series โ€“ you may refer to my previous articles:

Create custom property in SharePoint Framework โ€“ SPFx web part pane

First, letโ€™s look at the below screenshot โ€“ when we developed the default first hello world web part, we can see only the โ€œDescriptionโ€ property in the web part property configuration pane. However, now we could see some additional configurable properties like Multi-line-Text Field, Checkbox, Dropdown, and Toggle control in the web part configuration pane.ย  Here, in this article, I will explain how these additional controls have been added over here.

Add custom property in SPFx web part - configuration pane
Add a custom property in the SPFx web part โ€“ configuration pane

Create custom property in SharePoint Framework (custom properties SharePoint) โ€“ Coding

I am not describing how to create a new project here โ€“ I am taking forward my previous demo project(Hello World), and starting from here.ย  Step 1: Import the necessary property pane from the โ€˜@microsoft/sp-property-paneโ€™ ย Letโ€™s open the HelloWorldWebPart.ts file from the project file structure. At the beginning of this file, we must add the below code otherwise this will not work.

import {
IPropertyPaneConfiguration,
PropertyPaneTextField,
PropertyPaneCheckbox,
PropertyPaneDropdown,
PropertyPaneToggle
} from '@microsoft/sp-property-pane';
ย 
@microsoft/sp-property-pane for the custom property in the HelloWorldWebPart.ts file.
ย 
Import microsoft-sp-property-pane for the custom property
Import microsoft-sp-property-pane for the custom property

Note:

  • In the above import, we can import as many allowed property pane we want โ€“ here I am importing Text Field, Checkbox, Dropdown, Toggle.

Step 2:ย 

Add the above fields in the export section. In the below IHelloWorldWebPartProps interface by default when we create default hello world web part, we will just have โ€œdescription: stringโ€ property now we have added the below additional property test: string; test1: boolean; test2: string; test3: boolean;

exportย interfaceย IHelloWorldWebPartProps

ย {

ย ย description:ย string;

ย ย test:ย string;

ย ย test1:ย boolean;

ย ย test2:ย string;

ย ย test3:ย boolean;

}

Step 3:ย  Handle the page render section Go to the default class HelloWorldWebPart extends BaseClientSideWebPart <IHelloWorldWebPartProps> section. By default, we can see the below line for the rendering of the description field. <pย class=โ€${ย styles.descriptionย }โ€>${escape(this.properties.description)}</p>

ย 
Then below to that, we can add the custom additional fields like added below(see the high lighted lines).
exportย defaultย classย HelloWorldWebPartย extendsย BaseClientSideWebPartย <IHelloWorldWebPartProps>

ย {

ย ย publicย render():ย voidย {

ย ย ย ย this.domElement.innerHTMLย =ย `

ย ย ย ย ย ย <divย class="${ย styles.helloWorldย }">

ย ย ย ย <divย class="${ย styles.containerย }">

ย ย ย ย ย ย <divย class="${ย styles.rowย }">

ย ย ย ย ย ย ย ย <divย class="${ย styles.columnย }">

ย ย ย ย ย ย ย ย ย ย <spanย class="${ย styles.titleย }">Welcomeย toย SharePoint!</span>

ย ย <pย class="${ย styles.subTitleย }">Customizeย SharePointย experiencesย usingย Webย Parts.</p>

ย ย ย ย <pย class="${ย styles.descriptionย }">${escape(this.properties.description)}</p>

ย ย ย ย <pย class="${ย styles.descriptionย }">${escape(this.properties.test)}</p>ย ย ย ย 

ย ย ย ย <pย class="${ย styles.descriptionย }">${escape(this.properties.test2)}</p>ย ย ย ย 

ย ย ย ย ย ย <aย href="https://aka.ms/spfx"ย class="${ย styles.buttonย }">

ย ย ย ย ย ย ย ย <spanย class="${ย styles.labelย }">Learnย more</span>

ย ย ย ย ย ย ย ย ย ย </a>

ย ย ย ย ย ย ย ย ย ย </div>

ย ย ย ย ย ย ย ย ย ย </div>

ย ย ย ย ย ย ย ย ย ย </div>

ย ย ย ย ย ย ย ย ย ย </div>`;

ย ย }
ย 
Step 4:
ย 
Grouping the custom columns

Now, in the same file โ€“ letโ€™s go to the getPropertyPaneConfiguration() method which has been inherited from IPropertyPaneConfiguration. We can see all the below additional properties have been added in the โ€œGroupsโ€ section just below the default property description.

  • PropertyPaneTextField
  • PropertyPaneCheckbox
  • PropertyPaneDropdown
  • PropertyPaneToggle
protected getPropertyPaneConfiguration(): IPropertyPaneConfigurationย 
{

ย ย returnย 

ย ย {

ย ย ย ย pages:ย [

ย ย ย ย ย ย {

ย ย ย ย ย ย ย ย header:

ย ย ย ย ย ย ย ย ย {

ย ย ย ย ย ย ย ย ย ย description:ย strings.PropertyPaneDescription

ย ย ย ย ย ย ย ย },

ย ย ย ย ย ย ย ย groups:ย [

ย ย ย ย ย ย ย ย ย ย {

ย ย ย ย ย ย ย ย ย ย ย ย groupName:ย strings.BasicGroupName,

ย ย ย ย ย ย ย ย ย ย ย ย groupFields:ย [

ย ย ย ย ย ย ย ย ย ย ย ย PropertyPaneTextField('description',ย {

ย ย ย ย ย ย ย ย ย ย ย ย ย ย label:ย 'Description'

ย ย ย ย ย ย ย ย ย ย ย ย }),

ย ย ย ย ย ย ย ย ย ย ย ย PropertyPaneTextField('test',ย {

ย ย ย ย ย ย ย ย ย ย ย ย ย ย label:ย 'Multi-lineย Textย Field',

ย ย ย ย ย ย ย ย ย ย ย ย ย ย multiline:ย true

ย ย ย ย ย ย ย ย ย ย ย ย }),

ย ย ย ย ย ย ย ย ย ย ย ย PropertyPaneCheckbox('test1',ย {

ย ย ย ย ย ย ย ย ย ย ย ย ย ย text:ย 'Checkbox'

ย ย ย ย ย ย ย ย ย ย ย ย }),

ย ย ย ย ย ย ย ย ย ย ย ย PropertyPaneDropdown('test2',ย {

ย ย ย ย ย ย ย ย ย ย ย ย ย ย label:ย 'Dropdown',

ย ย ย ย ย ย ย ย ย ย ย ย ย ย options:ย [

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย {ย key:ย '1',ย text:ย 'One'ย },

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย {ย key:ย '2',ย text:ย 'Two'ย },

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย {ย key:ย '3',ย text:ย 'Three'ย },

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย {ย key:ย '4',ย text:ย 'Four'ย }

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ]}),

ย ย ย ย ย ย ย ย ย ย ย ย ย ย PropertyPaneToggle('test3',ย {

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย label:ย 'Toggle',

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย onText:ย 'On',

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย offText:ย 'Off'

 ย ย ย ย ย ย ย ย ย ย ย })ย ย ย ย ย ย ย ย ย ย ย ย 

ย ย ย ย ย ย ย ย ย ย ]

ย ย ย ย ย ย ย ย ย ย }

ย ย ย ย ย ย ย ย ]

ย ย ย ย ย ย }

ย ย ย ย ]

ย ย };

}

All the below property functions have been defined in โ€œindex-internal.d.tsโ€ file.

  • PropertyPaneTextField
  • PropertyPaneCheckbox
  • PropertyPaneDropdown
  • PropertyPaneToggle

So if we go the definition of all the above each property function we will land into the below : PropertyPaneTextField

exportย declareย functionย PropertyPaneTextField(targetProperty:ย string,ย properties:ย IPropertyPaneTextFieldProps):ย IPropertyPaneField<IPropertyPaneTextFieldProps>;
PropertyPaneCheckbox
exportย declareย functionย PropertyPaneCheckbox(targetProperty:ย string,ย properties:ย IPropertyPaneCheckboxProps):ย IPropertyPaneField<IPropertyPaneCheckboxProps>;
PropertyPaneDropdown
exportย declareย functionย PropertyPaneDropdown(targetProperty:ย string,ย properties:ย IPropertyPaneDropdownProps):ย IPropertyPaneField<IPropertyPaneDropdownProps>;
PropertyPaneToggle
exportย declareย functionย PropertyPaneToggle(targetProperty:ย string,ย properties:ย IPropertyPaneToggleProps):ย IPropertyPaneField<IPropertyPaneToggleProps>;
index-internal.d.ts โ€“ This file is automatically created which looks like below:
ย 
index-internal.d.ts - SPFx Property Pane
index-internal.d.ts โ€“ SPFx Property Pane
ย 
HelloWorldWebPart.manifest.json
ย 
Now letโ€™s go to the HelloWorldWebPart.manifest.json file. Down to this file, we can seeย 
preconfiguredEntries JSON โ€“ here we can set all default value to property pane controls.
ย 
ย 
"preconfiguredEntries":ย [{

ย ย ย ย "groupId":ย "5c03119e-3074-46fd-976b-c60198311f70",ย //ย Other

ย ย ย ย "group":ย {ย "default":ย "Other"ย },

ย ย ย ย "title":ย {ย "default":ย "HelloWorld"ย },

ย ย ย ย "description":ย {ย "default":ย "HelloWorldย description"ย },

ย ย ย ย "officeFabricIconFontName":ย "Page",

ย ย ย ย "properties":ย {

ย ย ย ย ย ย "description":ย "HelloWorld",

ย ย ย ย ย ย "test":ย "Multi-lineย textย field",

ย ย ย ย ย ย "test1":ย true,

ย ย ย ย ย ย "test2":ย "2",

ย ย ย ย ย ย "test3":ย true

ย ย ย ย }

ย ย }]
As we have set the default value for the above-highlighted properties, at run time we can see the below default value in the web part property pane:
ย 
The description text box has HelloWorld
ย 
Multi-line text Field has aย Multi-line text field
ย 
Checkbox by default is selected (true).
ย 
The dropdown has default two
ย 
Toggle by default is on (true).
ย 
ย 
ย 
Add custom property in SPFx web part - configuration pane
Add a custom property in the SPFx web part โ€“ configuration pane
Note:
ย 
In the above example, for the dropdown in the JSON, it is set as 2 but while rendering the web part it displays as โ€œTwoโ€œ, you may ask why?
ย 
This is because of the key-value parameter mapping in the PropertyPaneDropdown function which is located at the HelloWorldWebPart.ts file where โ€œ2โ€ is mapped with the โ€œTwoโ€ text.
ย 
ย PropertyPaneDropdown('test2',ย {

ย ย ย ย ย ย ย ย ย ย ย ย ย ย label:ย 'Dropdown',

ย ย ย ย ย ย ย ย ย ย ย ย ย ย options:ย [

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย {ย key:ย '1',ย text:ย 'One'ย },

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย {ย key:ย '2',ย text:ย 'Two'ย },

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย {ย key:ย '3',ย text:ย 'Three'ย },

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย ย {ย key:ย '4',ย text:ย 'Four'ย }

ย ย ย ย ย ย ย ย ย ย ย ย ย ย ]}),
ย 
HelloWorldWebPart.manifest.json file in the actual project(solution):
ย 
HelloWorldWebPart.manifest.json file SPFx Property Pane Configuration
HelloWorldWebPart.manifest.json file SPFx Property Pane Configuration
ย 

Custom property pane SPFx (SPFx web part property pane) โ€“ What field types are supported in the web part property pane?

The following field types are supported in the web part property pane configuration:
ย 
  1. Button
  2. Checkbox
  3. Choice group
  4. Dropdown
  5. Horizontal rule
  6. Label
  7. Link
  8. Slider
  9. Textbox
  10. Multi-line Textbox
  11. Toggle
  12. Custom

Download: Create custom property in SharePoint Framework โ€“ SPFx web part pane

The above project can be downloaded from here.

Summary: Create custom property in SharePoint Framework โ€“ SPFx web part pane

Thus, in this article โ€“ we have learned the below with respect to adding the custom property in the SPFx web part property pane:
ย 
  • How do we create a custom property pane field?
  • What is the SharePoint framework custom property pane?
  • What field types are supported in the web part property pane?

See Also: SharePoint Online tutorial

You may also like the below SharePoint Online tutorials:

Download SharePoint Online PDF Book

Download SharePoint Online & Office 365 Administration eBook

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:



Buy SharePoint Online & Office 365 Administration eBook


ย 

Get the free demo PDF eBook from here:

FREE DOWNLOAD

Send download link to:

Subscribe to get exclusive content and recommendations every month. You can unsubscribe anytime.

ย 
ย 
ย 
ย 

About Post Author


Discover more from Global SharePoint

Subscribe to get the latest posts sent to your email.

3 comments on โ€œIn 2 steps instantly create custom property in SharePoint Framework โ€“ SPFx web part paneโ€

Do you have a better solution or question on this topic? Please leave a comment