Friday, June 24, 2016

Sharepoint 2013 Restoring Master Page

Master pages are sensitive. If you happen to mess up with one you can get into a mess. I recently made a change to the master page and uploaded to realize that it had an error. The problem is i was no longer able to access my website.

The solution was to use powershell to upload a copy of the master file with the error fixed and a different file name, as changes to the master file in use are not allowed (as it cannot be deleted).

Step 1: Upload a master file with a new name. (note that the master file being used cannot be deleted)
WebURL = "http://XXX:100"
$SourcePath ="C:\temp\afz_seattle.master"

#Get the Web
$web = Get-SPWeb $WebURL
#Get the Target folder - Master page Gallery
$MasterPageList = $web.GetFolder("Master Page Gallery")
#Set the Target file for Master page
$TargetPath = $Web.Url + "/_catalogs/masterpage/afz_seattle.master"
#Get the Master page from local disk
$MasterPageFile = (Get-ChildItem $SourcePath).OpenRead()
 #upload master page using powershell
 $MasterPage = $MasterPageList.Files.Add($TargetPath,$MasterPageFile,$false)
 $web.Update()        

Step 2: Once the new master file is uploaded with a different name change the templates to point to this file (Instead of the one with error).
$web = Get-SPWeb http://XXX:100
$web.CustomMasterUrl = "/_catalogs/masterpage/afz_seattle.master"
$web.MasterUrl = "/_catalogs/masterpage/afz_seattle.master"
$web.Update()

No comments: