Programmatically retrieve Managed Paths

Introduction:

By defining managed paths, you can specify which paths in the URL namespace of a Web application are used for site collections. You can specify that one or more site collections exists at a specified path. This can be a convenient method of consolidating multiple site access for users in various departments.

Types of Managed Paths:

  • Explicit inclusion : An explicitly named path (for example, http://server/sites/team) is used for a single site collection.
  • Wildcard inclusion.: A wildcard path of “sites” (for example, http://server/sites/) indicates that child URLs of the path are site collections.

The Code:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Get managed path and display on console
public void GetManagedPath()
{
	try
	{
		// get current site and web application object
		SPSite currentSite = SPContext.Current.Site;
		SPWebApplication currentWebApp = currentSite.WebApplication;		
 
		// Get first level node of managed path and store in array list
		foreach (SPPrefix prefix in currentWebApp.Prefixes)
		{
			Console.WriteLine("Managed Path :{0}, PrefixType :{1}", prefix.Name, prefix.PrefixType);
		}
	}
	catch{// handle exception
	}
}
 
// Create explicit managed path
Public CreateManagedPath()
{
	string path = "HR";
	SPSite currentSite = SPContext.Current.Site;
	SPWebApplication currentWebApp = currentSite.WebApplication;
 
	SPPrefixCollection prefixColl = currentWebApp.Prefixes;
	if (prefixColl.Contains(path) == false)
	{
		SPPrefix newPrefix = currentWebApp.Prefixes.Add(path, SPPrefixType.ExplicitInclusion);
		Console.WriteLine(path+" is successfully added to the web application");
	}
	else
	{
		Console.WriteLine(path + " already existe in the web application");
	}
	Console.ReadLine();
}

References:-

http://technet.microsoft.com/en-us/library/cc261845.aspx
http://social.technet.microsoft.com/Forums/en/sharepoint2010programming/thread/ca344357-cf34-4a51-aea0-b0ed8f81aa86

Thanks!
Avinash

calendarMarch 16, 2012 · cardInfoyen · commentsNo Comments
tagTags: , , , ,  · Posted in: MOSS, SharePoint

Leave a Reply

Spam Protection: , required

myworldmaps infoyen