Programmatically create Managed Paths 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:

  1. Explicit inclusion.

  2. 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 pathsProgrammatically get all the managed paths for a web application


Programmatically create managed paths:

  • Open Visual Studio 2010.

  • Go to File => New => Project.

  • Select the console application template.

  • Add the following references.

i) Microsoft.SharePoint

  • Add the following namespaces.

i) using Microsoft.SharePoint;

ii) using Microsoft.SharePoint.Administration;

  • Replace Program.cs with the following code.

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using Microsoft.SharePoint.Administration; 

  7. namespace ManagedPath

  8. {

  9. class Program   

  10. {

  11. static void Main(string[] args)       

  12. {

  13. string path = "Bangalore";

  14. SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://serverName:1111/"));

  15. SPPrefixCollection prefixColl = webApp.Prefixes;

  16. if (prefixColl.Contains(path) == false)            

  17. {

  18. SPPrefix newPrefix = webApp.Prefixes.Add(path, SPPrefixType.ExplicitInclusion);

  19. Console.WriteLine(path+" is successfully added to the web application");           

  20. }

  21. else           

  22. {

  23. Console.WriteLine(path + " already exists in the web application");           

  24. }

  25. Console.ReadLine();       

  26. }

  27. }

  28. }

  • Build the solution.

  • Hit F5.


Output:

  • Go to Central Administration => Application Management => Manage Web Applications.

  • Select the web application, click on Managed Paths available in the ribbon interface.


  • "Bangalore" managed path is created successfully.

Programmatically get all the managed paths for a web application: Code:

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using Microsoft.SharePoint.Administration; 

  7. namespace ManagedPath

  8. {

  9. class Program   

  10. {

  11. static void Main(string[] args)       

  12. {

  13. SPWebApplication webApp = SPWebApplication.Lookup(new Uri("http://serverName:1111/"));

  14. SPPrefixCollection prefixColl = webApp.Prefixes;

  15. foreach (SPPrefix prefix in prefixColl)           

  16. {

  17. Console.WriteLine("Managed Path :{0}, Prefix Type :{1}", prefix.Name, prefix.PrefixType)           

  18. }

  19. Console.ReadLine();       

  20. }   

  21. }

  22. }

Output:


To create, get and delete the managed paths for a web application using PowerShell refer

http://www.c-sharpcorner.com/uploadfile/anavijai/5355/.

Recent Posts

See All