Wednesday, April 07, 2010

Adding new Virtual Directories with code!!!

Ok, we had a question come up at the User Group (UG) the other night. This person needs to create new virtual directories each time a new site is created. These VirDirs are to host legacy ASP.net applications. Now I know the CORRECT answer (at least mine) is to not host other applications on your SharePoint FEWS. However in the interest of being fair and realizing that it may be the only option for some folks, here is some C# code you can use where appropriate:
=================
//Create New Virtual Directory in IIS with DirectoryEntry()
string wwwroot = "c:\\Inetpub\\wwwroot";
string virtualDirectoryName = "myNewApp";
string sitepath = "IIS://localhost/W3SVC/1/ROOT";
DirectoryEntry vRoot = new DirectoryEntry(sitepath);
DirectoryWntry vDir = vRoot.Children.Add(virtualDirectoryName, "IIsWebVirtualDir");
vDir.CommitChanges();
vDir.Properties["Path"].Value = wwwroot + "\\" + virtualDirectoryName;
vDir.Properties["DefaultDoc"].Value = "Default.aspx";
vDir.Properties["DirBrowseFlags"].Value = 2147483648;
vDir.Commitchanges();
vRoot.CommitChanges();
======================
Enjoy!

No comments: