Copied to clipboard

Flag this post as spam?

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


  • Martijn 11 posts 88 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 124 posts 340 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 11 posts 88 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 124 posts 340 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 11 posts 88 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 124 posts 340 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 11 posts 88 karma points
    Jul 04, 2019 @ 12:00
    Martijn
    0

    What is the value in totalchilderen?

  • Thomas Hansen 124 posts 340 karma points
    Jul 04, 2019 @ 12:04
    Thomas Hansen
    0

    In my case it's 5

  • Martijn 11 posts 88 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 124 posts 340 karma points
    Jul 04, 2019 @ 12:11
    Thomas Hansen
    0

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

  • Martijn 11 posts 88 karma points
    Jul 04, 2019 @ 12:15
    Martijn
    0

    breakpoint --> step into (f11)

    resultset getpagedchilderen

  • Thomas Hansen 124 posts 340 karma points
    Jul 04, 2019 @ 12:23
    Thomas Hansen
    0

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

  • Martijn 11 posts 88 karma points
    Jul 04, 2019 @ 14:46
    Martijn
    2

    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 124 posts 340 karma points
    Jul 05, 2019 @ 06:04
    Thomas Hansen
    0

    Thank you! :D

  • Martijn 11 posts 88 karma points
    Jul 05, 2019 @ 06:53
    Martijn
    1

    you're welcome

  • Thomas Hansen 124 posts 340 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 11 posts 88 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 124 posts 340 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 11 posts 88 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 124 posts 340 karma points
    Jul 05, 2019 @ 08:06
    Thomas Hansen
    0

    Cool, Thanks for your help !

  • Thomas Hansen 124 posts 340 karma points
    Sep 26, 2019 @ 12:38
    Thomas Hansen
    0

    Hey Martijn.

    I'm having some trouble with the filter again..

    SqlContext.Query<IContent>().Where(x => x.Name.Contains("TEST") && ( x.Id == 1 || x.Id == 3) && x.CreateDate.Year >= 2019 )
    

    I'm getting this... "An object reference is required for the non-static field, method or property SqlContext.Query

    And if i add

    new SqlContext.Query<IContent>().Where(x => x.Name.Contains("TEST") && ( x.Id == 1 || x.Id == 3) && x.CreateDate.Year >= 2019 )
    

    Then I get "The type name Query<> does not exits in the type SqlContext

  • Martijn 11 posts 88 karma points
    Sep 27, 2019 @ 11:52
    Martijn
    1

    Hi Thomas,

    For your question I think it implies you are using a non Static method.

    public static string YourMethod() { ... }
    

    or create a new instance (this is my working example)

    public ActionResult GetNewsByParentId(int Id,int PageIndex = 0,int PageSize = 10, string filter = null)
    {
        var Filter = SqlContext.Query<IContent>();
    
        if (filter != null)
        {
             Filter = Filter.Where(x => x.Name.Contains(filter));
        }
        else
        {
            Filter = null;
        }
    
        IEnumerable <IContent> Children = Services.ContentService.GetPagedChildren(Id, PageIndex, PageSize, out long TotalChildren, Filter, Ordering.By("CreateDate", Direction.Descending, null, false));
     var result = new JsonResult
            {
                Data = new
                {
                     TotalChildren
                    ,PageIndex
                    ,PageSize
                    ,Children
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
            return result;
        }
    

    On the otherhand:

    I do not think Umbraco is going to fix this problem properly because al the data is saved as a Json object in the Database. And you can only query Json nativily from sql server 2016. So that you can write querys that can query Json object.

    I think it would be a better solution to create a own stored procedure and add all the parameters you want to query. This will work better then the current native Umbraco function.

    If you are using sql server 2016, you can use Json query (https://docs.microsoft.com/en-us/sql/t-sql/functions/json-query-transact-sql?view=sql-server-2016) Otherwise ==> https://stackoverflow.com/a/23724200/2910930

    regards

  • Thomas Hansen 124 posts 340 karma points
    Sep 27, 2019 @ 12:17
    Thomas Hansen
    0

    Thanks for your reply !

    I actually ended up using the content service and quering and filtering on that.

    IEnumerable<IPublishedContent> content = Umbraco.Content(pageId).Descendants().Where(x => x.IsVisible() && x.ContentType.Alias == "articlePage").OrderByDescending(x => x.Value("date"));
    

    And then for that pageing and pagesize:

    IEnumerable<IPublishedContent> CurrentContent = content.Skip((page - 1) * pageSize).Take(pageSize);
    

    Thanks :)

  • Giant Penguin 12 posts 81 karma points
    Oct 03, 2019 @ 04:32
    Giant Penguin
    0
Please Sign in or register to post replies

Write your reply to:

Draft