1

Currently the code available in Core module help us to include sorting parameter for brokery query as shown below. This implementation has option to include Meta key for Metadata field. But i need to include for Contentdata field.

//Adding sort parameter to the Query
query.AddSorting(GetSortParameter());

private SortParameter GetSortParameter()
{
    SortDirection dir = _queryParameters.Sort.ToLower().EndsWith("asc") ? SortParameter.Ascending : SortParameter.Descending;            
    return new SortParameter(GetSortColumn(), dir);
}

private SortColumn GetSortColumn()
{
//TODO add more options if required
int pos = _queryParameters.Sort.Trim().IndexOf(" ", StringComparison.Ordinal);
string sort = pos > 0 ? _queryParameters.Sort.Trim().Substring(0, pos) : _queryParameters.Sort.Trim();
switch (sort.ToLower())
{
    case "title":
        return SortParameter.ItemTitle;
    case "pubdate":
        return SortParameter.ItemLastPublishedDate;
    default:
        //Default is to assume that its a custom metadata date field;
        return new CustomMetaKeyColumn(_queryParameters.Sort, MetadataType.DATE);
}
}

I want to create a Sort Column to query Content field "date" or "year" or "location" which is included in Content part of the News Article/Product Component.

1 Answer 1

4

Unfortunately, only metadata fields are published to a specific broker table to allow you for querying or sorting, so sorting on content fields isn't possible using the regular API.

Some options you may have

  • move the field you wish to sort on to metadata (most recommended option)
  • add a metadata field and copy the contents of the sort field to this field using eventsystem code
  • load all items from the broker, and sort these in code - note that this option is not viable when you expect a large amount of items, as this will degrade performance. Only do this when you know you will only have a small amount of items. (least recommended option)
1
  • 1
    +1 to the recommendations and the order. Custom Metadata Processors used to allow the ability to "stuff" a non-Metadata field into Custom Metadata, but it's no longer a supported extension point. Commented Oct 29, 2018 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.