Update/Delete WebParts On SharePoint Pages Using PnP PowerShell


Introduction

In this article, we will learn how to update the Web part properties and delete the Web parts on SharePoint pages, using PnP PowerShell. The Client Side Object Model is used internally for these operations.


The Web parts from any SharePoint Server page (Publishing, Wiki or Site Pages) can be updated or removed on the sites. In the references, given below, I have used the content editor Web part for my use cases. Similarly other Web parts or app parts can also be updated/deleted.


Prerequisite

You need to have PowerShell 3.0, available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or view more documentation from the official Website. The installers are available here. Online version installer is preferred for On Premise or Office 365 operations. You can also install all the three installers for testing (SharePoint 2013, 2016, online).


The PnP PowerShell is supported by SharePoint 2013, SharePoint 2016 On Premises and Office 365 versions. The following operations are tested on SharePoint 2013 and Office 365 environments.


Connect To Site

Connect to the site, using the snippet, given below. PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM).

  1. # Get Current Context Site (Root)

  2. $siteurl = "https://abc.sharepoint.com"

  3. Connect-SPOnline -Url 

  4. $siteurl  $ctx = Get-SPOContext  


Update Web Parts

The Web parts added to SharePoint pages can be updated, using PnP PowerShell commands. Set-SPOWebPartProperty command is used to update the properties. To update the properties, the following operation needs to be carried out.


The Web part to be updated needs to be retrieved from SharePoint page with Get-SPOWebPart command, using the page URL and Web part title (identity).


The Web part is updated with the following parameters: 

  • Server Relative URL of the page.

  • Identity – Web Part Id.

  • Key – Denotes the Web part property name.

  • Value – Denotes the Web part property value.


The code snippet, given below, shows how the Web part properties can be updated. In this example, the height of the Web part is changed.

  1. # Get Web Part ID

  2. $webpart = Get-SPOWebPart -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity "Test WP"

  3. $webpartId = $webpart .Id  

  4. # Update WebPart

  5. Set-SPOWebPartProperty -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity $webpartId -Key Height -Value 500

 

Delete Web Part

The Web parts can be deleted from SharePoint page, using Remove-SPOWebPart command. There are two ways of deleting a Web part.


Using Identity:

  • The Web part is first retrieved, using Get-SPOWebPart command along with the Server relative page URL and the identity (Web part title) parameters. 

  • The Web part can be deleted, using Remove-SPOWebPart command with the Server relative URL of the page and the identity (Web part Id). 

  • This approach is preferred, when you need to validate a Web part before deleting it.


The code, given below, helps in retrieving a Web part, followed by deleting it.

  1. $webpart = Get-SPOWebPart -ServerRelativePageUrl "/Pages/PnPPage.aspx"  Identity "Content Editor1"

  2. Remove-SPOWebPart -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Identity $webpart. Id  


Using Title

In this approach, the Web part is deleted directly, using the Remove-SPOWebPart command with the Server relative URL of page and the title parameters.


The code, given below, helps in deleting a Web part with the single command.

  1. Remove-SPOWebPart -ServerRelativePageUrl "/Pages/PnPPage.aspx" -Title "Test WP"


Summary

Thus, you have learned how to update the Web part properties and delete the Web parts from SharePoint pages on a SharePoint Website programmatically, using PnP PowerShell commands. The operations mentioned above, are supported for the Web parts on any custom or OOB page. PnP PowerShell is supported by SharePoint 2013, SharePoint 2016 On Premises and Office 365 versions. The operations, mentioned above, are tested on SharePoint 2013 and Office 365 environments.