Click here to Skip to main content
Click here to Skip to main content

IIS - Creating sites automatically from template

, 7 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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 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: 

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

Creating a site in IIS si done like this:

// 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: 

site.Applications.Add(alias, physicalPath);   

Or virtual directory: 

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:

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: 

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)

Share

About the Author

Zoran Rubinic
Software Developer (Senior)
Croatia Croatia
No Biography provided

Comments and Discussions

 
QuestionSample codes? PinmemberBigWCat7-Mar-13 7:46 
AnswerRe: Sample codes? PinmemberZoran Rubinic7-Mar-13 8:41 
GeneralRe: Sample codes? PinmemberBigWCat7-Mar-13 10:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 7 Mar 2013
Article Copyright 2013 by Zoran Rubinic
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid