Monday, November 11, 2013

Install Display Templates using PowerShell (or copy files using WebDAV in SharePoint)

I’m in the process of writing a Search Center configuration script for SharePoint in PowerShell, and in the process I wanted to copy my display templates as well.

I could have used the SharePoint API to upload the files into the _catalogs/masterpage/Display Templates folder, but this felt like too much work.

My next thought was to copy the files using WebDAV. I started my search on the interwebs and found some posts around PowerShell and WebDAV, but nothing really clear and simple. Then I had an epiphany, maybe I can just copy the files? And you can!!

$dest = '\\intranet.contoso.com@80\_catalogs\masterpage\Display Templates\'
Copy-Item LocalPath -Destination $dest -Recursive

It’s a matter of formatting the URL in a server UNC notation and it just works.

What this doesn’t do is making sure the item copied is approved and published as a major version in SharePoint. To do this you either have to use the SharePoint API from PowerShell, or for example navigate to https://intranet.contoso.com/search/_layouts/DesignDisplayTemplates.aspx and filter on Approval Status = Draft, and publish each one.

Personally I change the settings on the _catalogs/masterpage library to not require approval or check-in/check-out, and only have major versions. If doing this you have to make sure you have control over the library as any saved change will go live right away – this means using a test environment or test site collection for the search page first is highly recommended.