Monday, October 15, 2012

Programmatically How to create,get and delete managed path in SharePoint 2010


Introduction:

Managed Paths - We can specify which paths in the URL namespace of a Web application are used for site collections. We can also specify that one or more site collections exists at a specified path.

Types of Managed Paths:

Explicit inclusion.
Wildcard inclusion.

Explicit inclusion:

An explicitly named path is used for a single site collection.

Example: http://server/sites/team.

Wildcard inclusion:

A wildcard path of "sites" indicates that child URLs of the path are site collections. An wildcard named path is used for a multiple site collections.

Example: http://server/sites/

In this article we will be seeing the following

Programmatically create managed paths for a web application,
Programmatically get all the managed paths for a web application, and
Programmatically delete a managed path from a web application.

1. Creating a managed path:

string path = "NewManagedPath";
SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://ukreddy:6969/"));
SPPrefixCollection prefixColl = webApp.Prefixes;
if (prefixColl.Contains(path) == false)
   {
      SPPrefix newPrefix = webApp.Prefixes.Add(path, SPPrefixType.ExplicitInclusion);
      MessageBox.Show(path + " is successfully added to the web application");
   }
else
  {
       MessageBox.Show(path + " already existe in the web application");
  }


the above code is used to create managed path in http://ukreddy:6969 web application.

the following screen shot show managed paths before executing the above code.
to get the list of managed path we use the step 2 code.



Once the above code is executed, it will create managed path in the web application.

the following screen show shows the message after successfuly creation of managed path.

Once the path is created once again retrieving list of managed path in the web application.


2. Get all managed paths:

SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://ukreddy:6969/"));
SPPrefixCollection prefixColl = webApp.Prefixes;
string str = "";
foreach (SPPrefix prefix in prefixColl)
  {
      str += "Managed Path :" + prefix.Name + ", Prefix Type :" + prefix.PrefixType + "\n";
  }
MessageBox.Show(str);

the following screen show showing list of managed path in the web application i.e http://ukreddy:6969.



3. Delete a managed path:

string path = "NewManagedPath";
SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://ukreddy:6969/"));
SPPrefixCollection prefixColl = webApp.Prefixes;
if (prefixColl.Contains(path) == true)
  {
      webApp.Prefixes.Delete(path);
      MessageBox.Show(path + " is successfully deleted from the web application");
  }
else
  {
      MessageBox.Show(path + " managed path not exist in the web application");
  }

The above code is used to delete a managed path from the web application.

Once we sucessfully execute the above code it will display message,




after deletion of managed path , the following screen shows list of managed paths.



here the complete source code...

Reference to this link.

No comments:

Post a Comment