Update XSL links in .webpart file
#input parameters
#$siteUrl="https://localhost/sites/events"
$destination="c:\temp"
#$fileName="NewsShortOverview.webpart"
$siteUrl=$targetUrl.substring(0, $targeturl.length-1) #got from Settings.ps1 and '/' removal
$libraryFolder="_catalogs/wp"
$libraryUrl=$siteUrl + "/" + $libraryFolder
function DownloadWPfile
{
param([string]$siteUrl,
[string]$fileName,
[string]$destination)
try
{
$web=get-spweb $siteUrl
$folder=$web.GetFolder("_catalogs/wp")
$files=$folder.Files
$file=$files[$fileName]
#Ensure destination directory
$destinationfolder = $destination # + "/" + $folder.Url
if (!(Test-Path -path $destinationfolder))
{
$dest = New-Item $destinationfolder -type directory
}
#check in only when files are checked out, when it's publishing site
write-host "Downloading " $file.Name "..."
#Download file
$binary = $file.OpenBinary()
$stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
$writer = New-Object System.IO.BinaryWriter($stream)
$writer.write($binary)
$writer.Close()
write-host "Downloading completed."
}
catch [Exception]
{
write-error $_
}
}
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
}
function UploadFile
{
param([string]$srcPath,
[string]$fileName,
[string]$libraryUrl,
[string]$libraryFolder)
try
{
$web=get-spweb $siteUrl
$folder=$web.GetFolder($libraryFolder)
$file=get-childitem ($srcPath + "\" + $fileName)
$files=$folder.Files
write-host "Uploading " $fileName "..."
$files.Add(($libraryUrl+"/" + $fileName), $file.OpenRead(), $true)
#check in only when files are checked out, when it's publishing page
if($files[$fileName].checkOutStatus -ne "None")
{
$files[$fileName].checkIn("checked in by Powershell")
$files[$fileName].publish("published by Powershell")
start-sleep -s 5
$files[$fileName].Item.File.Approve("Approved by Powershell")
}
write-host "Upload finished."
}
catch [Exception]
{
write-error $_
}
finally
{
if($web -eq $null)
{
$web.dispose()
}
}
}
DownloadWPfile -siteUrl $siteUrl -fileName $fileName -destination $destination
FixXslLink -srcPath $destination -fileName $fileName -siteUrl $siteUrl
UploadFile -srcPath $destination -fileName $fileName -libraryUrl $libraryUrl -libraryFolder $libraryFolder