Return to Snippet

Revision: 54648
at January 6, 2012 01:24 by jsturtevant


Updated Code
static void CreateSite(string metabasePath, string siteID, string siteName, string physicalPath, string poolname)
       {
           //  metabasePath is of the form "IIS://<servername>/<service>"
           //    for example "IIS://localhost/W3SVC" 
           //  siteID is of the form "<number>", for example "555"
           //  siteName is of the form "<name>", for example, "My New Site"
           //  physicalPath is of the form "<drive>:\<path>", for example, "C:\Inetpub\Wwwroot"
           Console.WriteLine("\nCreating site {0}/{1}, mapping the Root application to {2}:",
               metabasePath, siteID, physicalPath);

           try
           {
               DirectoryEntry service = new DirectoryEntry(metabasePath);
               string className = service.SchemaClassName.ToString();
               if (className.EndsWith("Service"))
               {
                   DirectoryEntries sites = service.Children;
                   DirectoryEntry newSite = sites.Add(siteID, (className.Replace("Service", "Server")));
                   newSite.Properties["ServerComment"][0] = siteName;
                   newSite.Properties["ServerBindings"].Value = ":84:";
                   newSite.Properties["DefaultDoc"].Value = "loginpage.aspx";
                   
                   newSite.CommitChanges();
                   DirectoryEntry newRoot;
                   newRoot = newSite.Children.Add("Root", "IIsWebVirtualDir");
                   newRoot.Properties["Path"][0] = physicalPath;
                   newRoot.Properties["AppFriendlyName"][0] = "websiteName";
                   newRoot.Properties["AccessScript"][0] = true;
                   newRoot.Properties["AccessExecute"][0] = true;
                   newRoot.Properties["AppPoolId"].Value = poolname;
                   newRoot.Invoke("AppCreate", 1);
                   newRoot.CommitChanges();

                   //Start site
                   newSite.Invoke("Start");

                   Console.WriteLine(" Done. Your site will not start until you set the ServerBindings or SecureBindings property.");
               }
               else
                   Console.WriteLine(" Failed. A site can only be created in a service node.");
           }
           catch (Exception ex)
           {
               Console.WriteLine("Failed in CreateSite with the following exception: \n{0}", ex.Message);
           }
       }

Revision: 54647
at January 6, 2012 01:23 by jsturtevant


Initial Code
static void CreateSite(string metabasePath, string siteID, string siteName, string physicalPath, string poolname)
       {
           //  metabasePath is of the form "IIS://<servername>/<service>"
           //    for example "IIS://localhost/W3SVC" 
           //  siteID is of the form "<number>", for example "555"
           //  siteName is of the form "<name>", for example, "My New Site"
           //  physicalPath is of the form "<drive>:\<path>", for example, "C:\Inetpub\Wwwroot"
           Console.WriteLine("\nCreating site {0}/{1}, mapping the Root application to {2}:",
               metabasePath, siteID, physicalPath);

           try
           {
               DirectoryEntry service = new DirectoryEntry(metabasePath);
               string className = service.SchemaClassName.ToString();
               if (className.EndsWith("Service"))
               {
                   DirectoryEntries sites = service.Children;
                   DirectoryEntry newSite = sites.Add(siteID, (className.Replace("Service", "Server")));
                   newSite.Properties["ServerComment"][0] = siteName;
                   newSite.Properties["ServerBindings"].Value = ":84:";
                   newSite.Properties["DefaultDoc"].Value = "loginpage.aspx";
                   
                   newSite.CommitChanges();
                   DirectoryEntry newRoot;
                   newRoot = newSite.Children.Add("Root", "IIsWebVirtualDir");
                   newRoot.Properties["Path"][0] = physicalPath;
                   newRoot.Properties["AppFriendlyName"][0] = "ClarityWeb1.0";
                   newRoot.Properties["AccessScript"][0] = true;
                   newRoot.Properties["AccessExecute"][0] = true;
                   newRoot.Properties["AppPoolId"].Value = poolname;
                   newRoot.Invoke("AppCreate", 1);
                   newRoot.CommitChanges();

                   //Start site
                   newSite.Invoke("Start");

                   Console.WriteLine(" Done. Your site will not start until you set the ServerBindings or SecureBindings property.");
               }
               else
                   Console.WriteLine(" Failed. A site can only be created in a service node.");
           }
           catch (Exception ex)
           {
               Console.WriteLine("Failed in CreateSite with the following exception: \n{0}", ex.Message);
           }
       }

Initial URL


Initial Description
Properties that can be set are found here: http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/cde669f1-5714-4159-af95-f334251c8cbd.mspx?mfr=true

Initial Title
Create IIS Website

Initial Tags
c#

Initial Language
C#