Getting different published content to a common page
I have a home page (HomeController inherits from RenderMvcController) where I want to list published content from two different document types.
I've been searching and searching for help on how I can access the published content of each document type through the UmbracoHelper in the controller, but found nothing.
So I would appreciate any help on how this is best achieved?
You need the DocumentTypeAlias for each document type.
If the document types exist under a specific list then it is more efficient to target the parent and then use linq to loop over the child nodes and return the ones that match. If they live in different places within the content tree then you will have to iterate down from the root content node. I have included a code snippet for scenario one.
UmbracoHelper documentation is pretty good. For the scenario where the types live under a list this should work.
var helper = new UmbracoHelper(UmbracoContext.Current);
var rootnode = helper.TypedContent(LIST_PARENT_NODE_ID);
var matchingnodes = rootnode.Children.Where(c => c.DocumentTypeAlias == "DOCUMENT_TYPE_ALIAS");
The TypedContent method accepts a integer which represents the Content NodeID. Every page you create in Umbraco is assigned a unique NodeID. You can get the ID for the properties tab or info tab depending on which version of Umbraco 7.X.X
In my code snippet LISTPARENTNODE_ID is just the NodeID for the list page. For example if you had a page called blog (list) that has child blog articles then you would pass the TypedContent method the NodeID for the blog (list) page then loop over the child blog articles and return them as a collection of IPublishedContent objects to use for display on the page.
I'm hitting another problem but it's not really related to my initial question.
Since all objects are of IPublishedContent and the properties I need are on concrete classes found on runtime in Umbraco.Web.PublishedContentModels.MyDocType, I don't know how I can access those properties on class and not the interface?
You just need to cast your IPublishedContent into a concreate type. You can do this with linq as long as you setup the MyDocType class in your Model to inherit from PublishedContentModel. If you are allowing Umbraco to autogenerate the model you should be able to implement this using a parital class with the same name. You will need to use the same namespace on your class as Umbraco e.g. Umbraco.Web.PublishedContentModels
Below is the Linq you need.
var matchingnodes = rootnode.Children.Where(c => c.DocumentTypeAlias.Equals("DOCUMENT_TYPE_ALIAS")).OrderBy(x => x.Name).Select(t => new MyDocType(t));
If the document types are descendants of your Home Controller (which they almost certainly are) then you can do this in your render controller (assuming "yourAlias" is the alias of the document type you wish to find and there is only one of them):
var page = CurrentPage.Descendant("yourAlias");
That will return you a single IPublishedContent instance of the first page with an alias of "yourAlias" that is a descendant of your home page.
If your page is a child page (ie. exactly one level deeper than the home page) then this will be more efficient:
var page = CurrentPage.Children("yourAlias");
If you need to get multiple document types you can use:
var pages = CurrentPage.Descendants("yourAlias");
This will return IEnumerable<IPublishedContent>
If it's a specific page you require and you know the ID and don't mind hardcoding this then you can also do:
var page = Umbraco.TypedContent(1234);
(Where 1234 is the ID of the page).
Note: In a render controller you already have access to the UmbracoHelper and other contexts - you don't need to recreate these.
Getting different published content to a common page
I have a home page (HomeController inherits from RenderMvcController) where I want to list published content from two different document types.
I've been searching and searching for help on how I can access the published content of each document type through the UmbracoHelper in the controller, but found nothing.
So I would appreciate any help on how this is best achieved?
My level is Umbraco newb, be gentle. :)
Hi Johan,
UmbracoHelper documentation is pretty good. For the scenario where the types live under a list this should work.
I hope this helps
Hi David and thanks for helping out!
One question, what is
LIST_PARENT_NODE_ID
? I understand that it is a guid but what does it represent and where does it come from?Hi,
The TypedContent method accepts a integer which represents the Content NodeID. Every page you create in Umbraco is assigned a unique NodeID. You can get the ID for the properties tab or info tab depending on which version of Umbraco 7.X.X
In my code snippet LISTPARENTNODE_ID is just the NodeID for the list page. For example if you had a page called blog (list) that has child blog articles then you would pass the TypedContent method the NodeID for the blog (list) page then loop over the child blog articles and return them as a collection of IPublishedContent objects to use for display on the page.
Okay thanks David! It works.
I'm hitting another problem but it's not really related to my initial question.
Since all objects are of
IPublishedContent
and the properties I need are on concrete classes found on runtime inUmbraco.Web.PublishedContentModels.MyDocType
, I don't know how I can access those properties on class and not the interface?Hi,
You just need to cast your IPublishedContent into a concreate type. You can do this with linq as long as you setup the MyDocType class in your Model to inherit from PublishedContentModel. If you are allowing Umbraco to autogenerate the model you should be able to implement this using a parital class with the same name. You will need to use the same namespace on your class as Umbraco e.g. Umbraco.Web.PublishedContentModels
Below is the Linq you need.
Or you can just use the generic overload...
I think you can make the queries simpler...
If the document types are descendants of your Home Controller (which they almost certainly are) then you can do this in your render controller (assuming "yourAlias" is the alias of the document type you wish to find and there is only one of them):
That will return you a single IPublishedContent instance of the first page with an alias of "yourAlias" that is a descendant of your home page.
If your page is a child page (ie. exactly one level deeper than the home page) then this will be more efficient:
If you need to get multiple document types you can use:
This will return
IEnumerable<IPublishedContent>
If it's a specific page you require and you know the ID and don't mind hardcoding this then you can also do:
(Where 1234 is the ID of the page).
Note: In a render controller you already have access to the UmbracoHelper and other contexts - you don't need to recreate these.
is working on a reply...