1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Issue with PageSiteNode GetURL function when providing CultureInfo parameter

Issue with PageSiteNode GetURL function when providing CultureInfo parameter

6 posts, 1 answered
  1. Chris
    Chris avatar
    12 posts
    Registered:
    10 Aug 2010
    22 Jan 2013
    Link to this post
    Hi. We have a Sitefinity 5.2 site with multiple cultures (en and en-GB) enabled.

    I am trying to perform what should be the simple task of retrieving the URL of each language version of the current page. 

    The code below doesn't seem to work. 

    For instance on the home page currentPageSiteNode.GetUrl(availableCultureInfo, true) always returns ~/home regardless of the provided CultureInfo object. I would expect to see something like:

    ~/home
    ~/en-GB/home

    My test code is below

    PageSiteNode currentPageSiteNode = SiteMapBase.GetActualCurrentNode();
    System.Globalization.CultureInfo[] availableCultures = currentPageSiteNode.AvailableLanguages;
     
    string Html = String.Empty;
     
    foreach(System.Globalization.CultureInfo availableCultureInfo in availableCultures){
     
        Html += RouteHelper.GetAbsoluteUrl(currentPageSiteNode.GetUrl(availableCultureInfo, true)) + "<br />" + Environment.NewLine;
     
    }
     
    Response.Write(Html);

    Any help or a workaround would be much appreciated.

    Thanks.
  2. Arno
    Arno avatar
    249 posts
    Registered:
    08 Sep 2010
    22 Jan 2013 in reply to Chris
    Link to this post

    Hi Chris,

    I'm currently using the code below in a multilingual 5.3 site. It's VB.NET, you can translate it to C# here.

    Public Function SitefinityGetVirtualURLofPage(PageID As Guid, Culture As System.Globalization.CultureInfo) As String
                Dim ReturnValue As String = Nothing
     
                Try
                    Dim UrlLocalizationService As Telerik.Sitefinity.Localization.UrlLocalizationStrategies.UrlLocalizationService = Telerik.Sitefinity.Abstractions.ObjectFactory.Resolve(Of Telerik.Sitefinity.Localization.UrlLocalizationStrategies.UrlLocalizationService)()
     
                    If (UrlLocalizationService IsNot Nothing) Then
                        Dim PageManager As Telerik.Sitefinity.Modules.Pages.PageManager = Telerik.Sitefinity.Modules.Pages.PageManager.GetManager()
                        Dim PageNode As Telerik.Sitefinity.Pages.Model.PageNode = Nothing
     
                        Try
                            PageNode = PageManager.GetPageNode(PageID)
                        Catch ex As Exception
                            ReturnValue = Nothing
                        End Try
     
                        If (PageNode IsNot Nothing) Then
                            ReturnValue = UrlLocalizationService.ResolvePageUrl(PageNode, Culture)
                        End If
                    End If
     
                Catch ex As Telerik.Sitefinity.SitefinityExceptions.ItemNotFoundException
                    ' Log the error here
     
                    ReturnValue = Nothing
                Catch ex As Exception
                    ' Log the error here
     
                    ReturnValue = Nothing
                End Try
     
                Return ReturnValue
            End Function

    I hope it's useful.

    Answered
  3. Chris
    Chris avatar
    12 posts
    Registered:
    10 Aug 2010
    22 Jan 2013 in reply to Arno
    Link to this post
    Thanks Arno. I appreciate the assistance. I'll give it a try this afternoon and report back.
  4. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    22 Jan 2013 in reply to Chris
    Link to this post

    Hi Chris,

    For my projects I use this function:

    /// <summary>
            /// Get a page url
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            public static string GetPageUrl(Guid id) {
     
                CultureInfo currentLanguage = Thread.CurrentThread.CurrentUICulture;
                UrlLocalizationService s = ObjectFactory.Resolve<UrlLocalizationService>();
                var pm = PageManager.GetManager();
                try {
                    return s.ResolvePageUrl(pm.GetPageNode(id), currentLanguage);
                } catch (ItemNotFoundException) {
     
                    return string.Empty;
                }
            }

    Regards,
    Daniel

  5. Chris
    Chris avatar
    12 posts
    Registered:
    10 Aug 2010
    22 Jan 2013 in reply to Arno
    Link to this post

    Arno - Your solution works brilliantly for me thank you for posting!

  6. Chris
    Chris avatar
    12 posts
    Registered:
    10 Aug 2010
    22 Jan 2013 in reply to Daniel Plomp
    Link to this post

    Thank you Daniel - I'll give your code a try as well. I really appreciate your input.

6 posts, 1 answered