I have a node called NewsArea that has NewsItem child nodes in a Macro Partial that is rendered in the NewsArea template.
My code was working using this line of code:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@using Umbraco.Web.Models
var newsItems = Model.Content.AncestorOrSelf("homePage").Descendants("newsItem").Take(numberOfItems);
But to make the site more efficient, I changed it to this:
var newsItems = Umbraco.TypedContentAtXPath("//NewsItem").Take(numberOfItems);
By the way, numberOfItems is a property that is retrieved from the Macro property. Basically, I am now not getting any IPublishedContent items when debugging, and thus none are being rendered in the view.
I'm not sure what happened yesterday, but its working now. Yes, newsItem is the alias, but I thought it used the template name NewsItem. I also found that the XML node names can be found in the umbraco.config.
Yes, it's using the alias, if you look in the umbraco.config xml file you'll find the newsItem entry and see if it's lowercase or not...
... also using //newsItem is great if the news items are scattered all over the site, but if they are in one folder then specifying the path from the root will be much more performant eg
I will actually implement that as they are in a list view underneath a doctype named newsArea and not scattered all over the site. newsArea actually lives under the doctype homePage, so I will probably be looking at somthing like: homePage/newsArea/newsItem. I also like how you can find items with specific properties like //newsItem[type=news].
to measure the time it takes specifically for the line of code that pulls back the news items, and compare the different approaches, to see how much XPath wins by!
Very good, thanks Marc, that's very informative. When my node tree grows immensely, I will look into these. At the moment my tree is tiny as I am using this particular instance to learn Umbraco from scratch.
Umbraco.TypedContentAtXPath returning null in Macro Partial
I have a node called
NewsArea
that hasNewsItem
child nodes in a Macro Partial that is rendered in theNewsArea
template.My code was working using this line of code:
But to make the site more efficient, I changed it to this:
By the way,
numberOfItems
is a property that is retrieved from the Macro property. Basically, I am now not getting anyIPublishedContent
items when debugging, and thus none are being rendered in the view.Hi Darryl
The xpath will be case sensitive, so is your alias NewsItem or newsItem?
would changing to this:
make a difference for you?
regards marc
Thanks Marc,
I'm not sure what happened yesterday, but its working now. Yes,
newsItem
is the alias, but I thought it used the template nameNewsItem
. I also found that the XML node names can be found in theumbraco.config
.HI Darryl
Yes, it's using the alias, if you look in the umbraco.config xml file you'll find the newsItem entry and see if it's lowercase or not...
... also using //newsItem is great if the news items are scattered all over the site, but if they are in one folder then specifying the path from the root will be much more performant eg
root/NewsSection/newsItem
or similary might be worth playing around with.
but glad it's working!
cheers
marc
Thanks again Marc for the further insight :-)
I will actually implement that as they are in a list view underneath a doctype named
newsArea
and not scattered all over the site.newsArea
actually lives under the doctypehomePage
, so I will probably be looking at somthing like:homePage/newsArea/newsItem
. I also like how you can find items with specific properties like//newsItem[type=news]
.No probs, if you want to feel smug about your refactoring, you can use the Miniprofiler that ships with Umbraco
https://our.umbraco.com/documentation/Getting-Started/Code/Debugging/#miniprofiler
to measure the time it takes specifically for the line of code that pulls back the news items, and compare the different approaches, to see how much XPath wins by!
If the site gets 'really big' thousands of news articles, then consider using Examine or XPathNavigator to speed up the queries, there is a good comparison of approaches in this talk by Dave Woestenborghs https://www.slideshare.net/dawoe/the-need-for-speed-uk-fest?next_slideshow=1
Very good, thanks Marc, that's very informative. When my node tree grows immensely, I will look into these. At the moment my tree is tiny as I am using this particular instance to learn Umbraco from scratch.
is working on a reply...