Copied to clipboard

Flag this post as spam?

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


  • Martijn 10 posts 85 karma points
    Jun 12, 2019 @ 10:04
    Martijn
    0

    GetPagedChildren how to orderby and filter

    Hi there,

    I used this method to create a custom controller that I can access thru xhr. (first of all is this still usable / preferable in V8?)

    https://our.umbraco.com/apidocs/v7/csharp/api/Umbraco.Core.Services.ContentService.html#UmbracoCoreServicesContentServiceGetPagedChildrenSystemInt32SystemInt32SystemInt32SystemInt32_SystemStringUmbracoCorePersistenceDatabaseModelDefinitionsDirectionSystemString

    My custom method looks like this and is working. Now I am stuck with the ordering and filtering.

        [System.Web.Http.AcceptVerbs("GET", "POST")]
        [System.Web.Http.HttpGet]
        [System.Web.Http.AllowAnonymous]
        public ActionResult GetNewsByParentId(int Id,int PageIndex = 0,int PageSize = 10
        , string orderby, string direction, string filter = null)
        {
            //are there children for this content id?
            bool hasChildren = Services.ContentService.HasChildren(Id);
    
            //get the first 10 childeren
            IEnumerable<IContent> Children = Services.ContentService.GetPagedChildren(Id, PageIndex, PageSize, out long TotalChildren);
    
            //create Json object
            var result = new JsonResult
            {
                Data = new
                {
                     TotalChildren
                    ,PageIndex
                    ,PageSize
                    ,Children
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
            return result;
        }
    

    Ordering: I would like to now if I can filter by a 1 or multiple custom field .OrderBy("Tag").ThenBy("Date")

    Or can i only use the default properties (publisheddate, Name etc) (and where can I find a list with witch ones)

    The ordering direction? https://our.umbraco.com/apidocs/v7/csharp/api/Umbraco.Core.Persistence.DatabaseModelDefinitions.Direction.html Is there a example how this works (what do I need to fill in).

    De filter function How can I use to search on multiple tags / words / numbers (is this even possible?) can i use where and or contains?

    I hope somebody can help me with a nice example.

    Services.ContentService.GetPagedChildren(Id, PageIndex, PageSize, out long TotalChildren,"?","?","?");
    

    regards

  • Thomas Hansen 102 posts 305 karma points
    Jul 04, 2019 @ 08:24
    Thomas Hansen
    0

    Hey Martijn

    I'm sitting with the same right now. Did you figured it out ?

  • Martijn 10 posts 85 karma points
    Jul 04, 2019 @ 09:20
    Martijn
    1

    Yes I did

        public ActionResult GetNewsByParentId(int Id,int PageIndex = 0,int PageSize = 10, string filter = null)
        {
            //are there children for this content id?
            bool hasChildren = Services.ContentService.HasChildren(Id);
    
            IEnumerable<IContent> Children = Services.ContentService.GetPagedChildren(Id, PageIndex, PageSize, out long TotalChildren, null, Ordering.By("CreateDate", Direction.Descending, null, false));
    
            //create Json object
            var result = new JsonResult
            {
                Data = new
                {
                     TotalChildren
                    ,PageIndex
                    ,PageSize
                    ,Children
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
            return result;
        }
    

    The extra info from the class ordering https://github.com/umbraco/Umbraco-CMS/blob/853087a75044b814df458457dc9a1f778cc89749/src/Umbraco.Core/Services/Ordering.cs

    when using custom fields set the last property on true and use your custom field

  • Thomas Hansen 102 posts 305 karma points
    Jul 04, 2019 @ 09:37
    Thomas Hansen
    0

    Thanks for your answer.

    I just copied your code, and i'm still getting an empty array back...

    {Umbraco.Core.Models.Content[0]}
    
  • Martijn 10 posts 85 karma points
    Jul 04, 2019 @ 11:49
    Martijn
    0

    Thomas

    I am not a Umbraco specialist but I will give it a try.

    If you not already did this:

    (Content --> "home page" --> "news" --> newsmessage

    1. Create a parent node this wil be "news" (publish and get the Id number (under info))
    2. Create 1 or 2 childeren nodes under "news" this will be "newsmessage" --> publish
    3. Run the method and use de Id in step 1 to set the Id hardcoded
    4. and check how many childeren you have under the TotalChildren property

    If you get 0, then you have something wrong in your setup and check if you have version 8.0.2 running

  • Thomas Hansen 102 posts 305 karma points
    Jul 04, 2019 @ 11:59
    Thomas Hansen
    0

    Hey,

    The TotalChildren are correct, but i'm still getting a empty array. Weird..

    But thanks, I will try to dive deeper in :)

  • Martijn 10 posts 85 karma points
    Jul 04, 2019 @ 12:00
    Martijn
    0

    What is the value in totalchilderen?

  • Thomas Hansen 102 posts 305 karma points
    Jul 04, 2019 @ 12:04
    Thomas Hansen
    0

    In my case it's 5

  • Martijn 10 posts 85 karma points
    Jul 04, 2019 @ 12:06
    Martijn
    0

    can you set a breakpoint on IEnumerable<IContent> Children = Services.ContentService.GetPagedChildren(.....

    So you can see what values you got under Childeren

  • Thomas Hansen 102 posts 305 karma points
    Jul 04, 2019 @ 12:11
    Thomas Hansen
    0

    It's just giving me "{Umbraco.Core.Models.Content[0]}"

  • Martijn 10 posts 85 karma points
    Jul 04, 2019 @ 12:15
    Martijn
    0

    breakpoint --> step into (f11)

    resultset getpagedchilderen

  • Thomas Hansen 102 posts 305 karma points
    Jul 04, 2019 @ 12:23
    Thomas Hansen
    0

    I can't step in to it.. https://ibb.co/xm3ZLbx

  • Martijn 10 posts 85 karma points
    Jul 04, 2019 @ 14:46
    Martijn
    1

    Replace the 1 in to 0, the index starts at 0 not @ 1 (pageindex)

    If you want 100 (pagesize) records and you start @ page 1 instead of page 0 you will need @ least a 101 childeren before you get something.

  • Thomas Hansen 102 posts 305 karma points
    Jul 05, 2019 @ 06:04
    Thomas Hansen
    0

    Thank you! :D

  • Martijn 10 posts 85 karma points
    Jul 05, 2019 @ 06:53
    Martijn
    1

    you're welcome

  • Thomas Hansen 102 posts 305 karma points
    Jul 05, 2019 @ 06:54
    Thomas Hansen
    0

    Martijn, Do you know how the filter works ? I would like to get children of a specific culture.

  • Martijn 10 posts 85 karma points
    Jul 05, 2019 @ 07:00
    Martijn
    1

    You can use something like this, change it to your liking

    SqlContext.Query<IContent>().Where(x => x.Name.Contains("TEST") && ( x.Id == 1 || x.Id == 3) && x.CreateDate.Year >= 2019 )
    
  • Thomas Hansen 102 posts 305 karma points
    Jul 05, 2019 @ 08:01
    Thomas Hansen
    0

    Thanks!

    I can't get it to filter en a custom property..

    var filter = SqlContext.Query<IContent>().Where(x => x.GetValue<DateTime>("date", culture).Year == year && x.IsCulturePublished(culture));
    

    I get this error:

    "an expression tree may not contain a call or invocation that uses optional arguments"

  • Martijn 10 posts 85 karma points
    Jul 05, 2019 @ 08:05
    Martijn
    1

    Thomas

    I am stuck on that too, for now, I believe you can only use:

    Id, Name, Key, VersionId, Trashed, Path, SortOrder, ParentId, CreateDate, UpdateDate, Level, ContentTypeId, CreatorId, Published

    I am goining to create a featue request on github, hopefully It will get some extra explanation.

  • Thomas Hansen 102 posts 305 karma points
    Jul 05, 2019 @ 08:06
    Thomas Hansen
    0

    Cool, Thanks for your help !

Please Sign in or register to post replies

Write your reply to:

Draft