Showing posts with label How to create site collection under managed paths different ways to create site collection in SharePoint 2010.. Show all posts
Showing posts with label How to create site collection under managed paths different ways to create site collection in SharePoint 2010.. Show all posts

Thursday, September 20, 2012

Programmatically how to create site collections in SharePoint


Method 1

In this method first i will create new web application and then create first 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();
                    //display message like "Web Application and site collection Created Successfully"
                }
                else
                {
                   //display message like "Port Not available"
                }

            }
            catch (Exception ex)
            {
                //show the exception
            }



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


Method 2:


Creating site collection under root web application i.e '/' if already root site collection created then it will through an exception
            SPSite site = null;
            try
            {
                SPWebApplication webApplication = null;
                webApplication = SPWebApplication.Lookup(new Uri("http://ukreddy:8998"));
                site = webApplication.Sites.Add("/", "firstsitecollection", "sitecolection desc", 1033, "STS#0", "UKREDDYSYKAM\\administrator", "UKREDDYSYKAM\\administrator", "s.urkeddy@gmail.com");

            }
            catch (Exception ex)
            {
                //Display error message.
            }
            finally
            {
                if (site != null)
                    site.Close();
            }


Method 3:

If the root site collection is already created then above method through an exception like " ".
Then need to create new site collection under managed path i.e /sites/


            SPSite site = null;
            try
            {
                SPWebApplication webApplication = null;
                webApplication = SPWebApplication.Lookup(new Uri("http://ukreddy:8998"));
                site = webApplication.Sites.Add("/sites/siteCollname", "sitecollectiontitle", "sitecolection desc", 1033, "STS#0", "UKREDDYSYKAM\\administrator", "UKREDDYSYKAM\\administrator", "s.urkeddy@gmail.com");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (site != null)
                    site.Close();
            }