Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Update Web Part Properties Programmatically in SharePoint

$
0
0
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: 
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.

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>