Tuesday, June 25, 2013

SharePoint 2010/2013: System.IO.FileNotFoundException on setting master page

If you are trying to set a master page to your newly create or imported site through code and getting the following error
System.IO.FileNotFoundException: The file{path}.master/ does not exist.  
at Microsoft.SharePoint.ApplicationRuntime

then try this in your code: [some sites suggested to use {subsite}.ServerRelativeUrl but it won’t work for the sitecollections under multi-level managed paths, so it is safer to use the sitecollection object].

newSubWeb.AllowUnsafeUpdates = true;
newSubWeb.CustomMasterUrl = newSubWeb.Site.ServerRelativeUrl + "/_catalogs/masterpage/{mymaster}.master";
newSubWeb.MasterUrl = newSubWeb.Site.ServerRelativeUrl + "/_catalogs/masterpage/{mymaster}.master";
powershell script for any further troubleshooting:
$web = Get-SPWeb "http://{subsitepath}"
$web.MasterUrl = "/{sitecollectionpath}/_catalogs/masterpage/{masterpage.master}/"
$web.CustomMasterUrl = "/{sitecollectionpath}/_catalogs/masterpage/{masterpage.master}/"
hope it would save someone else’s time.