[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-12
7
Medium Priority
?
1,009 Views
Last Modified: 2012-05-12
I need to programmatically create the site and apply a site template to it from a Visual Web Part.
0
Comment
Question by:Rahsaan-Pringle
  • 6
7 Comments
 

Author Comment

by:Rahsaan-Pringle
ID: 36959565
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
 
LVL 5

Assisted Solution

by:danshady
danshady earned 2000 total points
ID: 36959698
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
 

Author Comment

by:Rahsaan-Pringle
ID: 36962075
I'm getting a "Self-Service Site Creation is not enabled." error. Any idea how to enable self service site creation?
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 

Author Comment

by:Rahsaan-Pringle
ID: 36962230
I found how to enable self service site creation. The link is below.

http://technet.microsoft.com/en-us/library/cc261685.aspx
0
 

Author Comment

by:Rahsaan-Pringle
ID: 36963053
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
 

Accepted Solution

by:
Rahsaan-Pringle earned 0 total points
ID: 36965249
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
 

Author Closing Comment

by:Rahsaan-Pringle
ID: 36984663
Thanks to the guys over at http://WinSmarts.com.
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

872 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