I'm trying to write a MacroPartial that will allow me to pull out content from every item on my website that has a specific doctype.
I'm building a blog for a friend and it will have different sections, but every article will have the same doctype. For the homepage I want to be able to select every article from every section, but I'm struggling to figure it out.
You could try something like this (I don't have an IDE open, so this may include some mistakes, so consider it more as pseudo-code):
var articles = Model.Content
// This gets the node at the first level (i.e., the homepage node).
.AncestorOrSelf(1)
// This gets all descendants of the specified doctype.
.Descendants("SomeArticleDocType");
That will be slow, so you'll want to perform some caching (perhaps in a static variable).
Thank you for your reply. I've implemented your code, and while I'm no longer getting an error message, it just doesn't return any content? Maybe I'm just being blind so I'll add a screenshot + my code
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{ var home = @CurrentPage.Site();
var postNodes = home.AncestorOrSelf().Descendants("Article"); }
@foreach (var item in postNodes)
{
<h3>@(item.GetPropertyValue("Title"))</h3>
}
Thank you for your help. I've made the change you supplied but I'm still getting absolutely no content? Been staring at this for 45 minutes and can't see what the issue may be :(
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{ var home = CurrentPage.Site();
var postNodes = home.Descendants("Article"); }
@foreach (var item in postNodes)
{
<h3>@item.title</h3>
}
Again I've implemented the code but still no output. I've looked over the code a thousand times and I still can't understand why it's pulling absolutely nothing from the article pages I've set up!
Just test this code in Umbraco 7.4.3 and this is working:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
var home = Umbraco.TypedContentAtRoot().FirstOrDefault();
var articles = home.Descendants("article");
}
@foreach (var item in articles)
{
<h3>@item.Id</h3>
}
Here no dynamic objects and please check alias of your article document type.
Could it be something as simple as a casing issue? I noticed that in your code you wrote home.Descendants("Article") but Alex wrote home.Descendants("article") in the above example. Have your tried that? DocumentTypes usally starts with a small letter.
Glad it worked out for you. This is exacly the kind of thing that one can go crazy from when one has been staring at a peace of code forever and turns out it something as simple as a casing issue. :)
Get All Articles in Umbraco
Hello everyone,
I'm trying to write a MacroPartial that will allow me to pull out content from every item on my website that has a specific doctype.
I'm building a blog for a friend and it will have different sections, but every article will have the same doctype. For the homepage I want to be able to select every article from every section, but I'm struggling to figure it out.
Any ideas? Thanks guys!
Hi Jedd,
You can do it with this code:
Thanks,
Alex
You could try something like this (I don't have an IDE open, so this may include some mistakes, so consider it more as pseudo-code):
That will be slow, so you'll want to perform some caching (perhaps in a static variable).
Hi Alex,
Thank you for your reply. I've implemented your code, and while I'm no longer getting an error message, it just doesn't return any content? Maybe I'm just being blind so I'll add a screenshot + my code
Any idea what I'm doing wrong?
Thanks Alex,
-Jedd
Hi Jedd,
You are using the dynamic Razor you you need to change
To
Hope this helps,
/Dennis
Hi Dennis,
Thank you for your help. I've made the change you supplied but I'm still getting absolutely no content? Been staring at this for 45 minutes and can't see what the issue may be :(
Thanks,
Jedd
Hi Jedd,
Can you debug your foreach?
Or just try to write ids:
Maybe 'postNodes' is empty?
Thanks,
Alex
Hi Jedd,
Could you please try this:
Hope this helps,
/Dennis
Hi Dennis,
Again I've implemented the code but still no output. I've looked over the code a thousand times and I still can't understand why it's pulling absolutely nothing from the article pages I've set up!
Thanks,
Jedd
Hi Jedd,
This code should give you the name of all your articles.
If you donĀ“t get anything out, then please double check that your alias of your document type for the articles is Article.
Normally when I debug code, then I am trying to do it in steps, so what you could try is to out put the home variable by do this:
Did you then get the name of the home out? If so then you can add the next variable and see if you still get data out.
Hope this helps,
/Dennis
Hi Dennis and Jedd,
Just test this code in Umbraco 7.4.3 and this is working:
Here no dynamic objects and please check alias of your article document type.
Thanks,
Alex
Could it be something as simple as a casing issue? I noticed that in your code you wrote
home.Descendants("Article")
but Alex wrotehome.Descendants("article")
in the above example. Have your tried that? DocumentTypes usally starts with a small letter.Hello everyone,
Thank you all very much for your help, I think it may have just been a casing issue after all. I've managed to get the macro working now.
Thanks again!
Jedd
Glad it worked out for you. This is exacly the kind of thing that one can go crazy from when one has been staring at a peace of code forever and turns out it something as simple as a casing issue. :)
Have a great weekend! Take care!
is working on a reply...