0

I am able to set the masterpage to all existing sites in site collection as well as the newly created sites/site collections using this link.

But, I have a question and want to know if this is feasible.

  1. Create VS project

  2. Create module and put in the masterpage with elements.xml.

  3. Do not create a feature activated for this masterpage feature. Set the scope to site collection.

  4. Create an event receiver (web provisioned) that has the code to set the masterpage to all sites in the webapplication. (for each site in each site collection in each web application)

  5. Create another feature scoped at webapplication level with the above event receiver.

Is this a better solution compared to the solution (feature stapling) given in the link?

6
  • How will you activate the feature of step 3 in each site collection? Why would Web provisioned event in step 4 loop through all site and site collections. Shouldn't it simply set its own master page. Finally don't understand the need for step 5 Mar 28, 2014 at 6:28
  • From step 2 we have the masterpage file in a location on the SharePoint (say path is: X). Then, Step 4 will then loop through all webs in sitecolection for each web app to update their masterpage. Step 5 is needed because the Step 4 will happen only if the feature is activated.
    – variable
    Mar 28, 2014 at 6:31
  • Are you aware that Since an event receiver has to be registered with the site collection before it's handlers can execute, the WebAdding and WebProvisioned handlers cannot run when a site collection and its root web site are created. Moreover, it is run for every sub site. Mar 28, 2014 at 6:40
  • registered in the web.config? is that the reason to use staplers..?
    – variable
    Mar 28, 2014 at 6:56
  • where did I mention web.config? Mar 28, 2014 at 7:05

1 Answer 1

1

Since you want to apply master page to all sites within a web application and a web application can have several site collections and each site collection can have many sub sites. So, here is how I will go about it.

  1. Create a site collection feature which uploads master page to library.
  2. Create a web scoped feature (staplee feature). This feature will check if the current site is root site collection. If yes it will activate feature created in step 1 so that master page is available in gallery. Then it will apply master page to the site whether current site is root or sub site.
  3. Create a web application stapler feature. Associate feature created in step 2 to your site definition.

This way master page will be applied to site whether root or not.

Web Provisioned event will make more sense in case mater page needs to be applied to sub sites of a site collection. That means applying master page to sub sites in a site collection will be automated. However, you will still have to find a way to apply it to root site. The automation is lacking in getting a master page uploaded to library and applying to toot web. After that it is present though. Compare it to feature stapling where everything is completely automated.

2

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.