2

I'm trying to build metadata navigation programmatically so it can be deployed in a feature event receiver and have been using the code from this MSDN page, but am unable to get a simple link to work.

The code I'm running is unmodified from the example, except that the SPSite and SPWeb objects are coming from the feature event receiver properties, but whenever I get to this line

NavigationTerm term1 = navTermSet.CreateTerm("Term 1", NavigationLinkType.SimpleLink);

I get an ArgumentException with the message "The URL does not point to a page". Which is interesting because I haven't specified a URL for the link yet!

Has anyone got this working successfully in 2013 and can see where I've gone wrong?

EDIT - Full code

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    var site = (SPSite)properties.Feature.Parent;
    var web = site.RootWeb;

    var webNavSettings = new WebNavigationSettings(web);

    // Set basic nav settings
    webNavSettings.CurrentNavigation.Source = StandardNavigationSource.TaxonomyProvider;
    webNavSettings.AddNewPagesToNavigation = false;
    webNavSettings.CreateFriendlyUrlsForNewPages = false;

    // Set navigation term set
    TaxonomySession taxonomySession = new TaxonomySession(site, true);

    if (taxonomySession.TermStores.Count > 0)
    {
        TermStore termStore = taxonomySession.TermStores[0];
        TermSet existingTermSet = termStore.GetTermSet(NavTermSetId);

        if (existingTermSet != null)
        {
            existingTermSet.Delete();
            termStore.CommitAll();
        }

        Group siteCollectionGroup = termStore.GetSiteCollectionGroup(site);
        TermSet termSet = siteCollectionGroup.CreateTermSet("Navigation", NavTermSetId);

        NavigationTermSet navTermSet = NavigationTermSet.GetAsResolvedByWeb(
            termSet, web, StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider);
        navTermSet.IsNavigationTermSet = true;
        navTermSet.TargetUrlForChildTerms.Value = "~site/default.aspx";
        termStore.CommitAll();

        NavigationTerm term1 = navTermSet.CreateTerm("Home", NavigationLinkType.SimpleLink);
        term1.SimpleLinkUrl = "~site/default.aspx";

        termStore.CommitAll();
    }
    webNavSettings.Update();
}
  • Is it the same if you use FriendlyUrl like var newNavTerm = navTermSetItem.CreateTerm(term.Name, NavigationLinkType.FriendlyUrl); – Robert Lindgren Jul 31 '13 at 11:21
  • This is really strange, you use the exact same code that I do (successfully) except you are creating a SimpleLink instead of a FriendlyUrl. But that is not even where your code is throwing errors. and the /default.aspx URL is valid I guess? – Robert Lindgren Jul 31 '13 at 12:32
  • Assuming ~site is expanding correctly, yes. I've tried it with a full URL, but it does't like that either. – Mourndark Jul 31 '13 at 12:37
2

Have you set TargetUrlForChildTerms?

I have the following code working:

TermSet newTermSet = siteCollectionGroup.CreateTermSet("Term set", NavTermSetId);
NavigationTermSet navTermSet = NavigationTermSet.GetAsResolvedByWeb(newTermSet, parentWeb,
                        StandardNavigationProviderNames.CurrentNavigationTaxonomyProvider);
navTermSet.IsNavigationTermSet = true;
navTermSet.TargetUrlForChildTerms.Value = "~site/Pages/default.aspx";
ts.CommitAll();

and then adding nodes:

.... // for eaching over the structure I create pages and nav terms for
navTermSet.CreateTerm(term.Name, NavigationLinkType.FriendlyUrl);
....
ts.CommitAll();
  • What is the type of ts? – Mourndark Jul 31 '13 at 12:00
  • Sorry, that is my TermStore. – Robert Lindgren Jul 31 '13 at 12:01
  • OK, I've added navTermSet.TargetUrlForChildTerms.Value = "~site/default.aspx"; (~site/Pages/default.aspx does not exist) but am now getting the same ArgumentException from that line instead. – Mourndark Jul 31 '13 at 12:10
  • Weird, can you post your full code so we can take a closer look? – Robert Lindgren Jul 31 '13 at 12:13
  • Posted in original question. – Mourndark Jul 31 '13 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.