Sorting items from ContentItemQuery based on the drag and drop sorting in the CMS

May 1, 2024 10:53 AM

I've been using the ContentItemSelectorComponent in a custom widget we've created and with the recent updates we've been able to use the drag and drop feature to sort the items (in the CMS).

I'm expecting the items to be sorted in this order by default on performing the ContentItemQuery when extracting the items for the frontend component however they still seem to be sorted in the order they were created in.

I'm passing the identifiers of the linked content items through to the ContentItemQuery and extracting them that way. Not sure if there is another new OrderBy field I can use to sort these some where but its not something I've picked up in the documentation or in VS.


Environment

Answers

The ContentItemSelectorComponent results include the GUIDs of the items selected, so order the result set by the original selector items order using the GUIDs.

The SQL generated by passing the identifiers to your content query is something like WHERE MYFIELD IN (GUID, GUID, GUID).

You could do an ORDER BY in the query with some more advanced SQL... you'd need to pass in the full list of GUIDs, make a temp table out of it, JOIN with the results and order them by the list you sent - not worth it!

This is a very different use case compared to scenarios when you don't know which items you want from the DB and you are asking the DB to give you "whatever's at the top" when sorting by some column.

You can see an example of this result set sorting in the Community Portal source code.


To answer this question, you have to login first.