Timtek
Timtek

Reputation: 65

Access Kentico 12 Alternative URL value

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.

Upvotes: 3

Views: 466

Answers (2)

Zach Perry
Zach Perry

Reputation: 756

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();

Upvotes: 4

graham
graham

Reputation: 91

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.

Upvotes: 3

Related Questions