Tuesday, 18 September 2012

Programmatically How to create web application in SharePoint 2010 using Server Object Model




Here the code snippet to create a new web application in SharePoint 2010 using Server Object Model.


Step 1:

Add the following namespace to project


using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData.SystemSpecific;

Step 2:

Write the following code for creating SPWebApplication

Here am taking some values from text boxes


            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);//Checking port is in use or not
                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.Provision();
                    
                }
                else
                {
                    MessageBox.Show("Port Not available");
                }

            }
            catch (Exception ex)
            {
                label1.Text = ex.Message;
            }



//Here the actual method for checking given port number is in use or not


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

1 comment: