Add site url to XslLinks

19/07/2012 22:59

 

#input parameters
$siteUrl="http://localhost/sites/events"
$srcPath="c:\temp"
$fileName="BannerWebpart.webpart"
 
function GetSiteFromUrl(
    [string]$url
)
{
$first=$url.IndexOf('/')
$short=$url.substring($first+2,$url.length-$first-2)
$index=$short.IndexOf('/')
$site=$short.substring($index+1, $short.length-$index-1)
return $site
}
 
function FixXslLink
{
    param([string]$srcPath,
            [string]$fileName,
            [string]$siteUrl)
            
$file=[xml](get-content ($srcPath+"\"+$fileName))
 
#Get nodes with Xsl Link settings (not error if node doesn't exist)
$main=$file.webparts.webpart.data.properties.property|where {$_.name -match "MainXslLink"}
$header=$file.webparts.webpart.data.properties.property|where {$_.name -match "HeaderXslLink"}
$item=$file.webparts.webpart.data.properties.property|where {$_.name -match "ItemXslLink"}
 
$links=@($main, $header, $item)
 
$prefix=GetSiteFromUrl -url $siteUrl
$prefix="/"+ $prefix
$prefix
 
foreach($link in $links)
{
 
$text=$link."#text"
#write-host "Vynatok z linku:" $text.substring(0, $prefix.length)
 
if($text -ne $null) #if link is defined
{
 
    #Check if site url is already added to links, if not, add it
    if($text.substring(0, $prefix.length) -ne $prefix)
    {
        $text=$prefix + $text
        $text
        write-host "Changing XslLink..."
        $link.set_InnerText($text)
    }
    else
    {
        write-host "XslLink is already changed."
    }
 }
 
}
write-host "Saving file..."
$file.Save($srcPath+"\"+$fileName)
write-host "File saved to: " $srcPath
}
 
FixXslLink -srcPath $srcPath -fileName $fileName -siteUrl $siteUrl