Accessing the language / culture of a page from the Umbraco content service
Hi all,
We are in the process of producing another automated website that is driven by our external desktop software an I have some questions regarding Umbraco multi-language support.
In previous projects we have only dealt with one language and we handle the creation of new pages by traversing through the Umbraco tree structure based upon a pages relation to the root/homepage node.
However, in this new project we will have multiple homepages in our Umbraco installation as we will be making use of the cultures and hostnames functionality.
My question is, how can I seperate and identify each of the homepages using the Umbraco content service when used in an API controller.
Currently I do the following the get the homepage:
Now I understand that GetRootContent naturally returns an
IEnumerable<IContent>
so I have tried adapting the code to the following and accessing the language property of each of the pages as so:
IEnumerable<IContent> umb_homepages = ApplicationContext.Current.Services.ContentService.GetRootContent();
foreach(IContent homepage in umb_homepages){
string language = homepage.Language;
}
However, when I run this in debug mode in Visual studio and stick a breakpoint in my foreach, I find that language is coming through as Null.
I have ensured that my culture and hostnames values of each of the homepage nodes in my Umbraco installation are set so why am I not getting anything returned when accessing this language property?
Is there anything else I can do to differentiate between the two (or more) pages?
I had a requirment to get the home page from within WebApi, and after a bit of rummaging around the only simple solution I had was to access some of the internal functionality of Umbraco using reflection. I did release a package that contains this code, called AutoRouteTemplate, but if you want the code within it looks like this.
Note: You might notice that anyrepeated requests are cached, which can make it faster than using other methods when being used outside a WebApi - Like a standard razor page, for example
Please, please, please do not use the ContentService for querying. It's going straight to the database and should ONLY be used when you need to create, update or delete data.
In 7.2.3/7.2.4 you can simple do the following:
UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
var items = helper.TypedContentAtRoot();
items.First().GetCulture();
Of course, if you need anything but the First() you can do something like: items.First(x => x.Name == "UK").GetCulture() or something like that.
I am creating content though and I've been using this methodology since Umbraco 7 was introduced. I select my homepage as a reference point then traverse the tree to find where to create the content from our web services.
Okay, but you only need to use the ContentService for querying if you need to find unpublished content. You can still use the above method for published content.
I'm afraid that method doesn't even work. In Visual Studio I get the following error:
Error 1 'Umbraco.Core.Models.IPublishedContent' does not contain a
definition for 'GetCulture' and no extension method 'GetCulture'
accepting a first argument of type
'Umbraco.Core.Models.IPublishedContent' could be found (are you
missing a using directive or an assembly reference?)
That might be why. For some reason the version I have is 7.2.1 despite only pulling it down from NuGet the other day. Is this something that was fixed recently?
Sometimes you get a version from your local cache, you should try to clear it. It's usually located in C:\Users\<your_username>\AppData\Local\NuGet\Cache.
Well, I thought this was the solution but it turns out that it doesn't work.
If I use Model.Content.GetCulture() on a page that is assigned a language of "is-IS" then it is showing up as "en-US" when it is output to the page.
Accessing the language / culture of a page from the Umbraco content service
Hi all,
We are in the process of producing another automated website that is driven by our external desktop software an I have some questions regarding Umbraco multi-language support.
In previous projects we have only dealt with one language and we handle the creation of new pages by traversing through the Umbraco tree structure based upon a pages relation to the root/homepage node.
However, in this new project we will have multiple homepages in our Umbraco installation as we will be making use of the cultures and hostnames functionality.
My question is, how can I seperate and identify each of the homepages using the Umbraco content service when used in an API controller.
Currently I do the following the get the homepage:
Now I understand that GetRootContent naturally returns an
so I have tried adapting the code to the following and accessing the language property of each of the pages as so:
However, when I run this in debug mode in Visual studio and stick a breakpoint in my foreach, I find that language is coming through as Null.
I have ensured that my culture and hostnames values of each of the homepage nodes in my Umbraco installation are set so why am I not getting anything returned when accessing this language property?
Is there anything else I can do to differentiate between the two (or more) pages?
Any help would be greatly appreciated.
I have also tried the following:
However, this results in a:
I had a requirment to get the home page from within WebApi, and after a bit of rummaging around the only simple solution I had was to access some of the internal functionality of Umbraco using reflection. I did release a package that contains this code, called AutoRouteTemplate, but if you want the code within it looks like this.
Note: You might notice that anyrepeated requests are cached, which can make it faster than using other methods when being used outside a WebApi - Like a standard razor page, for example
}
Hope this helps.
Please, please, please do not use the ContentService for querying. It's going straight to the database and should ONLY be used when you need to create, update or delete data.
In 7.2.3/7.2.4 you can simple do the following:
Of course, if you need anything but the
First()
you can do something like:items.First(x => x.Name == "UK").GetCulture()
or something like that.I am creating content though and I've been using this methodology since Umbraco 7 was introduced. I select my homepage as a reference point then traverse the tree to find where to create the content from our web services.
Okay, but you only need to use the ContentService for querying if you need to find unpublished content. You can still use the above method for published content.
Hi Sebastiaan,
I'm afraid that method doesn't even work. In Visual Studio I get the following error:
This is a new extension method in 7.2.3 and 7.2.4 though, are you using an older version?
That might be why. For some reason the version I have is 7.2.1 despite only pulling it down from NuGet the other day. Is this something that was fixed recently?
Oh..
Update-Package UmbracoCms
:-)Sometimes you get a version from your local cache, you should try to clear it. It's usually located in
C:\Users\<your_username>\AppData\Local\NuGet\Cache.
Thanks for your help with this Sebastiaan. It seems to be working. Just a quick question about the UmbracoHelper method though:
https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/64377-Get-ancestor-at-level-one-using-the-UmbracoHelper-method
Well, I thought this was the solution but it turns out that it doesn't work. If I use Model.Content.GetCulture() on a page that is assigned a language of "is-IS" then it is showing up as "en-US" when it is output to the page.
is working on a reply...