How to obtain all content nodes (published & unpublished) quickly
I have a project which has a large number of nodes (2000+) and I am creating a custom section which displays them all. Some of the nodes are published and some of them are unpublished.
I know I can get all nodes with the following line of code:
var contentList = Services.ContentService.GetChildren(1096);
However, this takes several seconds to load which is too long.
This showed me a method of getting all content nodes much much faster with the following line of code:
var contentList = Umbraco.TypedContentAtXPath("//blog [@isDoc]");
The only issue with this is that I can't get unpublished nodes this way. Can anyone please help me with a method that obtains unpublished nodes just as fast.
This will be a lengthy process when retrieving a lot of content, as it has to hit the database to get unpublished content. I would recommend using paging in your custom section and then use something like this:
You might be able to get want you want from the index, which might be a bit faster that hitting the db?
for a rough example:
var searcher = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"];
var criteria = searcher.CreateSearchCriteria(BooleanOperation.And);
var query = "+__IndexType:content";
var searchQuery = criteria.RawQuery(query);
var results = searcher.Search(searchQuery);
@foreach (var result in results)
{
<p>@result.Fields["nodeName"] @result.Fields["isPublished"]</p>
}
Notice this is using the Internalsearcher which includes unpublished content but this might also pull back content that is in the recycle bin, however i guess you could put checks in place for that.
Thank you very much for your response it works like a charm. I ended up with the following code which gets all the nodes I want:
var searcher = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"];
var criteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria(IndexTypes.Content);
var query = criteria.Field("nodeTypeAlias", "blog");
var results = searcher.Search(query.Compile());
The above is for V7, Umbraco 8 is quite different and you can now inject a lot more into your service.
Try injecting IContentPublishedCache or one of the others found in the our umbraco api documentation.
How to obtain all content nodes (published & unpublished) quickly
I have a project which has a large number of nodes (2000+) and I am creating a custom section which displays them all. Some of the nodes are published and some of them are unpublished.
I know I can get all nodes with the following line of code:
However, this takes several seconds to load which is too long.
I stumbled across this great article about the speeds of different methods to query content in Umbraco: https://skrift.io/articles/archive/testing-the-performance-of-querying-umbraco/
This showed me a method of getting all content nodes much much faster with the following line of code:
The only issue with this is that I can't get unpublished nodes this way. Can anyone please help me with a method that obtains unpublished nodes just as fast.
Hi James,
This will be a lengthy process when retrieving a lot of content, as it has to hit the database to get unpublished content. I would recommend using paging in your custom section and then use something like this:
Thanks, Tom
Look at this here. Document seems to have a .Published way of finiding the published ones.
You got me curious so I'm going to try this and come back to you.
Hi Tom,
thanks for your reply.
Unfortunately the project is a little too far to change to using paging rather than the tree that is currently being used.
I'm thinking about just having all content nodes published so that I can access them faster.
You might be able to get want you want from the index, which might be a bit faster that hitting the db?
for a rough example:
Notice this is using the Internalsearcher which includes unpublished content but this might also pull back content that is in the recycle bin, however i guess you could put checks in place for that.
cheers.
Hi Ismael,
Thank you very much for your response it works like a charm. I ended up with the following code which gets all the nodes I want:
Much faster than hitting the db.
Hi James,
Are you using V8? I try to acheive this and I can't get this to work.
The above is for V7, Umbraco 8 is quite different and you can now inject a lot more into your service. Try injecting IContentPublishedCache or one of the others found in the our umbraco api documentation.
is working on a reply...