添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Flag this post as spam?

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

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#Umbraco Core Services ContentService GetPagedChildren System Int32 System Int32 System Int32 System Int32 _System String Umbraco Core Persistence DatabaseModelDefinitions Direction System String

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

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

Thanks for your answer.

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

{Umbraco.Core.Models.Content[0]}
  • Create a parent node this wil be "news" (publish and get the Id number (under info))
  • Create 1 or 2 childeren nodes under "news" this will be "newsmessage" --> publish
  • Run the method and use de Id in step 1 to set the Id hardcoded
  • 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

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

    So you can see what values you got under Childeren

    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"

    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.

    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

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

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