Copied to clipboard

Flag this post as spam?

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


  • Vesselin Vassilev 3 posts 71 karma points
    22 days ago
    Vesselin Vassilev
    0

    How to use IQuery<IContent> in ContentService.GetPagedChildren in v.13

    Hi guys,

    can you please give me a sample on how to use the filter option of the GetPagedChildren method of the ContentService?

    I found some old threads, but they all use _sqlContext which might have been the case for older versions, but not for v.13?

  • Joppe Ruessink 8 posts 118 karma points
    21 days ago
    Joppe Ruessink
    0

    Hi Vesselin,

    Before I can give you an correct answer: Are you only option for retrieving published content?

    In practice you should ONLY use the IContentService for creating and updating content.

    If you want to retrieve content you can use the IPublishedContentQuery.

  • Vesselin Vassilev 3 posts 71 karma points
    21 days ago
    Vesselin Vassilev
    0

    Hi Joppe,

    good point.

    I was using IContentService because I was creating some content programmatically, but for retrieving published items I should be using the UmbracoHelper and IPublishedContentQuery - am I right?

    As an example, say you have a Document Type called Player and each player has a property Age.

    I have a custom RenderController for this doc type.

    So, if I want to display a list of Players where Age = 30, should I be using the Search of the IPublishedContentQuery ?

    Thanks a lot!

  • Joppe Ruessink 8 posts 118 karma points
    21 days ago
    Joppe Ruessink
    100

    Hi Vesselin,

    With the IPublishedContentQuery (inject it from the dependency container of Umbraco in a registered service or in a .cshtml view using @inject) it is possible to get retrieve the model Player.

    Umbraco generates models based on the DocTypes you created in the solution of the project. In this case your generated model is called: Player. You can then pass this generic model to an function of the IPublishedContentQuery like so:

    //Replace with correct page id
    int pageId = 0;
    
    //Using the generic function
    var player = publishedContentQuery.Content(pageId)?.DescendantOrSelf<Player>();
    
    //Properties are now accessable 
    player.Age;
    

    I just quickly put this here off the top of my head but I'm guessing this would work.

    Let me know!

    Greeting, Joppe

  • Vesselin Vassilev 3 posts 71 karma points
    21 days ago
    Vesselin Vassilev
    0

    I ended up using the UmbracoHelper and then filtering the .Children() IPublishedContent collection which works great for me.

    Thanks again for your help!

  • Joppe Ruessink 8 posts 118 karma points
    21 days ago
    Joppe Ruessink
    0

    Cheers!

    Greetings, Joppe

Please Sign in or register to post replies

Write your reply to:

Draft