Sunday, December 9, 2012

Create - Delete - Get Managed Paths in SharePoint Programmatically

SharePoint Managed Paths can be manipulated programmatically with SharePoint object model (.Net / PowerShell.) or with STSADM command line tool.

Using STSADM Tool to Add/Remove Managed Path:

These two stsadm command line tool switches available to Add/Remove Managed paths in SharePoint.
  • Stsadm -o AddPath
  • Stsadm -o DeletePath
stsadm -o addpath -url <managed path URL followed by web app URL> -type <explicitinclusion or wildcardinclusion>
stsadm -o addpath -url -type wildcardinclusion
Create/Delete/Get Managed Paths in SharePoint Programmatically

stsadm -o deletepath -url <Managed Path URL with Web App>
stsadm -o deletepath -url

Technet Reference:

Create/Delete/Get Managed Paths with PowerShell:

Create a New Managed Path
Use New-SPManagedPath cmdlet to create a new managed path in a web application on the SharePoint farm.

New-SPManagedPath –WebApplication "" –RelativeURL "teams" 

By default, the managed path is created as a wildcard inclusion type. You can include the
Explicit switch to create a managed path of explicit inclusion type.

New-SPManagedPath –WebApplication "" –RelativeURL
"/departments/" -Explicit

Remove a Managed Path
If you need to delete a managed path from a specific web application: use Remove-SPManagedPath cmdlet.
Remove-SPManagedPath -Identity "Teams" –WebApplication ""


Check whether a Particular Managed Path Exists:
PowerShell offers one more cmdlet: Get-SPManagedPath to check the existence of a managed path. Say, for e.g. you may want to check whether a particular managed path exists or not before creating managed path. 

Get Managed Paths
Get-SPManagedPath -WebApplication "" - Get-SPManagedPath displays all managed paths for the given web application.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

 write-host "Enter the Web Application URL:"
 write-host "Enter the Managed Path:" 
 $ManagedPath =read-host

 #Check whether the particular managed path exist already
 $ManagedPathExists = Get-SPManagedPath -WebApplication $WebAppURL -Identity $ManagedPath -ErrorAction SilentlyContinue

 if ($ManagedPathExists -eq $null)
    #Go ahead and create the managed path
    New-SPManagedPath –RelativeURL $ManagedPath -WebApplication $WebAppURL
    Write-Host "Managed path $ManagedPath already exists!"

You can Create Managed Paths in C# Object Model as:

string webAppURL = "";
string RequiredManagedPath ="teams";

//Get the web app
SPWebApplication WebApp = SPWebApplication.Lookup(new Uri(webAppURL));

//Get the Managed Paths of the web app
SPPrefixCollection Prefixes = WebApp.Prefixes;

   //Check If it doesn't exists!
   if (Prefixes.Contains(RequiredManagedPath) == false)
           //Create the managed Path  
    SPPrefix prefix = WebApp.Prefixes.Add(RequiredManagedPath, SPPrefixType.ExplicitInclusion);

Tail: What if I delete the Managed path in use?
Answer: Your SharePoint Sites under the specific managed path will result: HTTP 404 Page Not Found! Refer my post: SharePoint Managed Paths - FAQs - If you have any questions on SharePoint Managed paths!

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...