top of page

Add webpart to page Using CSOM With PowerShell on SharePoint

In this article, you will learn how to add web part to a publishing page using CSOM with PowerShell on SharePoint 2013 / SharePoint 2016 / SharePoint online.

Steps Involved:

The following section explains the flow for adding web part to a publishing page.

  • Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll and Microsoft.SharePoint.Client.Publishing.dll.

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"

  • Initialize client context object with the site URL.

$siteURL = ""

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  

  • If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context. 

# Not required for on premise site - Start

$userId = ""

$pwd = Read-Host -Prompt "Enter password" -AsSecureString  

$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  

$ctx.credentials = $creds   

# Not required for on premise site - End

  • If you are trying to access the SharePoint on premise site, then the credentials parameter is not required to be set to the context. But you need to run the code on the respective SharePoint server.

  • Get the page from the respective library using server relative url. Load the file and execute the query to access the page components.

# page where web part to be changed

$pageName = "TestPage1.aspx"

# Get the page to add $file = $ctx.Web.GetFileByServerRelativeUrl("/Pages/TestPage1.aspx")  



  • Check if web part already exists.

  1. From the page, we will get the web parts present.From the file object, using web part manager and personalization scope we can access the page web parts. 

  2. Load and execute the query to access the web parts. 

  3. Load the individual web parts by loading the web part property

  4. Then compare and check all the web part titles with new web part using for each loop.

# Get all the webparts Write-Host "Retrieving webparts" $wpManager = $file.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared)  

$webparts = $wpManager.Webparts  



$webparts | ForEach-Object 




# New web part name

$newwpTitle = "Content Editor"

# Web part exists flag

$wpExists = $false if($webparts.Count -gt 0)


Write-Host "Looping through all webparts"     

foreach($webpart in $webparts)


if($webpart.WebPart.Title -eq $newwpTitle)


Write-Host "Webpart present"             

$wpExists = $true         

}       }  }  



# your further code goes here.


  • If web part not present, then we will add the web part. Check out the file using below logic.

  1. Check the file is checked out by some one.

  2. If so, undo check out.If its not checked out, then check out the file using check out type "none" and check out method.

# Check if page already checked out. If so, undo check out

if($file.CheckOutType -eq [Microsoft.SharePoint.Client.CheckOutType]::Online)




Write-Host "Undo Checkout"         







write-host "Error in checkout.. $($_.Exception.Message)" -foregroundcolor red      

}  }  

# If page is not checked out, then check out

if($file.CheckOutType -eq [Microsoft.SharePoint.Client.CheckOutType]::None)


Write-Host "Checkout"     





  • Then access the web part file. It will have extension of webpart or dwp. It is a xml file with necessary web part details like title, zone id, header, etc. The web part xml can be generated by exporting and modifying the existing web part files. Once generated, import the web part xml and add it to web part manager with necessary zone id and zone index.

Write-Host "Adding webpart"

$XmlDocument = Get-Content -Path "local drive path/NewWebpart.xml" $importWP = $wpManager.ImportWebPart($XmlDocument)  



  • Then check in and publish the page.

Write-Host "Page checkin" $file.CheckIn("Added webpart", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)  



I have added a content editor web part to the page. Like wise any other web part can be added to the page using the respective web part file. Here generating or using the correct web part file is important. Apart from that, there will not be any change in addition logic explained



bottom of page