Programatically create a SharePoint site based on a site definition

Posted on 5/19/2007 @ 4:08 PM in #SharePoint by | Feedback | 26573 views

Here is a peice of code that I know you will find useful one day. Basically, you supply it with an existing site definition, say "STS#1", and this will then create a site for you, based on the supplied site definition, at the URL you asked for.

Here goes -

public static bool CreateSite(
    string parentSiteURL, string siteURLRequested, 
    string siteTitle, string siteTemplateName)
    bool returnCondition = false; // Assume failure.

    const Int32 LOCALE_ID_ENGLISH = 1033;

    using (SPSite siteCollection = new SPSite(parentSiteURL))
        SPWeb parentWeb = siteCollection.OpenWeb();
        SPWebTemplateCollection Templates = 
        SPWebTemplate siteTemplate = Templates[siteTemplateName];
        if (parentWeb.Webs[siteURLRequested].Exists)

            false, false);
        // All is good? 
        returnCondition = true;

    return returnCondition;


Sound off but keep it civil:

Older comments..

On 5/21/2007 3:19:01 PM Dez said ..
Maybe you can answer this. I've been sucessfull so far in creating sites programmatically...examples like yours have made that rather simple. The problem I'm running into now is how to access the different templates through the site creation code.

I've used the standard list of:

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

But I cant seem to find any documentation on the "40" templates ID #'s. I've scoured the web trying to find anyone who's made reference to this list of ID's, bu no one seems to have posted or listed this info anywhere.

Would you happen to know what the Id's are for the 40 additional templates or how I would reference them through code in the site creation process?

On 5/21/2007 8:59:59 PM Sahil Malik said ..
Dez -

The best thing to do would be to install a template, and see what ID it gets. The ID depends on how the site definition's XML files are written.


On 5/22/2007 10:54:53 AM Dez said ..

I was actually able to reference the template through its name in code. What's odd is how Microsoft designed this whole thing. The standard templates (listed in my previous post) and the new 20 application templates all get referenced through their ID's, but the new 20 custom templates all get referenced through their names instead.

Would've been nice to see some documentation for that fact from them, but it doesn't matter now that I've got it figured out.

Thanks for the help,


On 5/25/2007 4:05:09 AM Sharepointer said ..
And how can I "see what ID it gets" - I'm new to this.

Also: is it possible to create a Web based on a .stp file?

On 1/2/2008 3:53:16 PM HyperX said ..

Should this code be run on the sharepoint server? How to create a site dynamically using my ASP.NET application, which is hosted on a different box?



On 1/3/2008 1:14:23 AM Sahil Malik said ..
HyperX - you need to WSS Object model to run the above code.

On 1/9/2008 12:06:29 PM Ami said ..

Is it possible to pass in site template (<filename>.stp)?



On 1/10/2008 2:55:53 PM Ami said ..
I am getting following error message while executing the code, can you help?

System.IO.FileNotFoundException occurred

Message="The Web application at http://amrndhw034:10011/sites/TKR2/ could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application."



at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)

at Microsoft.SharePoint.SPSite..ctor(String requestUrl)

at SharepointSiteCreationService.SiteCreatton_Form.CreateSite(String parentSiteURL, String siteURLRequested, String siteTitle, String siteTemplateName) in D:\Projects\SharepointSiteCreationService\SharepointSiteCreationService\SiteCreatton Form.aspx.cs:line 36



On 1/23/2008 5:16:40 AM James said ..

How do I add the wiki template to the site definition? It must go like this:

The Wiki Template is not default in the list of templates in the subsites because it is not included in the site definition.

Once included, we could already stop doing the steps below every time we create a new site:

o Add the Microsite Wiki Site Template (Root > Site Settings > Modify All Site Settings > Page layouts and site templates)

o Ensure that the “Reset all subsites to inherit these preferred subsite template settings” is checked.

On 4/15/2008 1:05:03 PM Anadmin said ..

I tried using your code... but when there are more than one user simultaneously executing the same code to create a site, the code throws a 'Out Of Memory' error when executing the below line







false, false);

We dont know what to do... any help would be highly appreciated...!

On 6/16/2008 12:31:53 PM Kien Tran said ..
Just a remark,

I've been trying to use this code to create a site based on a custom template (Site Settings > Save this site as template...)

I was getting a error :

File or arguments not valid for site template 'Custom.stp'

I got it to work by replacing this line:

SPWebTemplateCollection Templates = siteCollection.GetWebTemplates(Convert.ToUInt32(LOCALE_ID_ENGLISH));

with this line:

SPWebTemplateCollection Templates = siteCollection.GetCustomWebTemplates(Convert.ToUInt32(LOCALE_ID_ENGLISH));



On 6/16/2008 3:08:04 PM Sahil Malik said ..
Kien - excellent tip! Thanks

