SharePoint Pals
 | Sign In
How to Add WebPart to the Publishing Page using PowerShell in SharePoint 2013
by Sathish Nadarajan 11 Dec 2013
SharePoint MVP
Today  :   16     Total  :    19551

As part of the previous article, the next step would be adding a WebPart to the Page which we created. In the same manner, the script is going to be self-explanatory and straight forward. The explanations were in lined.


 # Add PowerShell Snapin
 $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
 if ($snapin -eq $null) 
     Add-PSSnapin "Microsoft.SharePoint.Powershell" 
 # Get the Site URL
 $SiteUrl = "https://MySiteCollectionURL/"
 # Get the Web URL
 $WebUrl = "https://MyWebSiteURL"
 # Get the Page on which WE are going to Add the WebPart
 $PageName = "Test.aspx"
 # The location of the WEbPart Definition File
 $localWebpartPath = "C:\Windows\System32\MyWebPart.webpart"
 # Error Message which is required as a Reference Parameter while Importing the WEbPart
 $errorMsg = "Test Error Message"
 # Initializing the SPSite Object
 $Site = Get-SPSite($SiteUrl)
 # Get an instance for Publishing Site based on SPSite
 $PubSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)
 # Get the SPWEb Object
 $Web = Get-SPWeb $WebUrl
 # Get the Publishing Web Based on the SPWeb Object
 $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
 # The below commented line is to get all the Pages
 # Get only our Page
 $PublishingPage = $PubWeb.GetPublishingPage("https://MyWebURL/Pages/Test.aspx");
 # Make the Web as AllowUnSafeUpdates as true
 $Web.AllowUnsafeUpdates = $true
 # Checkout the Publishing Page
 # Get the LimitedWEbPartManager
 $limitedWebPartManager = $PublishingPage.ListItem.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
 # Initialize the XmlReaderSettings Object which is required for the XmlReader Object
 $xmlReaderSettings = New-Object System.Xml.XmlReaderSettings
 # Create the XmlReader Object by using the WebPart Definition file and the ReaderSettings Object
 $xmlReader = [System.Xml.XmlReader]::Create($localWebpartPath,$xmlReaderSettings);
 #Add Web Part to catalogs folder and Get the WebPart Definition Object based on the webpart definition xml file     
 $oWebPartDefinition = $limitedWebPartManager.ImportWebPart($xmlReader,[ref]$errorMsg); 
 # Add the WebPart to the WebPartManager by specifing the Zone and the Index.
 # Checkin the Publishing Page
 $PublishingPage.CheckIn("Test Checkin by Sathish");
 # Publish the Page
 $PublishingPage.ListItem.File.Publish("Test Publish By Sathish");
 # Revert the AllowUnsafeUpdates to False once we are done.
 $Web.AllowUnsafeUpdates = $false
 # I was trying to Approve the Page.  But, if the Approve is enabled on the Pages Library level, 
 # then only we can do that.  Otherwise we cannot.  But the Syntax is correct as below.
 # $PageListItem.File.Approve("Page approved automatically by PowerShell script")  


Hope the above scripts will be executing without any problem. If you face any issue, please let me know. Thanks.

Happy Coding.

Sathish Nadarajan.

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter October 23, 22:21
How to Add/Remove User Custom Actions (in Site Actions Menu) Programmatically using CSOM PNP in SharePoint -

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation