CRUD Operation On SPFx Webpart Using PnP PowerShell
Introduction
With the introduction to SharePoint Framework (SPFx), creating webparts using frontend technologies has been very easy. But what if we want to maintain it and create some automation for adding, updating, or deleting this SPFx webparts? In this scenario, PnP PowerShell helps us with all the CRUD operations for SPFx webparts. So let us check how we can utilize this.
Installation of PnP PowerShell
Installing PnP PowerShell is very easy. There are two methods.
If you are using Windows 10 then running the below command in PowerShell will install the PnP PowerShell module
|
|
Install the executable by downloading it from the below link.
Add SPFx Webpart in Site Page
Before Running the below commands we need to first connect sitecollection. To do so we can use the below command:
|
|
For adding SPFx webpart there are two options,
- We add out of the box webpart
- We add custom created webpart
For Adding Out of the box webpart we can run the below command
|
|
Note
In the –DefaultWebPartType we can use any of the out of the box webpart names.
For Adding Custom SPFx Webpart we can use the below command,
|
|
Available Parameters
- Page - This parameter accepts a string and we can provide the name of the page where we want to add the webpart
- Section - This parameter accepts integer values which indicate in which section we want to add the SPFx webpart
- Column - This parameter also accepts integer values and indicate in which column we want to add SPFx webpart
- Component - To add custom SPFx webpart we need to use this parameter. We can either provide GUID or the name of the component
- DefaultWebPartType - To add Out of the box webpart we can specify the name of the webpart in this parameter
- Order - This parameter accepts integer values and indicates the order of the webpart
- WebPartProperties - To specify the property of the webpart we can use this field it can be used both in Custom SPFx webpart as well Out of the box webparts
Update SPFx Webpart in Site Page
To update the SPFx webpart we want to update the webpart properties. This can be easily achieved using the below command:
|
|
Available Parameters
- Identity - This parameter accepts the ID of the webpart. This can accept title or instance id of the webpart.
- Page - This parameter accepts a string and we can provide the name of the page where we want to update the webpart.
- PropertiesJson - To update the property values we can provide the JSON. In this parameter it’s displayed in the command above.
- Title - To update the internal title of the webpart
Note
This parameter will not update the title which is displayed for the webpart.
Fetch All SPFx Webpart in Site Page
To fetch all the webparts present in the Site Page we can use the below command. This would enumerate all the client-side webparts.
|
|
Available Parameters
- InstanceId - This parameter accepts the ID of the webpart. This can accept title or instance id of the webpart.
- Page - This parameter accepts a string and we can provide the name of the page where we want to get the webpart.
Delete SPFx Webpart in Site Page
To remove any specific instance of the webpart in the site page we can use the below command which would delete the instance from the page.
|
|
Available Parameters
- Force - If we want to skip the confirmation for deleting the webpart we can use this parameter
- InstanceId - This parameter accepts the ID of the webpart. This can accept title or instance id of the webpart
- Page - This parameter accepts a string and we can provide the name of the page where we want to delete the webpart.
Conclusion
From the above commands, we can conclude that for automating the process adding, creating, updating or deleting the webpart becomes easy using the PnP PowerShell command and it becomes very easy to maintain the Site Pages using these commands. We can even leverage this to automate the modern page creation by adding webparts as per our need using above command.