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
Syntax: 
AddPath:
stsadm -o addpath -url <managed path URL followed by web app URL> -type <explicitinclusion or wildcardinclusion>
E.g.:
stsadm -o addpath -url http://sharepoint.company.com/teams/ -type wildcardinclusion
Create/Delete/Get Managed Paths in SharePoint Programmatically

DeletePath:
Syntax:
stsadm -o deletepath -url <Managed Path URL with Web App>
E.g.
stsadm -o deletepath -url http://sharepoint.company.com/teams/

Technet Reference:http://technet.microsoft.com/en-us/library/cc263161%28v=office.12%29.aspx


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 "http://sharepoint.company.com" –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 "http://sharepoint.company.com" –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 "http://sharepoint.company.com"

Reference: http://technet.microsoft.com/en-us/library/cc261845%28v=office.14%29.aspx

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 "http://sharepoint.company.com" - 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:"
 $WebAppURL=read-host
 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
 }
 else
 {
    Write-Host "Managed path $ManagedPath already exists!"
 }

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

string webAppURL = "http://sharepoint.company.com";
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.
Five Challenges in SharePoint Security
...And How to Solve Them. Free White Paper
*Sponsored


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...