Sunday, 25 December 2011

How to set/update webpart property using powershell script


Hello,
In our project we have situation, there we need to update the property of one webpart. Since our site is in production, we couldn’t recreate the site and this webpart is almost on every welcome page and lots of pages are created, we cannot manually change the property. So I decided to write the powershell script.
Following are the steps with powershell commands to update the webpart property
1.      Get the site
$site = Get-SPSite "SiteCollectionURL"
2.      Since we have to throughout the site collection, among all the web we will go through all the webs
$webcoll = $site.AllWebs
 #Step through each web in site collection
foreach ($web in $webcoll) {
3.       Now, we need to read welcome page of every web
#Get the root folder
$rootFolder = $web.RootFolder
#Get the welcome page
$welcomepage = $ rootFolder.WelcomePage
4.       Once we got the welcome page, get the file and checkout it
$file = $web.GetFile("$welcomepage")
$file.CheckOut()
5.       Now from welcome page we will get webpart manager
$wpm = $web.GetLimitedWebPartManager("$welcomepage", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
6.       Now we have webpartmanager from the page, we can traverse through all webparts and check for the webpart for which we need to set/update the property
foreach($webpart in $wpm.WebParts){
            if($webpart.MyUniqueProperty){
if(!$webpart. MyUniqueProperty){
$webpart. MyUniqueProperty = $ MyUniquePropertyValue(Value which need to be Set/Update)
#More details for WebPartManager.SaveChanges()
                                    $wpm.SaveChanges($webpart)
}
}
}
7.       Check in and Publish the file - #more details SPFile.CheckIn()
                                $ file.CheckIn("Updated/Set property of webpart",1)
                        $ file.Publish("Updated/Set property of webpart")
8.       Finally dispose the site object
                $site.Dispose()           
Below the complete powershell script
$site = Get-SPSite "SiteCollectionURL"
$webcoll = $site.AllWebs
 #Step through each web in site collection
foreach ($web in $webcoll) {
#Get the root folder
$folder = $web.RootFolder
#Get the welcome page
$welcomepage = $folder.WelcomePage
$file = $web.GetFile("$welcomepage")
$file.CheckOut()
$wpm = $web.GetLimitedWebPartManager("$welcomepage", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
foreach($webpart in $wpm.WebParts){
            if($webpart.MyUniqueProperty){
if(!$webpart. MyUniqueProperty){
$webpart. MyUniqueProperty = $ MyUniquePropertyValue(Value which need to be Set/Update)
                                    $wpm.SaveChanges($webpart)
}#First If
}#Second If
}#Foreach
$file.CheckIn("Updated/Set property of webpart",1)
            $file.Publish("Updated/Set property of webpart")
            $site.Dispose()           
}#foreach
I hope this will help you. Thanks. Feel free to discuss.





           


Monday, 12 December 2011

Setting / Updating available page layouts programatically



Hello All,

In our project, we had situation like we need set available page templates to the given web. Since we already deployed the solution on production environment and we are not going to create site again, for existing site we need to do manually and for new sites which will be created we used feature stapler.

So here we will discuss how to set available page layouts programmatically (We did this using custom feature receiver and associated our custom feature in feature stapler). 

Setting the available page layout programmatically is little difficult than setting in site definition in Onet.xml file.

To set the available page layouts there is a method “SetAvailablePageLayouts()” of publishing web. This method takes one parameter which is array of “PageLayout” class.

So to set the available page layouts programmatically:
  1. Get the PublishingWeb of the web where we need to set / update the available page layouts since SetAvailablePageLayouts() is method of PublishingWeb as
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(currentWeb);

  1. Get all the available page layouts for the web (Just in case if we want all the existing set page layouts) and created instance of ArrayList class to add the layouts.
PageLayout[] layouts = pWeb.GetAvailablePageLayouts();
     ArrayList pageLayoutList = new ArrayList();
pageLayoutList.AddRange(layouts);

  1. Now, since the we will required instance of “PageLayout” class, I get the all available page layouts for the site collection, loop through them and compare with my new page layout which I need to add to the web and added in array list as follows:
SPSite site = pWeb.Web.Site;
       if (site != null)
       {
           PublishingSite publishingSite = new PublishingSite(site);
       }
       if (publishingSite != null)
       {
PageLayoutCollection pageLayoutsCollection = publishingSite.GetPageLayouts(true);
        }

foreach (PageLayout pl in pageLayoutsCollection)
{
if (pl.Name.Equals(mynewpagelayoutnamewhcihneedtobemadeavailablefortheweb, StringComparison.InvariantCultureIgnoreCase))
              {
                pageLayoutList.Add(pl);
               }
        }

  1. Type cast the arraylist into array of PageLayout class and call the SetAvailablePageLayouts() of PublishingWeb
PageLayout[] newPls = (PageLayout[])pageLayoutList.ToArray(typeof(PageLayout));

pWeb.SetAvailablePageLayouts(newPls, false);

I hope this will be useful to you. In case if any queries, questions feel free to discuss.

Thanks J.