Introduction
It looks like it is very difficult to programmatically find the layouts folder of SPS using the IIS object model but I found a very easy and interesting method using Directory
Services and I am sure you will find it easy too.
private string FindSPSPath( string spsPath )
{
try
{
string metabasePath="IIS://Localhost/W3SVC";
DirectoryEntry path = new DirectoryEntry(metabasePath);
PropertyCollection properties=path.Properties;
DirectoryEntries entries=path.Children;
foreach(DirectoryEntry entry in entries)
{
if( entry.SchemaClassName=="IIsWebServer" &&
entry.Properties["ServerComment"][0].ToString().
ToLower().Equals(spsPath.ToLower()) )
{
DirectoryEntries vdirs = entry.Children;
foreach(DirectoryEntry vdir in vdirs)
if( vdir.Name== "root")
{
DirectoryEntries childVdirs = vdir.Children;
foreach(DirectoryEntry childVdir in childVdirs)
if(childVdir.Name=="_layouts")
return childVdir.Properties["Path"].Value.ToString();
}
}
}
return null;
}
catch
{
throw;
}
}
What I have done is, I traverse through the collection of children of the IIS Server, i.e., IIsWebService. It contains all the Web Servers
and Application Pools indicated by a number n following the path IIS://Localhost/W3SVC/n. The next step is to find the appropriate Web Server you
are looking for, which can be found through the ServerComment property of the Web Server. Also do not forget to check the type of child of IIS as it can be
an application pool and application pools do not have such a property. The SchemaClassName of the Web Servers is IIsWebServer.
Now we have found the Web Server of SPS (SharePoint Portal Server). The next step is to find the layouts virtual directory of SPS. Here, something typical to SPS comes.
The SPS contains two virtual directories which are not visible through inetmgr: root and filters. root is the virtual directory that we are interested in as it contains
all the virtual directories including _vti_bin, Layouts, etc. So we have now reached the layouts virtual directory of SPS. The final step is to find its physical path.
The Path property of virtual directories contain their physical path. In a similar fashion, you can find any property of any virtual directory or modify them.