Add,Update & Delete Webpart of a SharePoint Page Using PNP PowerShell
Softree Technology > Blogs > PowerShell > Add,Update & Delete Webpart of a SharePoint Page Using PNP PowerShell

Add,Update & Delete Webpart of a SharePoint Page Using PNP PowerShell

  • Posted by: Softree Technology

In this blog, I have discussed about how to edit and delete a webpart of a SharePoint page using PNP PowerShell. Follow the below instructions to edit and delete a webpart.

• Add a webpart
• Edit 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” -Row 1 -Column 1

Edit 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 -Key Height -Value 500 

Remove webpart :

For removing a webpart you have to follow below codes.

$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 :

  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

Image-2: Changed the Title from “team list” to “TodayList”

Before running script,

After running 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 that the code size is less as well so performance will be higher. So this will be the convenient method to do the above operation.

Keywords :

  1. Wikipage’s webpart CRUD operation using PowerShell PNP.
  2. Add,Update & Delete webpart of a SharePoint page using PNP PowerShell.
  3. Update webpart of wikipage using PNP PowerShell.
Author: Softree Technology

Leave a Reply