Developers have started use of SharePoint framework for developing the client side web parts in SharePoint online(Office 365). Some of the code block is getting repeated across multiple client side web parts like selecting the list, date time selection, color picker, term set picker. Developers have noticed this to the PNP community. Hence Microsoft came up with reusable controls which are similar to our ASP.net concept of user controls. This controls can be added to the web part easily and can be used as custom property in property pane. I didn't find all steps in single blog so decided to summaries here starting from creating the client side web parts.
There are around 6 controls currently available.
we will use PropertyFieldColorPicker control. Import the following module to your web part file.
Step 3 : Install the following dependency to your project
Make sure that node.js command prompt is running and it is referring to your project. Execute the below command.
npm install @pnp/spfx-property-controls --save --save-exact
Step 4 : Update the configuration file of your project
Go to Config folder, under that open config.json file and add the following line to the localizedresource property.
"PropertyControlStrings": "./node_modules/@pnp/spfx-property-controls/lib/loc/{locale}.js"
Step 5 : Adding control to your web part"localizedResources": {"CustomucwebpartWebPartStrings": "lib/webparts/customucwebpart/loc/{locale}.js","PropertyControlStrings": "./node_modules/@pnp/spfx-property-controls/lib/loc/{locale}.js"}
There are around 6 controls currently available.
- PropertyFieldColorPicker
- PropertyFieldDateTimePicker
- PropertyFieldListPicker
- PropertyFieldPeoplePicker
- PropertyFieldSpinButton
- PropertyFieldTermPicker
import {
PropertyFieldColorPicker,
PropertyFieldColorPickerStyle
} from '@pnp/spfx-property-controls/lib/PropertyFieldColorPicker';
Add the new property in web part name it color of type string
export interface ICustomucwebpartWebPartProps {
description: string;
color: string;
}
Add the custom property control to the group fields of the web part property configuration.
PropertyFieldColorPicker('color', {
label: 'Color',
selectedColor: this.properties.color,
onPropertyChange: this.onPropertyPaneFieldChanged,
properties: this.properties,
disabled: false,
alphaSliderHidden: false,
style: PropertyFieldColorPickerStyle.Full,
iconName: 'Precipitation',
key: 'colorFieldId'
})
<p class="${ styles.description }">${escape(this.properties.color)}</p>
Now run the gulp serve to test property locally.Add the web part on workbench page then click on edit and in property pane you will see the color property with color picker option available. You can select the color and same value will be displayed in the web part body.
Enjoy coding !!!
Reference:
Hey Piyush, Its really great information. Thank you for sharing it.
ReplyDeleteThis post is very simple to read and appreciate without leaving any details out. Great work! best places to invest in property
ReplyDeleteYou might comment on the order system of the blog. You should chat it's splendid. Your blog audit would swell up your visitors. I was very pleased to find this site.I wanted to thank you for this great read!! areas to invest in property uk
ReplyDeleteThis particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform! Sobha windsor reviews
ReplyDeleteI have a hard time describing my thoughts on content, but I really felt I should here. Your article is really great. I like the way you wrote this information. Gibraltar estate agency
ReplyDeleteI am definitely enjoying your website. You definitely have some great insight and great stories. palm hills compound
ReplyDeletevery interesting post.this is my first time visit here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post! https://property-developer-cambodia.blogspot.com/2021/09/must-read-before-making-best-property.html
ReplyDeleteIm no expert, but I believe you just made an excellent point. You certainly fully understand what youre speaking about, and I can truly get behind that. check this out
ReplyDeleteThis particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform! Marcia Backstrom
ReplyDelete