Update Web Part Properties using PowerShell in SharePoint

Ever wanted to update SharePoint web part properties programmatically? After a company acquisition, We had to change the company name from a custom web part's title, throughout the web application.

SharePoint update web part properties using PowerShell: 
$Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

#configuration parameters

#Get objects
$web = Get-SPWeb $WebURL
$page = $web.GetFile("SitePages/Home.aspx")
$WebPartManager = $web.GetLimitedWebPartManager($Page.ServerRelativeUrl,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$WebPartManager.WebParts | % { write-host $_.title}
#Get the Web part
$Webpart = $WebPartManager.WebParts | Where { $_.Title -eq "Shared Documents"}

#Update Webpart Property
$Webpart.Title = "Team Documents"

#Save and Publish
$page.CheckIn("updated Title Property") 

Update Web Part Properties in entire Web Application:
Say, we want to update all web part titles containing "Crescent Inc." to "Lunar Inc."
#Get All Webs (sites)
$webs = Get-SPWebApplication "http://sharepoint.Crescent.com" | Get-SPSite -Limit All | Get-SPWeb -Limit All
 #Iterate through webs
 foreach ($web in $webs)
  #Get All Pages from site's Root into $AllPages Array
  $AllPages = @($web.Files | Where-Object {$_.Name -match ".aspx"})
  #Search All Folders for Pages
  foreach ($folder in $web.Folders)
          #Add the pages to $AllPages Array
          $AllPages += @($folder.Files | Where-Object {$_.Name -match ".aspx"})

   #Iterate through all pages
   foreach($Page in $AllPages)
       #Web Part Manager to get all web parts from the page
        $webPartManager = $web.GetLimitedWebPartManager($Page.ServerRelativeUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

       #Iterate through each web part
       foreach($webPart in $WebPartManager.WebParts)
            #Get the Content Editor web part with specific Title
            if($webPart.title -like "*Crescent Inc*")
                  #Replace the Old Title
                  $webPart.title = $webPart.title.Replace("Crescent Inc.", "Lunar Inc.")
                  #Same method goes to update any other custom properties.
                  #E.g. To update Page viewer web part's link property:
                  #$webPart.ContentLink = "http://www.sharepointdiary.com" 
                  #To set built-it properties, E.g. To set Set the Chrome type programmatically use:
                  #$webPart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::TitleAndBorder
                  #Save the changes
                  write-host "Updated '$($OldTitle)' on $($web.URL)$($Page.ServerRelativeUrl)"
Set  SharePoint web part properties programmatically using C#
Above code is fairly simple and we can re-write it in C# object model also.

1 comment:

  1. whats does the script looks like, when updating the contact webpart with contoso\username to fabrikam\username . ?


Please Login and comment to get your questions answered!

Powered by Blogger.