2

This works for existing site collection and any new sites under it and their subsites BUT when I create a new site collection, well i can't even create a new site collection now as it gives me file not found error as the root site of my new sitecollection is unable to find the masterpage...WHY?? I mean how??

        using (SPWeb currentWeb = (SPWeb)properties.Feature.Parent)
          {
             using (SPSite currentSite = currentWeb.Site)
                 {
                 //Set Masterpage
                 Uri masterURI = new Uri(currentSite.Url +   "/_catalogs/masterpage/V4Copy.master");
                 currentWeb.CustomMasterUrl = masterURI.AbsolutePath;
                 currentWeb.MasterUrl = masterURI.AbsolutePath;
                 currentWeb.Update();
                 }
         }

3 Answers 3

1

I believe you have an issue that could be related to your managed path. Basically due to the site collection (usually those in root) you would get 2 backslashes, but as well from the fact to you should probably rely on ServerRelativeUrl of your SIte Collection Root Web, e.g. SPContext.Current.Site.RootWeb.Url to construct the proper Url for each of your sub-sites. Simply check the following:

  1. Is it a root web without a managed path (/) and if so, instead of using ServerRelativeUrl return Empty to avoid the creation of the double-backslash as you have it currently already pointed.
  2. For sub-sites I also use a WebProvisioned handler to get whatever settings the RootWeb has and force the same master pages (custom+ default) on each sub-site. The WebProvisioned does not function for the TopLevel site, and also unless you considered it already, do not force a custom master page on Search Sites, they are not compatible by default (check Web template).

As another recommendation: Use feature properties to store the actual name of your master page so you could adapt that without recompilation of your feature, simply update of xml files.

0

If you use SharePoint 2010 Server, there are publishing feature exists. If I'm not wrong wen you activate web scoped feature on a new created web, the root web master page is inherited automatically. Also you have possibility to customize this settings through UI.

0

From the code it seems you are using a copy of V4 master page that is v4Copy.master. So you must have this master page already uploaded in the master page gallery. You may need to write another feature which will first upload this new master page to master page gallery and then call this feature which will apply the master page to the sites. Also, a good thing to do will be to first check whether the master page exists or not using this statement. if (rootWeb.GetFile(rootWeb.Url + "/_catalogs/masterpage/v4Copy.master").Exists) { } For more information please see this: http://sharepointnadeem.blogspot.in/2012/08/change-mater-page-of-sharepoint-site-on.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.