On 9/22/2008 7:19:32 AM Rohit Jain said ..
/// <summary>

/// Create Sub Site based on the siteTemplateID

/// </summary>

/// <param name="strSiteURL"> Sharepoint toplevel site url</param>

/// <param name="strSiteTitle">Site Title</param>

/// <param name="templateName">Site template Unique Id</param>

/// <param name="siteDescription">site description</param>

/// <param name="locale">Locale (usually 1033)</param>

/// <returns></returns>

public string CreateSiteByTemplateId(string strSiteURL, string strSiteTitle, string templateId, string siteDescription, Int32 locale)


string siteUrl = string.Empty;



SPSite siteCollection = new SPSite(strSiteURL);

//Get TemplateCollection



SPWebTemplateCollection templates =


siteCollection.AllowUnsafeUpdates = true;

SPWeb newWeb = siteCollection.OpenWeb();

SPWebCollection subSites = newWeb.Webs;

SPWeb newSiteWeb = subSites.Add(strSiteTitle, strSiteTitle, siteDescription, 1033, templateId, false, false);

siteUrl = newSiteWeb.Url;



catch (Exception ex)


System.Diagnostics.EventLog.WriteEntry("Application", "Error occured during site creation" + ex.Message.ToString());

throw ex;


return siteUrl;

On 11/11/2008 5:27:42 AM sbk said ..
hi i am trying to create a new site but when executing i am getting followin error, can anyone help in solving this issue.

Error Message:

Could not load file or assembly 'Microsoft.SharePoint.Library, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. The system cannot find the file specified.

On 11/17/2008 5:20:05 AM santhosh said ..

how to get a custom template title...

On 12/4/2008 6:45:48 AM Jas said ..
thanks lot .. CreateSite method working fine..

On 12/29/2008 7:23:55 PM Vlad Gedgafov said ..

Thanks for the post I got it working using Kien Tran comment, but I also had to add

parentWeb.AllowUnsafeUpdates = true;


Otherwise I was getting an error.


On 1/28/2009 2:22:45 PM Greg Naber said ..
This is fantastic. I was able to create this workflow and get it working, even though I don't have much experience with visual studio. I do have another need if anyone can help.

This solution uses the site name from a field. I have the need to create sites with a Name from the list, but the url from another item on the same list.

For instance, the title of the site may be "Project Number - Project Description" but to keep the URL's short the URL would be \"Project Number". Both these fields are easily defined within the list, I would just need a way to pass this information to the workflow.

any help with this would be appreciated, and remember like I said, I have almost no knowledge of programming.. (I know I shouldn't be doing this at all, but tell the owners that.. besides, if they want to pay for my time to be stuck on this, then ok)

Thank you in advance,


On 3/3/2009 10:24:07 AM Dan said ..
My site definition uses a SPWebProvisioningProvider rather than a feature receiver/stapler. I can create a site through the user-interface just fine. However, when I try to create it programatically following your code sample, I get a "The security validation for this page is invalid" error, which is raised inside of the provisioning code (a file in a data connections library is updated and saved). I've tried using your code with elevatedprivileges, but the error remains. Any ideas on a resolution?

On 3/23/2009 3:31:06 AM Kiran said ..
Thanks a TON.

On 10/12/2009 7:22:04 AM yamini said ..

i have created custom site with a document library under it, now want to create folder and upload file under it.

can anyone help me out in this????

On 11/5/2009 1:40:58 AM Deepa said ..
Really nice article...its useful to create site with any template...

Saved my time :)

Thanks a lot

On 11/11/2009 6:40:12 AM Mike said ..

That works great. How would I modify this to allow the site creation data (name, url, owner, template, etc) to be pulled from an excel sheet?

I'd like to list all the data for the site and just automate the site provisioning.

Is there an easy way to do this?


On 11/23/2009 6:51:47 AM Skamuju said ..
I Have downloaded some of the templates from microsoft they are around 15 templates.

I developed a windows app to create site programatically.

I need to give site template code as one of the parameter. So i want to know what are the site template codes for these templates downloaded from microsoft. could you please help me.

Thanks in advance


On 7/29/2010 6:18:07 AM Nasir Khan said ..

Thanks for nice work. I need help that how "programmatically create a site collection using custom site template". Any body can help me as i need it urgently. Thanks.



On 9/23/2010 5:36:09 AM sharon @ sharepoint said ..
gotta love how simple this is in 2010 - 2003/2007 were worth getting through to get here :D

On 4/11/2011 6:59:49 AM Vipul Jain said ..
Hi..Will you please provide the complete code i.e. the existing site definition code also and please suggest that which type of project we have to made..

On 6/5/2012 12:49:39 AM Adarsh kuamr said ..
Hi I want the steps and code for site creation in sharepoint programatically so that whenever a new user cliks a new site is created dynamically