Custom SharePoint Site Provisioning
There are lots of things that you can do with SharePoint-based automation that allow you to take basic SharePoint tasks and execute them in your custom applications. One of the more useful ones is site creation automation (the creation of a SharePoint site, through code, using a site template). Here's a quick code snippet to illustrate:
public static bool CreateSharePointSite(
string parentURL, string siteURLnode, string siteTitle, string siteTemplateName)
{
bool successStatus= false;
using (SPSite siteCollection = new SPSite(parentURL))
{
SPWeb parentWeb = siteCollection.OpenWeb();
SPWebTemplateCollection Templates = siteCollection.GetWebTemplates(1033);
SPWebTemplate siteTemplate = Templates[siteTemplateName];
parentWeb.Webs.Add(siteURLnode, siteTitle, "", 1033, siteTemplate, false, false);
successStatus = true;
}
return successStatus;
}
A few things to note here:
- This method will create a SharePoint site under a specified site using the site template defined. It will inherit the security of the parent.
- I'm using 1033 (English) but it can be any supported language.
- It is easy to create memory leaks with the SharePoint object model, especially with site manipulation. Notice the use of the 'using' statement as the correct way to take advantage of object disposal.
- Don't forget to wrap this with an error handler!
What if you don't want to use a custom site template? If you would like to use one of the native templates, you can substitute the siteTemplate value above based on the following mapping (e.g. a basic team site would be created with siteTemplate="STS#0"):
STS#0 Team Site
STS#1 Blank Site
STS#2 Document Workspace
MPS#0 Basic Meeting Workspace
MPS#1 Blank Meeting Workspace
MPS#2 Decision Meeting Workspace
MPS#3 Social Meeting Workspace
MPS#4 Multipage Meeting Workspace
WIKI#0 Wiki Site
BLOG#0 Blog Site
Finally, Microsoft has a downloadable code example on site provisioning ("SharePoint Server 2007 Sample: Creating a Custom User Site Provisioning Solution with Office SharePoint Server 2007"). The link is http://www.microsoft.com/downloads/details.aspx?FamilyID=5B6C8FB0-9B67-47DB-8A09-BCA76BC9A5D1&displaylang=en.
Happy coding!