Get page urls in all languages

Victor G asked on June 8, 2015 17:51

Hi,

I need to get all the urls for the current page for all the enabled languages on the site. How can I do this in a custom Web Part?

Thanks

Best, Victor

Correct Answer

Josef Dvorak answered on June 27, 2015 03:53

Hello,

Regrettably we found that this was a bug, which has been resolved in Hotfix package 8.2.26.

If necessary, it is also possible to build the URL manually, like this:

CultureInfo culture = CultureInfoProvider.GetCultureInfo(cult.DocumentCulture);
if (culture == null)
{
    throw new Exception("Page has no culture information");
}

string code = (String.IsNullOrEmpty(culture.CultureAlias)) ? culture.CultureCode : culture.CultureAlias;

string url = URLHelper.GetAbsoluteUrl(DocumentURLProvider.GetUrl(cult.NodeAliasPath, cult.DocumentUrlPath, null, code), null);
0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on June 9, 2015 13:30

Are you talking about the URLs rendered in the content on the page or just URL for the current page?

0 votesVote for this answer Mark as a Correct answer

Victor G answered on June 9, 2015 13:41

Hi Brenden,

I am referring just to the URL of the current page.

Best, Victor

0 votesVote for this answer Mark as a Correct answer

Victor G answered on June 9, 2015 15:32

Hi,

I've tried a little bit more and I come up with following code:

Code
    var items = CMS.DocumentEngine.DocumentHelper.GetDocuments().AllCultures(true).Where(p => p.NodeID == CMS.DocumentEngine.DocumentContext.CurrentDocument.NodeID).ToArray();
    if (items != null)
    {
        foreach (var cult in items)
        {
            lbl.Text += "<br />" + cult.DocumentCulture + ": " + cult.AbsoluteURL;
        }
    }

This returns:

en-GB: http://localhost/en/Help
de-DE: http://localhost/en/Hilfe
pl-PL: http://localhost/en/Help

The problem is that the generated URLs contain the wrong language code ("en"). If I execute the same code when the page is in german than I get all the URLs with the german code ("de"). Is this a bug? Is there other possibility to get the right URLs?

Thanks

0 votesVote for this answer Mark as a Correct answer

Victor G answered on June 18, 2015 10:30 (last edited on June 18, 2015 10:31)

Problem solved after contacting the support.

You can get the correct URL by using:

var culture = CMS.Localization.CultureInfoProvider.GetCultureInfo(document.DocumentCulture);
string code = (String.IsNullOrEmpty(culture.CultureAlias)) ? culture.CultureCode : culture.CultureAlias;
            CMS.Helpers.URLHelper.GetAbsoluteUrl(CMS.DocumentEngine.DocumentURLProvider.GetUrl(document.NodeAliasPath, document.DocumentUrlPath, null, code), null)
//where 'document' is a Kentico Document/Node
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.