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)
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;
}
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
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.
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.
regards
Hey Martijn
I'm sitting with the same right now. Did you figured it out ?
Yes I did
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...
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
If you get 0, then you have something wrong in your setup and check if you have version 8.0.2 running
Hey,
The TotalChildren are correct, but i'm still getting a empty array. Weird..
But thanks, I will try to dive deeper in :)
What is the value in totalchilderen?
In my case it's 5
can you set a breakpoint on IEnumerable<IContent> Children = Services.ContentService.GetPagedChildren(.....
So you can see what values you got under Childeren
It's just giving me "{Umbraco.Core.Models.Content[0]}"
breakpoint --> step into (f11)
I can't step in to it.. https://ibb.co/xm3ZLbx
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.
Thank you! :D
you're welcome
Martijn, Do you know how the filter works ? I would like to get children of a specific culture.
You can use something like this, change it to your liking
Thanks!
I can't get it to filter en a custom property..
I get this error:
"an expression tree may not contain a call or invocation that uses optional arguments"
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.
Cool, Thanks for your help !
Hey Martijn.
I'm having some trouble with the filter again..
I'm getting this... "An object reference is required for the non-static field, method or property SqlContext.Query
And if i add
Then I get "The type name Query<> does not exits in the type SqlContext
Hi Thomas,
For your question I think it implies you are using a non Static method.
or create a new instance (this is my working example)
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.
And then for that pageing and pagesize:
Thanks :)
Is this way one of the common pitfalls https://our.umbraco.com/documentation/reference/common-pitfalls/?
is working on a reply...