I am working on upgrading our blog to the latest version 7.11.1 from 7.1.9 and so far I have been able to get everything working except for when I try and get all of the articles from a certain Author.
For context I am trying to get all of the articles from a specific author. I have the article document type setup with an Content Picker 2 for author that I need to filter off.
@{
int count = 0;
var blog = Umbraco.TypedContentAtRoot().First().Descendants().Where(x => x.IsVisible() && x.DocumentTypeAlias == "MainFolder").FirstOrDefault();
if (blog != null)
{
var articleList = blog.Descendants().Where(x => x.IsVisible() && x.DocumentTypeAlias == "article" && x.GetPropertyValue("author").ToString() == Model.Id.ToString()).OrderByDescending(x => x.CreateDate);
foreach (var article in articleList)
{
count++;
}
}
This works on our older versions however once upgraded it will only return a blank value for articleList.
Is there a new or best practice way of doing this?
not sure why it's not working for you but I would advice you to use XPath instead of linq for this kind of query.
An example of an article query with this could be:
var authorId = Model.Content.Id;
var articleList = Umbraco.TypedContentAtXPath("/root/homePage/newsArea/newsArticle[@author=" + @authorId + "]");
var count = articleList.Count();
That should resolve your issue and enhance performance.
The problem with linq queries is that you generate ALL the content items you're iterating through as IPublishedContent items in memory. Especially .Descendants() can be really expensive. That is not necessary since you only want the articles. For more context see docs.
By using Umbraco.TypedContentAtXPath() you'll query directly against the XML cache (usually located at /AppData/umbraco.config) and only create IPublishedContent for what you actually need. In this case that would be articles where the value of the author property matches the authorId.
Filtering on Content Picker 2
I am working on upgrading our blog to the latest version 7.11.1 from 7.1.9 and so far I have been able to get everything working except for when I try and get all of the articles from a certain Author.
For context I am trying to get all of the articles from a specific author. I have the article document type setup with an Content Picker 2 for author that I need to filter off.
This works on our older versions however once upgraded it will only return a blank value for articleList.
Is there a new or best practice way of doing this?
Hi Mathew,
not sure why it's not working for you but I would advice you to use XPath instead of linq for this kind of query.
An example of an article query with this could be:
That should resolve your issue and enhance performance.
The problem with linq queries is that you generate ALL the content items you're iterating through as IPublishedContent items in memory. Especially
.Descendants()
can be really expensive. That is not necessary since you only want the articles. For more context see docs.By using
Umbraco.TypedContentAtXPath()
you'll query directly against the XML cache (usually located at/AppData/umbraco.config
) and only create IPublishedContent for what you actually need. In this case that would be articles where the value of theauthor
property matches theauthorId
.Use something like https://www.freeformatter.com/xpath-tester.html to test your xpath queries if needed :)
If you need help generating the XPath query https://xmltoolbox.appspot.com/xpath_generator.html is pretty neat and MS has some nice docs on it here.
Hope that helps.
All the best
Rune
is working on a reply...