Thursday, October 4, 2012

Programmatically how to create site collection in SharePoint 2010


In previous post we have seen how to create web application using server object model.


Now here the code snippet to create new site collection using Server Object Model.


Method 1:


in this method , first, once again creating new web application then creating root site collection in that web application. 


try
{
int port = Convert.ToInt32(txtPortNo.Text);

string UserName = txtUN.Text;
string DBName = txtDBName.Text;
string ServerName = txtServerName.Text;
string SiteCollectionTitle = txtSiteCollectionTitle.Text;              
string SiteCollectionDesc = txtSiteColDesc.Text;
string SiteColOwner = txtSiteColOwner.Text;
string SiteColOwnerEmail = txtEmailAddress.Text;
bool isAvail = CheckPortUsage(port);
if (isAvail)
{
SPWebApplicationBuilder builder = new SPWebApplicationBuilder(SPFarm.Local);

SPWebApplication newApplication;
builder.Port = port;
builder.ApplicationPoolId =
"SharePoint - " + port.ToString();

builder.IdentityType = IdentityType.SpecificUser;
SPFarmManagedAccountCollection manaccountcollection = new SPFarmManagedAccountCollection(SPFarm.Local);
SPManagedAccount maccount = manaccountcollection.FindOrCreateAccount(@UserName);
builder.ManagedAccount = maccount; //use the SPManagedAccount to receive the username and password automatically
builder.RootDirectory = new System.IO.DirectoryInfo("C:\\Inetpub\\wwwroot\\wss\\VirtualDirectories\\" + port.ToString());
builder.CreateNewDatabase = true;
builder.DatabaseName = DBName;
builder.DatabaseServer = ServerName;
builder.UseNTLMExclusively =
true;

newApplication = builder.Create(); // Create new web application
newApplication.Name = "SharePoint-" + port;
newApplication.Provision();
SPSite mySiteCollection = newApplication.Sites.Add("/", SiteCollectionTitle, SiteCollectionDesc, 1033, "STS#0", @UserName, SiteColOwner, SiteColOwnerEmail);

mySiteCollection.Close();
MessageBox.Show("Web Application and site collection Created Successfully");

}
else

{
MessageBox.Show("Port Not available");

}
}
catch (Exception ex)

{
label1.Text = ex.Message;
}



public static bool CheckPortUsage(int port)

{

try

{

new TcpClient(new IPEndPoint(IPAddress.Any, port)).Close();

return true;

}

catch

{

return false;

}

}



Method 2:


in this method  we are trying to create a site collection(if u want to create root site collection replace "sites/sitecol3" with "/") in already existing web application.


SPSite site = null;

try
{

SPWebApplication webApplication = null;

webApplication = SPWebApplication.Lookup(new Uri("http://ukreddy:8998"));
site = webApplication.Sites.Add("sites/sitecol3", "anothersitecollection", "sitecolection", 1033, "STS#0", "UKREDDYSYKAM\\administrator", "UKREDDYSYKAM\\administrator", "s.urkeddy@gmail.com");
}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

Pls let me know your queries/suggestions ...

2 comments:

  1. Thanking very much RKReddy ..
    i made console application ..
    working well
    below Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Administration;
    using System.Net;
    using System.Net.Sockets;

    namespace Wesgrow_D
    {
    class Program
    {
    static void Main(string[] args)
    {
    try
    {

    string UserName = "bala";// Login ID
    string SiteCollectionTitle = "Wesgrow Software";
    string SiteCollectionDesc = "We Shall Grow";
    string SiteColOwner = "KSR";
    string SiteColOwnerEmail = "ksrbala@gmail.com";

    SPWebApplicationBuilder builder = new SPWebApplicationBuilder(SPFarm.Local);
    SPWebApplication newApplication = null;

    int port = builder.Port;
    bool isAvail = CheckPortUsage(port);
    if (isAvail)
    {
    SPFarmManagedAccountCollection manaccountcollection = new SPFarmManagedAccountCollection(SPFarm.Local);
    SPManagedAccount maccount = manaccountcollection.FindOrCreateAccount(@UserName);
    builder.UseNTLMExclusively = true;
    newApplication = builder.Create(); // Create new web application
    newApplication.Provision();
    // Create new Site Collection
    SPSite mySiteCollection = newApplication.Sites.Add("/", SiteCollectionTitle, SiteCollectionDesc, 1033, "STS#0", @UserName, SiteColOwner, SiteColOwnerEmail);
    mySiteCollection.Close();
    Console.WriteLine("Web Application and site collection Created Successfully");
    }
    else
    {
    Console.WriteLine("Port Not available");
    }
    Console.Read();
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    Console.Read();
    }
    }

    public static bool CheckPortUsage(int port)
    {
    try
    {
    new TcpClient(new IPEndPoint(IPAddress.Any, port)).Close();
    return true;
    }
    catch
    {
    return false;
    }
    }
    }
    }

    ReplyDelete
  2. Nice its working fine bala murugan

    ReplyDelete