Tuesday, August 20, 2013

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
$WebURL="http://sharepoint.Crescent.com"

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

#$WebPartManager.WebParts
$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
$WebPartManager.SaveChanges($Webpart)
$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)
          {
            $OldTitle=$webPart.title
            #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
                  $webPartManager.SaveChanges($webPart)
                
                  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.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...