Subscribe: SharePoint SharePoint SharePoint

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();  
 }  

4 comments:

  1. The following command is returning error.
    $page.Publish("Relevant Articles")

    ReplyDelete
    Replies
    1. Hi it will be more helpful if you post the error message you are getting.

      The only error which I think May be because you might not have permissions to publish the page. Maybe you can only checkin a draft version and not a major version. The publish command will try to publish the major version which will throw error if adequate permissions are not set .

      Let me know if it helps
      Cheers
      Shubhdeep

      Delete
  2. đồng tâm
    game mu
    cho thuê nhà trọ
    cho thuê phòng trọ
    nhac san cuc manh
    số điện thoại tư vấn pháp luật miễn phí
    văn phòng luật
    tổng đài tư vấn pháp luật
    dịch vụ thành lập công ty trọn gói
    nước cờ trên bàn thương lượng
    mbp
    erg
    nghịch lý
    chi square test
    nghệ thuật nói chuyện
    coase
    thuyết kỳ vọng
    chiến thắng con quỷ trong bạn
    cân bằng nash

    Triệu Quốc Đống đứng lên đi ra cạnh cửa sổ rồi nói:
    - Hả, anh nói Liễu ca đã về, đang ngồi cùng anh? Các anh đang ở đâu? Hỉ Lai Đăng? Thái ca, đây là điểm bình thường các anh tiêu sài sao? Ha ha, cẩn thận Ủy ban kỷ luật nhìn chằm chằm đó. Đương nhiên Liễu ca về thì phải chiêu đãi tốt.

    - Em về bây giờ ư? Bây giờ về cũng mất hơn hai tiếng. Cái gì? Liễu ca có chuyện muốn nói với em? Em nhất định sẽ lên. Như vậy đi, em sắp ăn xong rồi, em sẽ về Giang Khẩu. Có khách trên huyện tới Xã Lĩnh Đông, em nhờ xe bọn họ về Giang Khẩu là được. Nếu không anh bảo Tiểu Tống tới cửa Ủy ban huyện chờ em chút, có lẽ hai bên cùng tới một lúc đó. Vâng, cứ như vậy đi.

    Triệu Quốc Đống bỏ máy xuống và thầm than mình tới Xã Lĩnh Đông liền ít liên lạc với bạn bè ngay. Bình thường hay tụ tập với đám đồng nghiệp Ban quản lý và Công an huyện, nhưng đến Xã Lĩnh Đông đúng là không tiện. Chẳng qua Liễu Đạo Nguyên vừa về, hơn nữa có việc cần nói với hắn, nghe giọng Thái Chánh Dương khá nghiêm túc, chẳng lẽ là?

    Triệu Quốc Đống nhếch miệng cười mình. Lần trước Liễu Đạo Nguyên bảo hắn tới Tân Châu, hứa hai năm sẽ thành Trưởng phòng,, hắn lại từ chối, bây giờ đã hơn năm, hắn vẫn đang ở chức Phó Trưởng phòng mà thôi.

    - Bí thư Triệu, phải về huyện thành ư?
    Hoàng Trung Kiệt nghe được vài câu cuối cùng của Triệu Quốc Đống.

    - Ừ, đi nhờ xe Trưởng phòng Hoàng có được không?

    - Không sao, ngồi được mà.
    Hoàng Trung Kiệt đứng lên lấy tăm rồi nói:
    - Gà núi đúng là ngon. Bí thư Triệu đúng là có phúc ăn, có thể cho tôi mang về hai con không, Trưởng phòng tôi cũng thích món này.

    - Không vấn đề gì, anh còn cần nói sao? Chẳng qua anh đây là kể khổ tôi đó. Ở đây cả ngày ăn gà, tôi sắp thành con gà rồi.
    Triệu Quốc Đống cười cười nói lại một câu.

    ReplyDelete
  3. I modified the script to update a specific webpart: CQWP on a specific page (/sitePages/Home.aspx).

    BUT when I open the page, the change (FilterDisplayValue1) is not shown.

    If I edit the webpart on the page, the updated value is displayed.
    If I Apply the change, the change shows on the page.

    Any Help!

    ReplyDelete