3

I'm looking for a way to retrieve the alternative URL value for a page in kentico.

Having followed this example to enable Alternative Urls in Kentico 12 SP : https://docs.kentico.com/k12sp/developing-websites/configuring-page-urls-on-mvc-sites/enabling-alternative-urls-for-pages

I'm now stuck trying to access the value for the alternative URL programmatically. Any pointers towards how I may get the value would be greatly appreciated.

Timtek
  • 65
  • 1
  • 7

2 Answers2

3
var altLink = new DataQuery()
                .From("CMS_AlternativeURL")
                .Where("AlternativeUrlUrl = @URL", new QueryDataParameters { new 
DataParameter("@URL", altURL) })
                .Execute()
                ?.Tables[0]
                ?.AsEnumerable();

            if (altLink != null)
            {
                var altLinkFirst = altLink
                    .ToList()
                    .FirstOrDefault();
                page = new TreeProvider()
                    .SelectSingleDocument(altLinkFirst.Field<int>("AlternativeUrlDocumentID"));

So CMS_AlternativeURL is the table with the pairings of alturl and url. I then pass in a string as the alias path, try finding it the treeprovider and if I cannot find it I run this which links the alternative url (the given string) and it will return back pairing with documentId if it exists.

if you're instead trying to get the alternative urls from a treenode, run a dataquery where AlternativeUrlDocumentID = the treennode's documentid.

graham
  • 91
  • 3
  • 1
    thank you so much for the quick reply, i'll try this out and mark as correct if i can get it to work :) – Timtek Feb 20 '20 at 15:49
3

Have you tried using AlternativeUrlInfoProvider class?

You can do something like this is if you have the page:

   var altUrl = AlternativeUrlInfoProvider.GetAlternativeUrls()
                .Where("AlternativeUrlDocumentID", QueryOperator.Equals, page.DocumentID).FirstOrDefault();
Zach Perry
  • 746
  • 3
  • 12
  • While the answer below from @graham worked for us, this answer is much more succinct, and slightly more performant from our testing. – Timtek Feb 21 '20 at 09:19