Sharepoint 2013 introduced the concept of navigation based on Managed Metadata. This comes as a part of Sharepoint Publishing Infrastruture. There are enough posts explaining how to configure Metadata Navigation and the basic concepts behind it. Please use the below links to go through the basics
First let us see how we can create a Friendly URL for a publishing page programatically using the API
Althought the code is self explanatory, let me explain a few important point
Happy Coding….
- http://social.technet.microsoft.com/wiki/contents/articles/15876.sharepoint-2013-navigation-using-managed-metadata.aspx
- http://sadomovalex.blogspot.de/2013/03/managed-metadata-navigation-and.html
First let us see how we can create a Friendly URL for a publishing page programatically using the API
///<summary>
/// Creates a Friendly URL for a Publishing Page
/// </summary>
/// <param
name="contextWeb">SPWeb
Object which contains Publishing Page</param>
/// <param
name="parentName">Parent
Navigation Term</param>
/// <param
name="page">Publishing
Page for which the Friendly URL is added</param>
/// <param
name="friendlyName">Friendly
Name for the Page</param>
/// <param
name="AddToNavigation">Flag to indicate whether to add this Navigation Term to Quick Launch and
Top Naviagation</param>
/// <returns></returns>
public static string AddFriendlyUrl(SPWeb
contextWeb, string parentName, PublishingPage page, string
friendlyName,bool AddToNavigation)
{
string relativeUrl = string.Empty;
try
{
// create Taxonomy Session for the context web
TaxonomySession taxonomySession
= TaxonomyNavigation.CreateTaxonomySessionForEdit(contextWeb);
// get the current Navigation Term Set
NavigationTermSet currNavTermSet
= TaxonomyNavigation.GetTermSetForWeb(contextWeb,
StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider,
true);
// Make the Term Set Editable
NavigationTermSet editableTermSet
= currNavTermSet.GetAsEditable(taxonomySession);
// Get the Parent Term using the Parent Name Parameter matching the term
Label
TermCollection parentTerms =
editableTermSet.GetTaxonomyTermSet().GetTerms(parentName, true, StringMatchOption.ExactMatch,
1, false);
// if a matching parent Term exists get a refernce to it
if (parentTerms.Count > 0)
{
Term parentTerm = parentTerms[0];
// get the navigation Term Corresponding to the Parent Term
NavigationTerm newTerm = NavigationTerm.GetAsResolvedByWeb(parentTerm,
contextWeb, StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider);
// make the parent Term editable
NavigationTerm editNewTerm =
newTerm.GetAsEditable(taxonomySession);
// create the new FriendlyUrl along with the new Navigation Term
relativeUrl
= page.AddFriendlyUrl(friendlyName, editNewTerm, AddToNavigation);
}
}
catch
{
throw;
}
return relativeUrl;
}
Althought the code is self explanatory, let me explain a few important point
- Since we are going to add new navigation term we have to make the current Term Set as Editable using currNavTermSet.GetAsEditable(taxonomySession);
- The SitemapProviderName has to be StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider
- The parent navigation term should also be made editable
Happy Coding….
No comments:
Post a Comment