Add a webpart in SharePoint 2013 using Powershell

Powershell has become one of the most valuable tools for any prominent SharePoint administrator or developer. In order to truly master the new version of SharePoint, SharePoint 2013, you need to be familiar with Powershell.

There are countless situations where you need to add webparts to pages but the situation or number of pages prevents you from using the interface and doing it manually. Ranging from quickly setting up several similar pages to provisioning a new instance of a company portal. Powershell can help you and make your day a bit simpler.

When I plan to add a webpart to a page using Powershell or code, like a feature receiver, I always start out on a temporary page within my portal. I add and customize the webpart using the web interface. I make sure the webpart looks exactly like the webpart I would like to add through Powershell or code. When I’m satisfied with the looks and appearance of the webpart I export the file to a .webpart (or .dwp) file. The exported .webpart file is a perfect starting point for adding copies of the webpart to new publish pages. Using the interface to customize the webpart and then exporting it, almost like a template, save me a lot of time compared to adding and configuring the webpart via code from scratch.

In the example below I will add a Summary Links webpart to a new publishing page. Starting off by customizing the webpart in the interface and then exporting it.

The following Powershell script adds a new page to a publishing web and add a webpart in one of the webpart zones. The concept is quite familiar compared to add the webpart using a feature receiver or similar.

$snapin=Get-PSSnapin|Where-Object{$_.Name -eq'Microsoft.SharePoint.Powershell'}if($snapin-eq$null){Add-PSSnapin"Microsoft.SharePoint.Powershell"}$SiteUrl="http://portal.devserver.local/"$WebUrl="http://portal.devserver.local/testweb"$PageLayoutRelUrl="/_catalogs/masterpage/WelcomeSplash.aspx"$PageName="MyPage.aspx"$PageTitle="My Page"$WebPartFileName="SummaryLinks.webpart"$WebPartZoneID="TopZone"$WebPartZoneIndex=1$Site= Get-SPSite($SiteUrl)$PubSite=New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)$Web= Get-SPWeb $WebUrl$PubWeb=[Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)$Layouts=$PubSite.GetPageLayouts($False)$PageLayout=$Layouts[$PageLayoutRelUrl]# Create a new publishing page.$Page=$PubWeb.AddPublishingPage($PageName,$PageLayout)$Page.Title =$PageTitle$Page.Update();
# Get the limited webpart manager.$WPMgr=$Web.GetLimitedWebPartManager("Pages/$PageName",[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)# Import the xml file of the webpart and add it to the page.$Dir=resolve-path .\
$WebPartFilePath=join-path-path$Dir-childpath$WebParthFileName$WebPartXml=get-content$WebPartFilePath$SR=New-Object System.IO.StringReader($WebPartXml.OuterXml)$XTR=New-Object System.Xml.XmlTextReader($SR)$Err=$null$WP=$wpMgr.ImportWebPart($XTR,[ref]$Err)$WPMgr.AddWebPart($WP,$WebPartZoneID,$WebPartZoneIndex)# Check in and publish the page.$Page.CheckIn("")$Page.ListItem.File.Publish("")