Create Site in Sharepoint Programmaticaly

There are two different concepts in sharepoint. Those are Site and Web. In sharepoint SPSite class models a collection websites. SPSite class is the model for a collection of SPWeb objects.

In this article I am explaining how to create site for root site in Sharepoint.

Before going to create new site in sharepoint, you need some basic information like URL of the new site, name of the new site, description for the new site and locale identifier.

To create new site
Open Visual studio - File - Create New project
then select C# as the language and Console Application from the panel.
Give CreateSite as name for project, then click ok.


After Creating the project, you need to add the Sharepoint DLL for your project.
For this right click on solution Explorer and select Add Reference . Browse for Microsoft.Sharepoint.dll file and click ok.
With default installation of Sharepoint, you can find this dll file at below location.

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll


So, you successfully establish the environment to create site in Sharepoint.
The code to create site is shown below.


    
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; namespace CreateSite { class Program { static void Main(string[] args) { SPSite site = new SPSite("http://home-pt72rbbzr0/"); SPWebApplication webApp = site.WebApplication; webApp.Sites.Add("/sites/FirstTestSite", "This is my Test Site", "This is the First Site Added Programmatically", 1033, "STS#0", @"Home\Administrator", "Administrator", "home@home.com"); Console.WriteLine("Test Site Created"); Console.ReadLine(); } } }

In the above code, if you observe we created the SPSite object for root site i.e, http://home-pt72rbbzr0 (In my System I have root URL as like this, you may have different root site URL. Please mention your sharepoint root site URL to create site for this root site)

After that I created reference for SPWebApplication class to call Add method for the sites property of site collection.
Here Add method sites property requires several fields.
Those are site URL(Where you want create your site, here /sites/FirstTestSite)
Site description(Description about your site)
Locale identifier(default is 1033 for US English)
Template Id(We have several Template Id’s depending on the site we created, here it is STS#0 for Blank site)
Owner login: owner of the site(here Home\Administrator)
Owner Username: Administrator
Owner email: home2home.com.

We write the code to create site, then we need to deploy it on production server.
If the production server is same as develop server, then just press F5, if not you need to copy the entire project to production server and install or deploying your code using various deployment methods in .Net Framework and install on server.



After completing the installation, open new Browser window with the new site URL(i.e. http://home-pt72rbbzr0/sites/FirstTestSite), then you see that the new site has been create with supplied values. The Browser window look like below.

New Sharepoint Site

The process of creating new site will take a very long time when you are installing it on production server, even on traffic free server. Don’t panic for this.

Download source code here