How do you create a SharePoint 2010 site in C# code?

I need to programmatically create the site and apply a site template to it from a Visual Web Part.
Who is Participating?
Rahsaan-PringleConnect With a Mentor Author Commented:
Thanks to the dudes over at
/// <summary>
        /// From
        /// </summary>
        /// <param name="parentSiteURL"></param>
        /// <param name="siteURLRequested"></param>
        /// <param name="siteTitle"></param>
        /// <param name="siteTemplateName"></param>
        /// <returns></returns>
        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 = siteCollection.GetWebTemplates(Convert.ToUInt32(LOCALE_ID_ENGLISH));
                SPWebTemplate siteTemplate = Templates[siteTemplateName];
                if (parentWeb.Webs[siteURLRequested].Exists)

                parentWeb.Webs.Add(siteURLRequested, siteTitle, "", Convert.ToUInt32(LOCALE_ID_ENGLISH), siteTemplate, false, false);

                // All is good? 
                returnCondition = true;

            return returnCondition;

Open in new window

Rahsaan-PringleAuthor Commented:
Here, you can see what I am trying. The commented out rows are what I tried, and the error that it gives. In the last row, I tied specifically creating the SPWebCollection object, to no avail.
danshadyConnect With a Mentor Commented:
You could use self service site creation...  something like this

using(SPSite oSiteCollection = SPContext.Current.Site)
    SPSite oSiteCollectionSelfServ = oSiteCollection.SelfServiceCreateSite("Absolute_URL", "Title",
    "Description", Locale_ID, "STS", "Owner_User_Name",
    "Owner_Display_Name",  "Owner_E-mail",
    "Contact_User_Name", "Contact_Display_Name", "Contact_E-mail"))
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Rahsaan-PringleAuthor Commented:
I'm getting a "Self-Service Site Creation is not enabled." error. Any idea how to enable self service site creation?
Rahsaan-PringleAuthor Commented:
I found how to enable self service site creation. The link is below.
Rahsaan-PringleAuthor Commented:
Having enabled self service site creation, I am getting the following error:
The content databases in this cluster are full.  You cannot add more Web sites until you change the content database Web site capacity settings or add more content databases.

I modified the database capacity settings using the instructions found at the following URL:

Right now, my [Maximum Number of Site Collections] is 35000, but I am still getting that error. Has anyone got an idea how I can really change the content database Web site capacity settings or add more content databases?
Rahsaan-PringleAuthor Commented:
Thanks to the guys over at
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.