Localized page via CmsSiteMapProvider

Posted by Community Admin on 05-Aug-2018 22:58

Localized page via CmsSiteMapProvider

All Replies

Posted by Community Admin on 05-Sep-2011 00:00

Dear team!

I've upgraded to 4.2 from 3.x (can't remeber the exact version). In 3.x I used the following syntax (in VB.NET) to get the localized node title and URL:

Dim ourMapProvider As Telerik.Cms.Web.CmsSiteMapProvider = SiteMap.Provider
Dim node As Telerik.Cms.Web.CmsSiteMapNode
 
node = ourMapProvider.FindSiteMapNodeFromKey("3aab4e5b-caf6-49c6-94a7-010d6e272f5a")
If Not Object.Equals(node, Nothing) Then
    lnkExperience.Text = node.Title
    lnkExperience.NavigateUrl = node.Url
End If

Now, in 4.2 this doesn't work. Well, it works a bit... I can get the correct page given the GUID is equal to the GUID in the table sf_page_data that represents the page I want.

In 3.x I got the correct localized version using the same GUID. In 4.x I need to have different GUID for every given language version.

The current code I use in C# looks like this:
if (System.Threading.Thread.CurrentThread.CurrentUICulture.Name == "sv")
            
                node = this.SiteMap.Provider.FindSiteMapNodeFromKey("4E0C3C26-449D-487E-BE2C-9EBC84E38CC5");
                if (!object.Equals(node, null)) lnkExperience.Text = node.Title; lnkExperience.NavigateUrl = node.Url;
            
            else
            
                node = this.SiteMap.Provider.FindSiteMapNodeFromKey("023A40A9-6AA2-469D-B9FB-E3E495AF6984");
                if (!object.Equals(node, null)) lnkExperience.Text = node.Title; lnkExperience.NavigateUrl = node.Url;
            

Is there a way to get the same functionality in 4.2 as in 3.x without the if-statement above?
Is there a nother GUID that I can use that is the same for every localized version of a given page?

I hope you understand what I mean...

Thanks in advance,
Fredrik

Posted by Community Admin on 08-Sep-2011 00:00

Hi Fredrik,

Unfortunately all language versions have their own PageNode object with its own GUID. When you use the SiteMap.Provider.FindSiteMapNodeFromKey method you get a reference to a System.Web.SiteMapNode. If you get a reference to a PageNode object it has a property named AvailableLanguages that contains a list of all the translations and then you may use some PageManager method to retrieve a language version for a specific page.

Best wishes,
Lubomir Velkov
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Posted by Community Admin on 24-Oct-2011 00:00

Hi Lubomir,

Which exactly methods of PageManager should I use to retrieve page data for different language versions?

Let's suppose I have two translation EN and RU for the home page. So, the page name is "home", the page title is "Home" for EN version and the page name is "Главная" with page title "Главная" for RU version. I would like to retrieve title and UrlName for both translations.

I tried to use the following code:

var configManager = Config.GetManager();
var config = configManager.GetSection<PagesConfig>();
 
//default root node
if (config.HomePageId != Guid.Empty)
    PageManagerpageManager = PageManager.GetManager();
     
    PageNode homePage = pageManager.GetPageNode(config.HomePageId);
    if (homePage != null)
    
        string urlEN = homePage.UrlName["en"];
        string urlRU = homePage.UrlName["ru"];
    

I expected to retrieve urlEN="home" and urlRU="главная". But I've got urlEN="home" and urlRU="home_ru".

What I do wrong? Any suggestion will be appreciated.

Thanks in advance,
Anton

Posted by Community Admin on 27-Oct-2011 00:00

Hello Anton Mernov,

You should use

PageManager.PagesLifecycle and use the methods that are in this LifecycleDecorator. This was introduced in SF 4.2 and is the new right way to handle lifecycle operations for cultures.

As for your second question - "Главная" might be an additional URL, while UrlName retrieves the main URL. To retrieve all the URLs use the pageNode.Urls properties.

Greetings,
Lubomir Velkov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 07-Feb-2012 00:00

Hi Velkov

Can you put one example on how to get the correct page node for a specific language using PageManager.PagesLifecycle as you said?

I could'nt find any method suitable to obtain the correct Url (ex: "/fr-CH/[pageTitle]")

Thanks in advance
PS: using version 4.4 if sitefinity

Posted by Community Admin on 09-Feb-2012 00:00

Hello Paulo,

First of all is your page in split mode or in sync mode? If it is in sync mode then all languages share the same PageNode object. You can get the page node like this -

var pageManager = PageManager.GetManager();
var node = pageManager.GetPageNodes().Where(p => p.Title == "MyPage").FirstOrDefault();

Then you can access various properties of the page node with

node.Title["en"]
node.Title["es"]

where "en" and "es" are the available translation codes.

If the pages are in split mode then you can get a reference to a specific page node the same way, only now you have different titles for each language version so you should be more specific in the LINQ where filter.

Greetings,
Lubomir Velkov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 09-Feb-2012 00:00

Hi Lubomir

Thanks for the tips.
I was able to find the other laguage nodes but i'm not sure that this wil work in every case:
the code used is below:

using(var sf = App.WorkWith())
  PageManager pm = PageManager.GetManager();
  var pgn = pm.GetPageNode(new Guid(idPageNode));
                             
  string tit = pgn.UrlName[Thread.CurrentThread.CurrentUICulture.Name];
  if(tit == null)
    return;
 
  var node = sf.Pages().Where(p => p.UrlName == tit.Replace("_" + Thread.CurrentThread.CurrentUICulture.Name,"")).Get().FirstOrDefault();
 
// The var node now has the translation node

Is this code the best way to achieve the result?

Thanks in advance.

Best regards
 

Posted by Community Admin on 14-Feb-2012 00:00

Hi Paulo,

Yes, the code looks good, however I wouldn't recommend mixing the using of the fluent API and the PageManager at the same time. If you get a reference to the PageManager you can replace this line of code

var node = sf.Pages().Where(p => p.UrlName == tit.Replace("_" + Thread.CurrentThread.CurrentUICulture.Name,"")).Get().FirstOrDefault();

with this

var node = pm.GetPageNodes().Where(p => p.UrlName == tit.Replace("_" + Thread.CurrentThread.CurrentUICulture.Name, "")).Get().FirstOrDefault();

Regards,
Lubomir Velkov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed