Subscribe: SharePoint SharePoint SharePoint
Showing posts with label Get Web Part Properties for a page. Show all posts
Showing posts with label Get Web Part Properties for a page. Show all posts

Monday, February 3, 2014

Updating Web part properties using PowerShell in SharePoint

Web part properties define the structure as well as functionality of a web part. They are quite useful while configuring a web part. We have a huge list of web part properties which have been defined by microsoft. you can have a look over these properties from the link below:

SharePoint Web part properties

Now once i ran into a situation wherein i had to change the "ChromeType" property of a web part in the landing page of all the subsites as well as root site in my site collection. There were a total of 32 subsites. So in order to do this through User Interface would have required a considerable amount of time.
SharePoint 2010

So i created a powershell script to do so and voila! I was able to do so in a single shot.

Each web part page is associated with a "SPWebPartManager" class. This class in particular contains references to all the web parts on the page. So in order to access my web part i have to iterate through the "SPWebPartManager" class.

Here the web part name is "Relevant Articles" and the chrome property has to be set to "TitleAndBorder".
  
 //Get reference to the SPSite object
 $SPsite = Get-SPSite "http://mywebapp/sites/mySite"  
   
 //Iterate through all sub sites and root site  
 foreach($SPWeb in $SPsite.AllWebs){  
   
 //Get reference to the landing page. Since it is a publishing site so default.aspx is the landing page  
 $page = $SPWeb.GetFile("Pages/default.aspx")  
   
 //Checkout the page  
 $page.CheckOut()  
   
 //Get reference to the webpartmanager class  
 $webpartmanager = $SPWeb.GetLimitedWebPartManager("Pages/default.aspx",   
 [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)    
   
   
 //Iterate through webparts in webpartmanager class  
  for($i=0;$i -lt $webpartmanager.WebParts.Count;$i++)  {   
   
 //Check for the name of required web part   
  if($webpartmanager.WebParts[$i].title -eq "Relevant Articles")   
  {    
   
     //Get reference to the web part  
     $wp=$webpartmanager.WebParts[$i];  
       
     //Set the chrome property  
     $wp.ChromeType="TitleAndBorder";  
   
     //Save changes to webpartmanager. This step is necessary. Otherwise changes won't be reflected  
     $webpartmanager.SaveChanges($wp);  
       
     break;   
   
  }   
 }   
   
  //Check in and Publish the page  
  $page.CheckIn("Relevant Articles")  
  $page.Publish("Relevant Articles")  
   
  // Update the SPWeb object  
  $SPWeb.Update();   
    
  //Dispose SPWeb object  
  $SPWeb.Dispose();  
 }