Skip Ribbon Commands
Skip to main content
www.iwkid.com > Blog > Posts > Templates and SharePoint Sites - 4 of 4
October 24
Templates and SharePoint Sites - 4 of 4

Best Practice

We've covered several different approaches to creating templated SharePoint Sites, now I'll bring everything together and we can walk through what I would recommend as a best practice.

 

Goals

First let's talk through my goals.  You may find that our goals differ and, in that case, one of the approaches on my previous post would work fine in your scenario.

  1. Allow Site Administrators to create a site using the out of the box site creation screens
  2. Support Feature Stapling for future updates
  3. Create no dependencies on the SharePoint Farm:
    • The Site "Template" can be removed at any time without affecting previously created sites
    • Minimal impact projected during future platform upgrades

 

Almost There

At first glance we can get pretty close using the Feature Stapling approach outlined in my previous post.  The biggest barrier is the creation of a new Site Definition which we have identified as an unacceptable dependency.  If we want to use Feature Stapling, we need a unique Template Name to point to...  If we create a unique Template Name, we have a dependency...

 

The Keystone

The piece that makes it all possible:  SPWebProvisioningProvider.  A Provisioning Provider can be specified in a Site Definition's webtemp*.xml file.  In the configuration element, you can specify the ProvisionAssembly, ProvisionClass, and ProvisionData attributes.

Hold it right there, does that mean we're creating a Site Definition?  I thought they were the root of all evil?  Not so fast, my friend:

If you implement your own class that inherits from SPWebProvisioningProvider, you can use it to call the ApplyWebTemplate method of a Web which allows you to manually provision one of the out of the box Site Definitions!  That means you get your unique Site Template Name but when the site is created it THINKS it is an out of the box Site Definition!

As my buddy Tom used to say, NOW how much would you pay?  :-)

 

The Solution

  1. Create the webtemp*.xml (using the ProvisionAssembly and ProvisionClass attributes):

    webtemp
  2. Create the Provisioning Provider (which applies the "Blank Site" Site Definition):

    SPWebProvisioningProvider
  3. Create any Feature Receivers and Feature Staplers
  4. Wrap everything into a SharePoint Solution and you're done!

 

Post-Deployment you can make updates by updating your Feature Receiver or the code in your provisioning provider.  Also, if you're done making sites of that type, go ahead and uninstall your SharePoint Solution: Your sites were created using out of the box Site Definitions so there is no leftover dependency.

 

Conclusion

This series of posts covers a lot of information and skims over even more.  I intend for it to be an evolving series and will be adding code samples and follow-up posts.  For the latest, keep an eye on the series introduction or subscribe to this post's category.

Comments

great guidance

I love the path and twist, great post!

 on 10/26/2008 12:41 AM

example solution?

i'm a newbie to sharepoint development and into the trial by fire learning mode.  I've been tasked to develop some kind of solution that will simplify adding additional sub-sites to an existing site.  The sub-sites are essentially identical except for things like project numbers which have to trickle down to form templates and reports in the various lists.  Your post has me thinking about using your "best practice" suggestion.  Do you have a sample solution (with something for items 3 and 4) which will help me get started?  thanks
 on 12/2/2008 12:45 PM

Re: Templates and SharePoint Sites - 4 of 4

I tried to follow your post but got an error when Iattempted to create a new site using the custom webtemp*.xml. The error says 'Failed to instantiate file "default.master" from module "DefaultMasterPage": Source path "default.master" not found.'

Do you have any idea what I might have missed?
 on 12/10/2008 9:10 PM

What's your response to AC's comments?

Hi Raymond
What is your response to AC's comments on your proposed best practice? I'm new to this all and am trying to figure out whose approach to follow.

The comments I'm tallking about are here, comment on 29/10/08: http://andrewconnell.com/blog/archive/2008/02/15/You-dont-need-to-create-site-definitions.aspx
 on 12/11/2008 1:28 AM

Response to AC

Don,
I've posted my response on AC's blog and hopefully it will show up soon.  I will also be adding a new post with more sample code soon.

Thanks!
Raymond MitchellNo presence information on 12/11/2008 6:40 AM

Stapling issues

I liked the lateral thinking in this, but have hit one snag testing it out - my stapling+stapled features doen't activate. A number of people seem to have this issue, so it may not be related to this approach, but if you have it working let me know how!

For now I'm adding (activating) a 'master' feature at the end of the provisioning provider method, and that receiver then blocks until provisioning is finished and adds (activates) the desired sub features.

 on 12/24/2008 1:26 AM

Re: Templates and SharePoint Sites - 4 of 4

Hi people,
I am new to site definitions, so my question may be stupid, but why not try?
How can I configure one certain thing: In a standard document workspace, as it comes, I need, in the document library webpart on the main page of the workspace, to add a Version column to the default view.
Is that difficult?
Thank you :)
 on 1/8/2009 8:59 AM

Great job

I've taken this and run with it - well done!

I'll blog on this later (and give you kudos for the idea of course), but essentially you provision your chosen OOTB def, then add (activate) a 'master' web feature in the provisioner. The receiver for this blocks until provisioning has completed, then adds any other web features needed in the order you require (other approaches such as the onet, stapling, feature dependencies etc don't seem to enforce ordering or wait for provisioning to complete). Using WSPBuilder, it can all easily be packaged into a single solution (even if the features are in different projects).

Result: no ONET, your own custom Site Template, base it on an OOTB Site Def if you want AND do everything in Features that can be in different projects but one VS Solution building a single WSP.

Most Excellent!

<a href='http://www.sharepointblogs.com/mossms'>MOSSuMS</a>
 on 2/6/2009 12:29 AM

I've written up my approach based on this blog post

Have a look and let me know your thoughts. It strikes me as something we could turn into a nice xml file driven process:

<CustomCfg id="webtempCustDef#0" parent="STS#0">
  <FeatureScopes>
    <SiteFeatures>
      <Feature Guid="guid1">
    </SiteFeatures>
    <WebFeatures>
      <Feature Guid="guid2">
    </WebFeatures>
  </FeatureScopes>
</CustomCfg>

http://www.sharepointblogs.com/mossms/archive/2009/02/09/template-that.aspx

Regards, Mike
 on 2/9/2009 2:17 AM

Works great until used programmatically

I've used this for several templates and it's been working great from the UI. However I've now just tried creating them programmatically using
                using (SPSite site = application.Sites.Add(relativeUrl, newSite.Title, newSite.Description, 1033, template, user.AccountName, user.DisplayName, user.Email))

and it fails everytime with
File or arguments not valid for site template 'templatename'

Do you know if this is this a generic problem with this approach or just with mine.

Richard
 on 7/3/2009 8:14 AM