How to Set or Update Webpart Property Using Powershell Script in SharePoint


In our project we have the situation that we need to update the property of a webpart. Since our site is in production, we couldn't recreate the site. The webpart is on nearly every welcome page and many pages are created, so we could not manually change the property. So I decided to write the Powershell script.

The following is the procedure to update the webpart property using powershell commands.

  1. Get the site.
      1. $site = Get-SPSite "SiteCollectionURL"  
  2. Since we must do this throughout the site collection, we will go through all the webs.

    $webcoll = $site.AllWebs.
      1. #Step through each web in site collection  
      2. foreach ($web in $webcoll) {  
  3. Now, we need to read the welcome page of each web.
    1. #Get the root folder  
    2. $rootFolder = $web.RootFolder  
    3. #Get the welcome page  
    4. $welcomepage = $ rootFolder.WelcomePage  
  4. Once we have the welcome page, get the file and check it out.
    1. $file = $web.GetFile("$welcomepage")  
    2. $file.CheckOut()  
  5. Now from welcome page we will get the webpart manager.

    #more details for SPWeb.GetLimitedWebPartManager.
    1. $wpm = $web.GetLimitedWebPartManager("$welcomepage", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)  
  6. Now we have webpartmanager from the page, we can traverse through all webparts and check for the webpart for which we need to set/update the property.
    1. foreach($webpart in $wpm.WebParts){  
    2.    if($webpart.MyUniqueProperty){  
    3.       if(!$webpart. MyUniqueProperty){  
    4.          $webpart. MyUniqueProperty = $ MyUniquePropertyValue(Value which need to be Set/Update)  
    5.          #More details for WebPartManager.SaveChanges()  
    6.          $wpm.SaveChanges($webpart)  
    7.       }  
    8.    }  
    9. }  
  7. Check in and Publish the file - #more details – SPFile.CheckIn().
    1. $ file.CheckIn("Updated/Set property of webpart",1)  
    2. $ file.Publish("Updated/Set property of webpart")  
  8. Finally dispose of the site object.
    1. $site.Dispose()  

The following is the complete Powershell script:

  1. $site = Get-SPSite "SiteCollectionURL"  
  2. $webcoll = $site.AllWebs  
  3.  #Step through each web in site collection  
  4. foreach ($web in $webcoll) {  
  5.    #Get the root folder  
  6.    $folder = $web.RootFolder  
  7.    #Get the welcome page  
  8.    $welcomepage = $folder.WelcomePage  
  9.    $file = $web.GetFile("$welcomepage")  
  10.    $file.CheckOut()  
  11.    $wpm = $web.GetLimitedWebPartManager("$welcomepage", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)  
  12.    foreach($webpart in $wpm.WebParts){  
  13.       if($webpart.MyUniqueProperty){  
  14.          if(!$webpart. MyUniqueProperty){  
  15.             $webpart. MyUniqueProperty = $ MyUniquePropertyValue(Value which need to be Set/Update)  
  16.             $wpm.SaveChanges($webpart)  
  17.          }#First If  
  18.       }#Second If  
  19.    }#Foreach  
  20.    $file.CheckIn("Updated/Set property of webpart",1)  
  21.    $file.Publish("Updated/Set property of webpart")  
  22.    $site.Dispose()   
  23. }#foreach  
I hope this will help you. Thanks. Feel free to discuss this.