Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / IIS

IIS - Creating sites automatically from template

5.00/5 (3 votes)
7 Mar 2013CPOL1 min read 20.2K   216  
Automatically creating sites in IIS from template

Introduction

If you ever had the need to create a large number of sites in your IIS server or servers, this article will show you how it is easily done. This little console application generates sites and the required application pools following an XML template you provide.  The code for reading values from the XML template is provided in the sample code for download. 

Using the code

First we need to add a reference to "Microsoft.Web.Administration.dll" which is located in "C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll". The Microsoft.Web.Administration namespace contains classes that a developer can use to administer IIS Manager. 

We have our template:

XML
<?xml version="1.0" encoding="utf-8" ?>
<SiteTemplate>
    <Site name="[sitePrefix].test.domain.com" path="D:\[sitePrefix]\test" port="80">
      <Application alias="/ResourceManager" path="D:\[sitePrefix]\admin.rm"></Application>
      <VirtualDirectory alias="/RMResources" path="D:\FS\ResourceManager"></VirtualDirectory>
      <VirtualDirectory alias="/WebResources" 
        path="D:\[sitePrefix]\_shopWebResources"></VirtualDirectory>
      <ApplicationPool name="[sitePrefix].test.domain.com" framework="v4.0" 
        IdentityUserName="" IdentityPassword=""></ApplicationPool>
    </Site>
    <Site name="[sitePrefix].test.services.domain.com" 
        path="D:\[sitePrefix]\services" port="80">
      <ApplicationPool name="[sitePrefix].test.services.domain.com" 
        framework="v4.0" IdentityUserName="" 
        IdentityPassword=""></ApplicationPool>
    </Site>
</SiteTemplate>

Each site can consist of many applications, virtual directories, and only one application pool. You must have noticed the [sitePrefix] string. This is useful if you need to create different sites using similar names and settings, like in my case we needed to create feature1.mydomain.com, feature2.mydomain.com so you can do this using the same template with different values. 

To start administering IIS: 

C#
// instantate ServerManager object for administrating IIS
ServerManager serverManager = new ServerManager();  

Creating a site in IIS si done like this:

C#
// add site to IIS
Site site = serverManager.Sites.Add(name, path, port);
// add application pool name, in my case name is equel to site name
site.ApplicationDefaults.ApplicationPoolName = name;
 
// add binding for you site
Binding oBinding = site.Bindings.CreateElement();
oBinding.Protocol = "http";
oBinding.BindingInformation = string.Format("*:{0}:{1}", port, name);
site.Bindings.Clear();
site.Bindings.Add(oBinding);
 
// auto start the site when changes are commited
site.ServerAutoStart = true;  

If you want to add an application to your site: 

C#
site.Applications.Add(alias, physicalPath);   

Or virtual directory: 

C#
site.Applications[0].VirtualDirectories.Add(alias, physicalPath);   

Application[0] means the root application (site). Application[1] is the application within the site structure. 

If we need to create a custom application pool:

C#
ApplicationPool newPool = _serverManager.ApplicationPools["CustomPool"];

if (newPool == null)
{
    serverManager.ApplicationPools.Add("CustomPool");
    newPool = serverManager.ApplicationPools["CustomPool"];
    newPool.Name = "CustomPool";
    newPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
    newPool.ManagedRuntimeVersion = "v4.0";

    newPool.ProcessModel.IdentityType = ProcessModelIdentityType.SpecificUser;
    newPool.ProcessModel.UserName = "MyCustomUsername";
    newPool.ProcessModel.Password = "MyCustomPassword";
}

When we are done, just call: 

C#
serverManager.CommitChanges(); 

Now would be a good idea to restart you IIS server.  

In the supplied sample code you can see in detail how sites are created reading values from the template. Customize the template for your needs.  

Hope this is helpful. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)