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

Posted on 2011-10-12
Last Modified: 2012-05-12
I need to programmatically create the site and apply a site template to it from a Visual Web Part.
Question by:Rahsaan-Pringle

    Author Comment

    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.
    LVL 5

    Assisted Solution

    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"))

    Author Comment

    I'm getting a "Self-Service Site Creation is not enabled." error. Any idea how to enable self service site creation?

    Author Comment

    I found how to enable self service site creation. The link is below.

    Author Comment

    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?

    Accepted Solution

    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


    Author Closing Comment

    Thanks to the guys over at

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
    There is one common problem that all we SharePoint developers share: custom solution deployment. This topic can't be covered fully in this short article, so all I want to do in this one is to review it from a development-to-operations perspectiv…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video discusses moving either the default database or any database to a new volume.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now