Localized page via CmsSiteMapProvider
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
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;
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,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 >>
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)
PageManager
pageManager = PageManager.GetManager();
PageNode homePage = pageManager.GetPageNode(config.HomePageId);
if (homePage != null)
string urlEN = homePage.UrlName["en"];
string urlRU = homePage.UrlName["ru"];
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.
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
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();
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
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();