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.
Rahsaan-PringleAsked:
Who is Participating?
 
Rahsaan-PringleConnect With a Mentor Author Commented:
Thanks to the dudes over at http://WinSmarts.com
/// <summary>
        /// From http://blah.winsmarts.com/2007-5-Programatically_create_a_SharePoint_site_based_on_a_site_definition.aspx
        /// </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.Delete(siteURLRequested);
                }

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

                // All is good? 
                returnCondition = true;
            }

            return returnCondition;
        }

Open in new window

0
 
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.
O12W2.pdf
0
 
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"))
    oSiteCollectionSelfServ.Dispose();
}
0
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?
0
 
Rahsaan-PringleAuthor Commented:
I found how to enable self service site creation. The link is below.

http://technet.microsoft.com/en-us/library/cc261685.aspx
0
 
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:
http://bit.ly/p4u6kT

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?
0
 
Rahsaan-PringleAuthor Commented:
Thanks to the guys over at http://WinSmarts.com.
0
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.