Click here to Skip to main content
13,590,480 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 7 Mar 2013
Licenced CPOL

IIS - Creating sites automatically from template

, 7 Mar 2013
Rate this:
Please Sign up or sign in to vote.
Automatically creating sites in IIS from template


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" ?>
    <Site name="[sitePrefix]" 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" 

      <ApplicationPool name="[sitePrefix]" framework="v4.0" 

        IdentityUserName="" IdentityPassword=""></ApplicationPool>
    <Site name="[sitePrefix]" 

        path="D:\[sitePrefix]\services" port="80">
      <ApplicationPool name="[sitePrefix]" 

        framework="v4.0" IdentityUserName="" 


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, 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);
// 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)
    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: 


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. 


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


About the Author

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

You may also be interested in...

Comments and Discussions

QuestionError on build Pin
Filipe Rafaeli17-Feb-15 5:17
memberFilipe Rafaeli17-Feb-15 5:17 
AnswerRe: Error on build Pin
Filipe Rafaeli17-Feb-15 5:51
memberFilipe Rafaeli17-Feb-15 5:51 
QuestionSample codes? Pin
BigWCat7-Mar-13 6:46
memberBigWCat7-Mar-13 6:46 
AnswerRe: Sample codes? Pin
Zoran Rubinic7-Mar-13 7:41
memberZoran Rubinic7-Mar-13 7:41 
GeneralRe: Sample codes? Pin
BigWCat7-Mar-13 9:59
memberBigWCat7-Mar-13 9:59 

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

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

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