Is it possible to programmatically set the master page for a site based on the custom template used to create said site?

I created some custom templates with custom page layout and webparts, they are called News, Projects and Products. I saved them as site templates, and now, when i create a new site using those templates i want to set a different master page to each of them.

I've created a EventReveiver with a WebProvisioned method that gets the webTemplateId and sets the masterpage,l the thing is, the webtemplateid return the base template, which is STS#0.

Is there a way to identify the Custom Template I used to create my site and apply the master page accordingly?

3 Answers
3

I think one way to do it (first way that comes to mind) is a custom Provisioning Provider for each template type (you specify this in the WebTemp...xml file) - you'll need one class per web template to set the masterpage after the ApplyWebTemplate() method call. I don't think there's a way to pass the Provisioning Provider any parameters (though if you could, that'd be neat and you'd just need one class).

Have you considered using feature stapling? Feature stapling is designed to activate a feature when a new site is created. Whether it activates can be setup to determine the site template id of the new site. In your case you would simply make sure to activate the stapling when you are creating a site of the templates you require. Once you have this you can simply add your masterpage to your feature and then use a feature event receiver to set the new site's masterpage to your custom one.