SharePoint MasterPage | File Not Found Error

Creating SharePoint features is cool because you can create a solution installable package with masterpages, webparts, page layouts and many others within one and only feature, installed with a simply click, available to any site, and capable of being activated and deactivated at any time.

But this dreamy capability brings also some issues.

One I've found is MasterPage applying.

If the process isn't strongly checked, you're sure to come up with
"File not found" when activating the feature
(God only knows how this error is enlightening)

So you SharePoint feature developers out there, keep in mind:




There are 3 Site possibilities your "apply" line should check:

- SharePoint Web Application
(Main or Root Site Collection)

- SharePoint Secondary Site Collection
(Any Site Collection within the root Site Collection)

- SharePoint Subsite Website
(Created by "Site Actions" -> "Create Site" Option)

Your feature dll should check the site type BEFORE changing the master page or you will most certainly get this error on at least one of this types of sites.


//Web Applications relative url end with bars,
//such as "http://localhost/"
if(web.ServerRelativeUrl.EndsWith(@"/"))
{
SPFilespmasterfile1=web.GetFile(web.ServerRelativeUrl+"_catalogs/masterpage/Puure.master");
    if(spmasterfile1.Exists)
    {
        web.MasterUrl=web.ServerRelativeUrl+"_catalogs/masterpage/Puure.master";
        web.CustomMasterUrl=web.ServerRelativeUrl+"_catalogs/masterpage/Puure.master";
        web.Update();
    }
}
else
//Site Collections relative url end with no bars,
//such as "http://localhost/sites/testersite/"
    {
SPFilespmasterfile2=web.GetFile(web.ServerRelativeUrl+"/_catalogs/masterpage/Puure.master");
        if(spmasterfile2.Exists)
        {
            web.MasterUrl=web.ServerRelativeUrl+"/_catalogs/masterpage/Puure.master";
            web.CustomMasterUrl=web.ServerRelativeUrl+"/_catalogs/masterpage/Puure.master";
            web.Update();
        }
    }

Comments

Popular posts from this blog

Breaking down document locking in SharePoint

Working around X-Frame-Options for iframes

Document ID not being generated