SharePoint: Adding WebPart on Pages Using PowerShell Script

In this article you will learn how to add a WebPart on pages using a PowerShell Script in SharePoint.

One of the requirements of our project was to replace the existing webpart from our production site that was causing many problems with performance. This web part was available in multiple content pages. So one of the options available to us was to write a PowerShell script to add a new web part to multiple content pages.

The following describes how to add a webpart to a SharePoint page using a PowerShell script.

  1. To add the web part we will require a web part definition file to be available on the file system so that we can add it to the webpart gallery, import it and add it using the WebPartManager class.

  2. Now, consider we need to add the webpart to the MyWPPage.aspx page from the page library of the web MyWeb.

  3. We need to get a publishing web instance from web instance and set the allow unsafe updates to true as follows:
    1. $webURL = “http://MyWeb.com”  
    2. $web = Get-SPWeb $webURL  
    Get the publishing web instance and set the allow unsafe update property to true.
    1. [Microsoft.SharePoint.Publishing.PublishingWeb]$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web);  
    2. $allowunsafeupdates = $web.AllowUnsafeUpdates  
    3. $web.AllowUnsafeUpdates = $true  
  4. Get the pages library and find the list item as follows:
    1. $list = $web.Lists[$pagesLibrary]  
    2. foreach($listItem in $list.Items){  
    3. if($listitem.URL.Contains("MyWPPage.aspx ") {  
    4.     $myListItem = $listItem  
    5.     break;    
    6. }  
  5. Get the file from listitem, check that if this page is checked out to another user or the same user and if the same user then publish and then check out the page.
    1. $page = $web.GetFile($listitem.URL)  
    2. if ($page.CheckOutStatus -ne "None")    {  
    3. #Check to ensure the page is checked out by same user, and if so, check it in  
    4.     if ($page.CheckedOutBy.UserLogin -eq $web.CurrentUser.UserLogin)  
    5.     {  
    6.         $page.CheckIn("Page checked in automatically by PowerShell script")  
    7.         Write-Output $page.Title"("$page.Name") has been checked in"  
    8.     }  
    9. }  
    10. if ($page.CheckOutStatus -eq "None"){  
    11. $page.CheckOut()  
    12. #Get the webpart manager  
    13. $webpartmanager = $web.GetLimitedWebPartManager($page.URL, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)  
    14. }  
  6. Now from the local file system, get the file stream and import it using the webpart manager as follows:
    1. $saveFolder = "C:\webpart\"  
    2. $webpartfile = “MyWebPart.webpart”  
    3. $fileDWP = $saveFolder + $webpartfile  
    4. write-output "DWP File"  $fileDWP  
    5.  
    6. #Getting the webpart gallery  
    7. [Microsoft.SharePoint.SPList]$wpList = $web.Site.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::WebPartCatalog)  
    8. $fileStream = ([System.IO.FileInfo](Get-Item $fileDWP)).OpenRead()  
    9. [Microsoft.SharePoint.SPFolder]$wpFolder = $wpList.RootFolder  
    10. [Microsoft.SharePoint.SPFile]$wpFile = $wpFolder.Files.Add($webpartfile, $fileStream, $true)  
    11. Write-host $wpFile  
    12.  [System.Xml.XmlReader]$xmlReader = [System.Xml.XmlReader]::Create($wpFile.OpenBinaryStream())  
    13. #Import the webpart  
    14. $myCustomWP = $webpartmanager.ImportWebPart($xmlReader,[ref]$errorMsg)  
    15. Write-Output "My custom WebPart" $myCustomWP.title  
    16. #If this webpart is not available on page then add it  
    17. if(! $webpartmanager.WebParts.Contains($myCustomWP)){  
    18.    $webpartmanager.AddWebPart($myCustomWP, $wpZoneId, $wpOrder)  
    19. }  
  7. Finally, Check in the page and clean up the instances.
    1. $page.CheckIn("Page checked in automatically by PowerShell script")  
    2. $page.Publish("Page published,new webpart added")  
    3. $web.AllowUnsafeUpdates = $allowunsafeupdates  
    4. $xmlReader.Close()  
    5. $pubWeb.Close()  
    6. $web.Dispose()  
Thanks!