Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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:
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.
The xpath will be case sensitive, so is your alias NewsItem or newsItem?
would changing to this:
var newsItems = Umbraco.TypedContentAtXPath("//newsItem").Take(numberOfItems);
make a difference for you?
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
or similary might be worth playing around with.
but glad it's working!
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 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].
No probs, if you want to feel smug about your refactoring, you can use the Miniprofiler that ships with Umbraco
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...
Write your reply to:
Image will be uploaded when post is submitted