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.
- $siteUrl = Read-Host "Enter site url " ;
- $url = Read-Host "Enter page url like 'SitePages/team-page.aspx' " ;
- try
- {
- #Connect to sharepoint site
- Connect-PNPOnline -Url $siteUrl -Credentials (Get-Credential)
- #Add webpart to a Shareoint page
- $WebXml ="
- <WebPart
- xmlns='http://schemas.microsoft.com/WebPart/v2'
- xmlns:iwp='http://schemas.microsoft.com/WebPart/v2/ContentEditor'>
- <Title>Webpart Title</Title>
- <FrameType>None</FrameType>
- <Description>Allows authors to enter rich text content.</Description>
- <IsIncluded>true</IsIncluded>
- <ZoneID>QuickLinks</ZoneID>
- <PartOrder>0</PartOrder>
- <FrameState>Normal</FrameState>
- <Height />
- <Width />
- <AllowRemove>true</AllowRemove>
- <AllowZoneChange>true</AllowZoneChange>
- <AllowMinimize>true</AllowMinimize>
- <AllowConnect>true</AllowConnect>
- <AllowEdit>true</AllowEdit>
- <AllowHide>true</AllowHide>
- <IsVisible>true</IsVisible>
- <DetailLink />
- <HelpLink />
- <HelpMode>Modeless</HelpMode>
- <Dir>Default</Dir>
- <PartImageSmall />
- <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
- <PartImageLarge>/_layouts/15/images/mscontl.gif</PartImageLarge>
- <IsIncludedFilter />
- <Assembly>Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
- <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
- <Content
- xmlns='http://schemas.microsoft.com/WebPart/v2/ContentEditor'>
- <div>Enter your Content here..........</div>
- </Content>
- <PartStorage
- xmlns='http://schemas.microsoft.com/WebPart/v2/ContentEditor' />
- </WebPart>"
-
- Add-PNPWebPartToWikiPage -ServerRelativePageUrl $url -Xml $WebXml -Row 1 -Column 1
- }
- 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.
- $siteUrl = Read - Host "Enter site url "
- $url = Read - Host "Enter page url like 'SitePages/team-page.aspx' ";
- try {
- #Connect to sharepoint site
- Connect - PNPOnline - Url $siteUrl - Credentials(Get - Credential)
- #Set property of a wikipage webpart
- $webPart = Get - PNPWebPart - ServerRelativePageUrl $url
- $webPartID = $webPart.Id
- Set - PNPWebPartProperty - ServerRelativePageUrl $url - Identity $webPartID - Key Title - Value "TodayList"
- } 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.
- $siteUrl = Read - Host "Enter site url "
- $url = Read - Host "Enter page url like 'SitePages/team-page.aspx' ";
- try {
- #Connect to sharepoint site
- Connect - PNPOnline - Url $siteUrl - Credentials(Get - Credential)
- $webPart = Get - PNPWebPart - ServerRelativePageUrl $url
- $id = $webPart.Id
- #Remove or delete a webpart
- Remove - PNPWebPart - ServerRelativePageUrl $url - Identity $id
- } catch {}
You can also delete a webpart by its title
Remove-PNPWebPart-ServerRelativePageUrl$url-Title"mywebpart"
Run the Script
- Right-click on the PowerShell script which we have made.
- Then click on "Run with PowerShell".
- Enter your SharePoint site Url in the script. Assign the value to $siteUrl.
- Enter the page server relative Url like “/SitePages/MyFirstPage.aspx”.
- Then give your SharePoint username and password.
Now you can check the output inside the created page.
Image 1 - Created the Webpart
Image 2 - Changed the Title from “team list” to “TodayList”
Before running the script:
After running the script:
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.