Introduction

In this blog, I have discussed how to edit and delete a web part of a SharePoint page using PNP PowerShell. Follow the below instructions to edit and delete a web part.
  • Add a webpart
  • Edit a webpart property
  • Remove a webpart
  • Run PowerShell script

Add a webpart

Follow the below code to add a webpart to a wiki page.
  1. $siteUrl = Read-Host "Enter site url " ;
  2. $url = Read-Host "Enter page url like 'SitePages/team-page.aspx' " ;
  3. try
  4. {
  5. #Connect to sharepoint site
  6. Connect-PNPOnline -Url $siteUrl -Credentials (Get-Credential)
  7. #Add webpart to a Shareoint page
  8. $WebXml ="
  9. <WebPart
  10. xmlns='http://schemas.microsoft.com/WebPart/v2'
  11. xmlns:iwp='http://schemas.microsoft.com/WebPart/v2/ContentEditor'>
  12. <Title>Webpart Title</Title>
  13. <FrameType>None</FrameType>
  14. <Description>Allows authors to enter rich text content.</Description>
  15. <IsIncluded>true</IsIncluded>
  16. <ZoneID>QuickLinks</ZoneID>
  17. <PartOrder>0</PartOrder>
  18. <FrameState>Normal</FrameState>
  19. <Height />
  20. <Width />
  21. <AllowRemove>true</AllowRemove>
  22. <AllowZoneChange>true</AllowZoneChange>
  23. <AllowMinimize>true</AllowMinimize>
  24. <AllowConnect>true</AllowConnect>
  25. <AllowEdit>true</AllowEdit>
  26. <AllowHide>true</AllowHide>
  27. <IsVisible>true</IsVisible>
  28. <DetailLink />
  29. <HelpLink />
  30. <HelpMode>Modeless</HelpMode>
  31. <Dir>Default</Dir>
  32. <PartImageSmall />
  33. <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
  34. <PartImageLarge>/_layouts/15/images/mscontl.gif</PartImageLarge>
  35. <IsIncludedFilter />
  36. <Assembly>Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
  37. <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
  38. <Content
  39. xmlns='http://schemas.microsoft.com/WebPart/v2/ContentEditor'>
  40. <div>Enter your Content here..........</div>
  41. </Content>
  42. <PartStorage
  43. xmlns='http://schemas.microsoft.com/WebPart/v2/ContentEditor' />
  44. </WebPart>"
  45. Add-PNPWebPartToWikiPage -ServerRelativePageUrl $url -Xml $WebXml -Row 1 -Column 1
  46. }
  47. catch{}
Also, you can add the webpart by its path:
Add-PNPWebPartToWikiPage-ServerRelativePageUrl$url-Path"C:\addwebpart.webpart"-Row1-Column1

Edit a Webpart Property

Follow the below code to edit an existing webpart.
  1. $siteUrl = Read - Host "Enter site url "
  2. $url = Read - Host "Enter page url like 'SitePages/team-page.aspx' ";
  3. try {
  4. #Connect to sharepoint site
  5. Connect - PNPOnline - Url $siteUrl - Credentials(Get - Credential)
  6. #Set property of a wikipage webpart
  7. $webPart = Get - PNPWebPart - ServerRelativePageUrl $url
  8. $webPartID = $webPart.Id
  9. Set - PNPWebPartProperty - ServerRelativePageUrl $url - Identity $webPartID - Key Title - Value "TodayList"
  10. } catch {}
You can also set other properties of a webpart, eg:
Set-PNPWebPartProperty-ServerRelativePageUrl$url-Identity$webPartID-KeyHeight-Value500

Remove a webpart

For removing a webpart. you have to follow the below code.
  1. $siteUrl = Read - Host "Enter site url "
  2. $url = Read - Host "Enter page url like 'SitePages/team-page.aspx' ";
  3. try {
  4. #Connect to sharepoint site
  5. Connect - PNPOnline - Url $siteUrl - Credentials(Get - Credential)
  6. $webPart = Get - PNPWebPart - ServerRelativePageUrl $url
  7. $id = $webPart.Id
  8. #Remove or delete a webpart
  9. Remove - PNPWebPart - ServerRelativePageUrl $url - Identity $id
  10. } catch {}
You can also delete a webpart by its title
Remove-PNPWebPart-ServerRelativePageUrl$url-Title"mywebpart"
Run the Script
  1. Right-click on the PowerShell script which we have made.
  2. Then click on "Run with PowerShell".
  3. Enter your SharePoint site Url in the script. Assign the value to $siteUrl.
  4. Enter the page server relative Url like “/SitePages/MyFirstPage.aspx”.
  5. Then give your SharePoint username and password.
Now you can check the output inside the created page.
Image 1 - Created the Webpart
Add, Update And Delete Webpart Of A SharePoint Page Using PNP PowerShell
Image 2 - Changed the Title from “team list” to “TodayList”
Before running the script:
Add, Update And Delete Webpart Of A SharePoint Page Using PNP PowerShell
After running the script:
Add, Update And Delete Webpart Of A SharePoint Page Using PNP PowerShell

Conclusion

From the above code, we can conclude that the CRUD operation in SharePoint using PowerShell script is very effective, as it reduces the amount of time needed to do the Operation. As we can see, the code size is less as well, so performance will be higher. This will be the most convenient method to do the above operation.