Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • ssougnez 46 posts 189 karma points c-trib
    Apr 24, 2018 @ 09:09
    ssougnez
    0

    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:

    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 ?

  • Peter Gregory 394 posts 1516 karma points MVP admin c-trib
    Apr 24, 2018 @ 10:29
    Peter Gregory
    0

    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.

    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.

  • ssougnez 46 posts 189 karma points c-trib
    Apr 24, 2018 @ 11:51
    ssougnez
    0

    Hi, interesting. Someone adviced me to use Lucene to search for the desired items. Would you recommend it?

  • ssougnez 46 posts 189 karma points c-trib
    May 17, 2018 @ 09:18
    ssougnez
    0

    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.

    Can someone enlighten me about this?

  • Nik 777 posts 3092 karma points MVP c-trib
    May 17, 2018 @ 09:32
    Nik
    100

    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

  • ssougnez 46 posts 189 karma points c-trib
    May 17, 2018 @ 10:23
    ssougnez
    1

    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 ;-)

Please Sign in or register to post replies

Write your reply to:

Draft