Today I needed to check through code whether a managed path is defined and couldn’t find any sample code or information on the net so I decided to investigate this myself.
I starting from the place where I know managed paths are shown and this is the “Defined Managed Paths” page in the Central Administration. The page is actually _admin/scprefix.aspx. I located this page in the 12 hive under 12\TEMPLATE\ADMIN and opened it up in Notepad to see which was the code behind class. It was Microsoft.SharePoint.ApplicationPages.SscPrefixPage:
<%@ Assembly Name=”Microsoft.SharePoint.ApplicationPages.Administration, Version=188.8.131.52, Culture=neutral, PublicKeyToken=71e9bce111e9429c”%>
<%@ Page Language=”C#” Inherits=”Microsoft.SharePoint.ApplicationPages.SscPrefixPage” MasterPageFile=”~/_admin/admin.master” %>
I then located and opened the Microsoft.SharePoint.ApplicationPages.Administration assembly in Reflector and found the code behind class. After examining it for a while I found that there is a public class available that represents the collection of all managed paths inside a web application and this class is Microsoft.SharePoint.Administration.SPPrefixCollection. Then I noticed that this class has a constructor that accepts an SPWebApplication and initializes the managed paths for this web application. Unfortunately the constructor was with internal visibility. I almost started writing reflection code to call it but first decided to check the “Analyze” option in Reflector and see if any other classes are calling this constructor. Then I discovered that (logically) the SPWebApplication calls it and even has a member that represents the managed paths. This member is called Prefixes. Looking at the methods of the SPPrefixCollection class I found that checking for existing manages paths, adding new ones or deleting existing ones is supported. This is exactly what I was looking for.
// Adds the specified prefix to the collection.
public SPPrefix Add(string strPrefix, SPPrefixType type);
// Returns a Boolean value that indicates whether the collection contains the specified prefix.
public bool Contains(string strPrefix);
// Deletes the specified prefix from the collection.
public void Delete(string strPrefix);
// Deletes the specified array of prefixes from the collection.
public void Delete(string strPrefixes);
Now I can go and finish my code and start packing up for my 2 weeks holiday. Oh Yeah!