Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

I love these puzzles :)

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).

As always, there's plenty of examples on the web for doing this, and your magic search term is 'sharepoint custom provisioning provider'. Here's an example post: http://blogs.technet.com/b/apurdon/archive/2008/02/05/the-mystery-that-is-spwebprovisioningprovider.aspx - This example sets Audit Settings on the site collection, so you're wanting the exact same level of access at the same time, but to set the MasterPage on site collection creation.

share|improve this answer
add comment

Just to follow on from James' answer.

You could create a "singleton" list (a list with one item) in the site template.

That list an have configuration information such as master page name.

Your web provisioning provider could read this list item, do what it needs to then deletes the list from the site.

Mark.

share|improve this answer
add comment

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.

Here is an example of someone walking through the steps required:

http://www.olavaukan.com/tag/feature-stapling/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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