Creating SharePoint Site Collection Programmatically
Creating a SharePoint 2010 site collection programmatically is straightforward. In this example, I will create a SharePoint site collection by using .Net Console Application.
Open Microsoft Visual Studio here i am using Microsoft Visual Studio 2012 and create new console Application project
Click File -> New -> Project -> Console Application
Enter name of project "SP_New_SiteCollection". and select the targeted framework to .NET Framework 3.5.
Make sure that target platform is 64bit to check this click project ->SP_New_SiteCollection Properties ->build
Now add the SharePoint 2010 reference. In Solution Explorer Right click on References -> Add Reference then click Extensions and Select Microsoft.SharePoint and click OK
Now Use the following Source Code to Create New Site Collection Programmatically:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace SP_New_SiteCollection
{
class Program
{
static void Main(string[] args)
{
createNewSiteCollection();
Console.ReadKey();
}
/*
* Title: Create New Site Collection
* Author: Axeex
*/
private static void createNewSiteCollection()
{
SPSite rootSite = null;
try
{
using (rootSite = new SPSite("http://dev.sharepoint.local/sites/local"))
{
SPWebApplication
webApplication = rootSite.WebApplication;
using (SPSite newSpCollection =
webApplication.Sites.Add(
"sites/axeex", // site
URL
"Axeex Code Site", // Site Title
"This site collection is create by using c#
code", // site
Descriptions
1033, // LCID
"STS#0", // web site template
"SP2010DEV\\axeex", // owner Login
"Axeex", // owner Name
"axeex@spdeveloper.com")) // owner Email
{
Console.WriteLine("Create Site
Collection {0}", newSpCollection.Url);
}
}
}
catch (SPException ex)
{
Console.WriteLine("Error -----
> " + ex.ToString());
}
finally
{
rootSite.Dispose();
}
}
}
}
I hope this code will help to create site collection.
Comments
Post a Comment