SP2010: Using a Site Definition with a SPWebProvisioningProvider
Today I was working on a SharePoint site which uses a custom Site Definition. Ok, maybe you’ll think “shouldn’t you be using a web template instead? ’cause site definitions suck when you need to update”. You’re right. And therefore we’re only using stapled features to deploy content, and we could as well use a web template which we probably will in the future. Hey, but that’s not the point here. Let’s assume you’re using a Site Def.
Ok, so I wanted to use the SPWebProvisioningProvider class to run some code when a new site was being provisioned off my site def. It’s pretty simple and well covered, follow there steps:
- Create a class which inherits from SPWebProvisioningProvider and implement the Provision method.
- Use props.Web.ApplyWebTemplate to apply the template to your web
- Run your custom code
- Hookup the class as a provisioning provider in your Configuration element
I’m not going to spend too much time on that, just google it if you need more info.
But after taking the above steps, I noticed weird behaviour. SharePoint was calling my custom code just fine, but when it hit the ApplyWebTemplate step, it then again called the same Provision code recursively. Well, not much rocket science there; this resulted in an overflow exception and the template not being applied at all.
So how should you fix this when you encounter such problems? The anwers lies in the Configuration element you created. You need to create two of those. One will be set to hidden. This is the one you actually apply to your web when you call ApplyWebTemplates. Apart from setting it hidden, also do not link the provisioning code to this one, since that’s what causing the recursive loop.
The visible Configuration is the one you’ll user will choose to provision the site. You don’t need to include features or lists in this configuration, since it won’t actually be applied (that’s the invisible one). The visible one only serves as a way to execute the provisioning code and doesn’t do much more then that.
Little less intuitive? Yeah, though so too.
Leave a Comment