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.




Monday, 5 September 2011

Using localized strings in XSLT of Summary Link webpart



Hello All,

Discussion Topic:
In my project we have requirement like, we have to display a localized string in Summary Link webpart. String like “See all”, since we have multilingual site we will require localized version of this string. So we decided to get string “See all” string from the resource file in XSLT.
So here we are going to discuss how to get the value from resource files in Summary Link webpart XSLTs.

Description:
SummaryLink webpart is derived from DataFormWebPart webpart as  
                Microsoft.SharePoint.WebPartPages.DataFormWebPart
                  Microsoft.SharePoint.Publishing.WebControls.CmsDataFormWebPart
                    Microsoft.SharePoint.Publishing.WebControls.SummaryLinkWebPart (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.webcontrols.summarylinkwebpart.aspx )

There is <ParameterBindings> property of DataFormWebPart webpart.  This property makes the resource strings available in XSLTs.

Syntax for <ParameterBindings> property in SummaryLinkWebPart definition file is as
<property name="ParameterBindings" type="string" >

&lt;ParameterBinding Name="testparameter" Location="Resource(resourcefilename,resourcestringwhosevalueneedtomakeavailable)" /&gt;

</property>

In above syntax only we need to mention the <ParameterBindings> property in SummaryLinkWebPart definition file.

Now here,
1.       Location specifies the Resource function. SharePoint foundation uses Resource function to get the value of specified resource strings. You will get more details here.
2.       Name is used in XSLT as parameter.
Now once you specified, in XSLT you will get this value as a parameter. I.e. in SummaryLinkMain.xsl file you will get the value from resource file by define it as follows

<xsl:param name=" testparameter" />

Here parameter name is same as specified in webpart definition file, i.e testparameter. Once I define this parameter in XSLT then I can use this value in XSLT as

<xsl:value-of select="$testparameter "/>


Also we can pass this value in ItemStyle.xslt also by using <xsl:with-param> element as

<xsl:apply-templates select="." mode="itemstyle">
       <xsl:with-param name="teste1234" select="$tes123t"></xsl:with-param>
</xsl:apply-templates>


These XSLTs are always having fun to work J Enjoy!