Category Archives: XSL

Setting the XSL Link in web part properties using PowerShell

webpartpropertiesTo change the XSL link value in the web part properties of a web part, it usually requires editing the page and entering the path to the XSL file in the web part properties (see image on right).

This can be automated using PowerShell. In the example below, the PowerShell loops through each web part on the page and sets the XSL Link value to “_layouts/xsl/tony.xsl”.

$spweb = Get-SPWeb “http://www.site.com”;
$url = $spweb.Url;
$WebPageUrl = “/Pages/test.aspx”
$spWpManager = $spweb.GetLimitedWebPartManager($WebPageUrl, [System.Web.UI.WebControls.WebParts
.PersonalizationScope]::Shared);
foreach($spwebpart in $spWpManager.Webparts)
{
$spwebpart.xsllink=”_layouts/xsl/tony.xsl”; #set the web part property
$spWpManager.SaveChanges($spwebpart);
}

The custom XSL style sheet in my previous post can be added to a large amount of lists that already exist by modifying this code and looping through all the sites/site collections. The code can also be modified to select web parts based on their title rather than changing the setting on every web part on the page.