Tuesday, February 12, 2013

Creating a website in IIS7 programatically

This post will let you know about how to create a website in the IIS7 panel programatically.
For this purpose you need to add the reference to the Microsoft.Web.Administration found in the following paths depending on your system configuration:

C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll
OR 
C:\Windows\SysWOW64\inetsrv\Microsoft.Web.Administration.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Web.Administration;

namespace WebSiteCreator
{
    ///


    /// Interaction logic for MainWindow.xaml
    ///

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CreateWebSite()
        {
            string siteName = txtSiteName.Text.Trim();
            string applicationPoolName = txtAppPoolName.Text.Trim();
            string virtualDirectoryPath = txtVDPath.Text.Trim();
            string virtualDirectoryPhysicalPath = txtVDPhysicalPath.Text.Trim();
            string ipAddress = txtIpAddress.Text.Trim();
            string tcpPort = txtPort.Text.Trim();
            string hostHeader = txtHostHeader.Text.Trim();
            string applicationPath = txtAppPath.Text.Trim();
            long siteId = 1;

            using (ServerManager servMgr = new ServerManager())
            {
                Site site = servMgr.Sites[siteName];

                if (site != null)
                    return;

                ApplicationPool appPool = servMgr.ApplicationPools[applicationPoolName];

                if (appPool == null)
                {
                    appPool = servMgr.ApplicationPools.CreateElement();
                    appPool.SetAttributeValue("name", applicationPoolName);
                }

                foreach (Site mysite in servMgr.Sites)
                {
                    if (mysite.Id > siteId)

                        siteId = mysite.Id;
                }

                siteId++;

                site = servMgr.Sites.CreateElement();

                site.SetAttributeValue("name", siteName);

                site.Id = siteId;

                site.Bindings.Clear();

                string bindingSetting = ipAddress + ":" + tcpPort + ":" + hostHeader;

                Microsoft.Web.Administration.Binding binding = site.Bindings.CreateElement();

                binding.Protocol = "http";

                binding.BindingInformation = bindingSetting;

                site.Bindings.Add(binding);

                Microsoft.Web.Administration.Application app = site.Applications.CreateElement();

                app.Path = applicationPath;

                app.ApplicationPoolName = applicationPoolName;

                Microsoft.Web.Administration.VirtualDirectory vdir = app.VirtualDirectories.CreateElement();

                vdir.Path = virtualDirectoryPath;

                vdir.PhysicalPath = virtualDirectoryPhysicalPath;

                app.VirtualDirectories.Add(vdir);

                site.Applications.Add(app);

                servMgr.Sites.Add(site);

                servMgr.CommitChanges();
            }
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            CreateWebSite();
        }
    }
}

Link to download the software - Download