As I said in the earlier article, the script to upload the Display Templates is as follows. I hope it does not require much explanations.
##================================================================================================
## Description : To do the below two items.
#1. Upload the Display Templates into Site Collection
## Author : Sathish Nadarajan
## Date : 06-Oct-2015
##================================================================================================
##============================================ Setup Input Paths ===========================================================
cls
$Host.UI.RawUI.WindowTitle = "-- Upload the Display Templates into Site Collection --"
$StartDate = Get-Date
Write-Host -ForegroundColor White "------------------------------------"
Write-Host -ForegroundColor White "| Upload the Display Templates into Site Collection |"
Write-Host -ForegroundColor White "| Started on: $StartDate |"
Write-Host -ForegroundColor White "------------------------------------"
$LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
$LogFile = ".UploadDisplayTemplates-$LogTime.rtf"
#start-transcript $logfile
$scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
Set-Location $scriptBase
$ErrorActionPreference = "Stop"
function AddPowerShellSnapin()
{
try
{
Write-Host "Adding PowerShell Snap-in" -ForegroundColor Green
# Try to get the PowerShell Snappin. If not, then adding the PowerShell snappin on the Catch Block
Get-PSSnapin "Microsoft.SharePoint.PowerShell"
}
catch
{
if($Error[0].Exception.Message.Contains("No Windows PowerShell snap-ins matching the pattern 'Microsoft.SharePoint.PowerShell' were found"))
{
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
}
Write-Host "Finished Adding PowerShell Snap-in" -ForegroundColor Green
}
function UploadDisplayTemplates([string]$siteUrl)
{
$spsite = Get-SPSite $siteUrl
$web = $spsite.RootWeb
$contentWebPartsDisplayTemplatesFolder = ($web).GetFolder("Content Web Parts")
$displayTemplatesDirectory = $scriptBase + "DisplayTemplates"
$web.AllowUnsafeUpdates=$true;
#For upload all files in document library from file system
foreach ($file in Get-ChildItem $displayTemplatesDirectory)
{
try
{
$stream = [IO.File]::OpenRead($file.fullname)
$destUrl = $web.Url + "/_catalogs/masterpage/Display Templates/Content Web Parts/" + $file.Name;
$displayTemplateFile = $web.GetFile($destUrl)
if($displayTemplateFile.CheckOutStatus -ne "None")
{
$contentWebPartsDisplayTemplatesFolder.files.Add($destUrl,$stream,$true)
$stream.close()
$displayTemplateFile.CheckOut();
$displayTemplateFile.CheckIn("CheckIn by PowerShell");
$displayTemplateFile.Publish("Publish by PowerShell");
$displayTemplateFile.Update();
$web.Update();
Write-Host $file.Name " Display Template Page uploaded on $web site" -ForegroundColor Green
}
else
{
$displayTemplateFile.CheckOut();
try
{
$contentWebPartsDisplayTemplatesFolder.Files.Add($destUrl,$stream,$true)
}
catch
{
Write-Host $_
}
$stream.close()
$displayTemplateFile.CheckIn("CheckIn by PowerShell");
$displayTemplateFile.Publish("Publish by PowerShell");
$displayTemplateFile.Update();
$web.Update();
Write-Host $file.Name " Display Template Page uploaded on $web site" -ForegroundColor Green
}
}
catch
{
Write-Host $_
}
}
$web.AllowUnsafeUpdates = $false;
$web.dispose();
$spsite.dispose();
}
try
{
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint.Administration")
$ConfigXmlPath = $scriptBase + "Configuration.xml"
Write-Host "Read the Config Values" -ForegroundColor Green
[Xml]$Config = Get-Content $ConfigXmlPath
AddPowerShellSnapin
UploadDisplayTemplates $Config.Configuration.PublishingSite.URL # SiteCollection URL
}
catch
{
Write-Host "Custom Exception Happened on Main : " + $Error[0].Exception.Message -ForegroundColor Red
}
#stop-transcript
This PowerShell script will Add the File for the first time, and from, the second time onwards, it will check out and check in the files. Hence, we will not lose our versioning as well.
Happy Coding,
Sathish Nadarajan.
Leave a comment