I have a web service in which I want to retrieve all document of a certain content type but as strongly typed object. I tried to use this:
var contentType = Services.ContentTypeService.GetContentType(PensionPoint.ModelTypeAlias);
var points = Services.ContentService.GetContentOfContentType(contentType.Id).ToList();
However, I get IContent object on which I can't do Object.Property. How can I retrieve the same items but as strongly-types object ?
Firstly you should not be using the services to return published content. You should always use the UmbracoHelper to get content back in the proper IPublishedContent model. If you have ModelsBuilder setup correctly and your models generated you should be able to use the UmbracoHelper to retrieve your items as the right type. Eg fin a view you would do this.
var point = Umbraco.TypedContent<PensionPoint>(1234);
If you need to get the helper.
var helper = new UmbracoHelper(UmbracoContext.Current);
var point = helper.TypedContent<PensionPoint>(1234);
I know this didn't fully answer your question but hopefully points you in the right direction.
I'm coming back with this question as I still don't have a reliable answer...
So far, I did this:
var query = ExamineManager.Instance.CreateSearchCriteria().NodeTypeAlias(PensionPoint.ModelTypeAlias).Compile();
var items = Umbraco.TypedSearch(query);
Which works but, once again, is it a best practice to use search for look for items in a web service ?
I'm searching the net since days now and I can't seem to find a document explaining the best practice to query items.
It actually depends, there isn't necessarily a best practice. There are pros and cons to using Lucene vs the Umbraco cache. In fact, you might even want to look at using an XPath query to find information from the Umbraco Cache, that can be quicker still.
If you use Lucene, you will probably still be after the typed object so you are still going to end up hitting the Umbraco Cache anyway. As you don't appear to be doing any filtering, you might be best to just hit the Umbraco Cache.
However, if you are doing paging/filtering of any sort, then yes I would probably use Lucene first and then just retrieve the desired models from the Umbraco Cache.
Funny, I found this article like 10 min after your answer hehe
Even though that article is great, I think that an official documentation is missing. All the information is split here and there and it takes time to really get the bigger picture. It would be interesting to have an article like the one you posted, up-to-date and explaining what happens with all the approach. Like saying that Descendants will loop through ALL items, while Lucene will only fetch the one corresponding to the criteria (I guess), etc...
Querying item is one of the most important thing when it comes to create a site.
Get all strongly-typed document of a certain type
Hi,
I have a web service in which I want to retrieve all document of a certain content type but as strongly typed object. I tried to use this:
However, I get
IContent
object on which I can't doObject.Property
. How can I retrieve the same items but as strongly-types object ?Hi there...
Firstly you should not be using the services to return published content. You should always use the UmbracoHelper to get content back in the proper IPublishedContent model. If you have ModelsBuilder setup correctly and your models generated you should be able to use the UmbracoHelper to retrieve your items as the right type. Eg fin a view you would do this.
If you need to get the helper.
I know this didn't fully answer your question but hopefully points you in the right direction.
Hi, interesting. Someone adviced me to use Lucene to search for the desired items. Would you recommend it?
I'm coming back with this question as I still don't have a reliable answer...
So far, I did this:
Which works but, once again, is it a best practice to use search for look for items in a web service ?
I'm searching the net since days now and I can't seem to find a document explaining the best practice to query items.
Can someone enlighten me about this?
Hi Ssougnez,
It actually depends, there isn't necessarily a best practice. There are pros and cons to using Lucene vs the Umbraco cache. In fact, you might even want to look at using an XPath query to find information from the Umbraco Cache, that can be quicker still.
If you use Lucene, you will probably still be after the typed object so you are still going to end up hitting the Umbraco Cache anyway. As you don't appear to be doing any filtering, you might be best to just hit the Umbraco Cache.
However, if you are doing paging/filtering of any sort, then yes I would probably use Lucene first and then just retrieve the desired models from the Umbraco Cache.
This is a really good article discussing performance of different querying approaches: http://skrift.io/articles/archive/testing-the-performance-of-querying-umbraco/
Thanks,
Nik
Funny, I found this article like 10 min after your answer hehe
Even though that article is great, I think that an official documentation is missing. All the information is split here and there and it takes time to really get the bigger picture. It would be interesting to have an article like the one you posted, up-to-date and explaining what happens with all the approach. Like saying that Descendants will loop through ALL items, while Lucene will only fetch the one corresponding to the criteria (I guess), etc...
Querying item is one of the most important thing when it comes to create a site.
Thanks for your answer ;-)
is working on a reply...