Issues with orderby?

Dec 12, 2013 at 4:03 PM
I've recently run into issues when trying to append an orderby clause to my filter using the ByQuery methods. I get an error with the ampersand due to the encoding.
var filter = "&$orderby=itg_CountryFamiliarName asc"
Has anyone else run into this issue? how did you get around it?
I've found myself having to re-write items in order to use the ByQueryUrl or do a sort afterwards. I don't particularly like having to do either, since the ByQuery does everything i want it to EXCEPT sort...
Coordinator
Dec 17, 2013 at 9:34 AM
Edited Dec 17, 2013 at 3:48 PM
Hi JDiMarzio,

I assume this is caused by the fact that the byQuery method will wrap the filter inside the ODATA-filter parameter:
///
/// Retrievs multiuple records based on filter
/// The max number of records returned by Odata is limited to 50, the result object contains the property 
/// 'next' and the fn loadNext that could be used to load the addional records 
///
function byQuery( entityName, columns, filter, opt_asyn ) {

    // default is 'true'
    var asyn = ( opt_asyn === undefined ) ? true : opt_asyn;

    // in case filter is empty 
    filter = ( filter ) ? "&$filter=" + encodeURIComponent( filter ) : '';

    // create defered object
    var setName = entityName + 'Set',
        query = getODataPath() + "/" + setName + "?$select=" + columns.join( ',' ) + filter;

    return doRequest( { url: query }, asyn );
}
So I am afraid that the order-by parameter could used with the byQuery-method. But what we could do is writing another method that offers this option:
///
/// Retrievs multiple records based on filter and applies are sort-order
/// The max number of records returned by Odata is limited to 50, the result object contains the property 
/// 'next' and the fn loadNext that could be used to load the addional records 
///
function byQueryOrderBy( entityName, columns, filter, orderBy, opt_asyn ) {

    // default is 'true'
    var asyn = ( opt_asyn === undefined ) ? true : opt_asyn;

    // in case filter is empty 
    filter = ( filter ) ? "&$filter=" + encodeURIComponent( filter ) : '';

    orderBy = ( orderBy ) ? "&$orderby" + encodeURIComponent( orderBy ) : '';

    // create defered object
    var setName = entityName + 'Set',
        query = getODataPath() + "/" + setName + "?$select=" + columns.join( ',' ) + orderBy + filter;

    return doRequest( { url: query }, asyn );
}
Please take into account that is code is not tested.

Let me know what you think

Daniel
Dec 17, 2013 at 1:21 PM
Daniel,

I think this will work. One thing i would do is flip the orderBy and filter when I build the query statement
query = getODataPath() + "/" + setName + "?$select=" + columns.join( ',' ) + filter  + orderBy;
as opposed to
query = getODataPath() + "/" + setName + "?$select=" + columns.join( ',' ) + orderBy + filter;
But apart from that, this looks good. I'll give this a try.

Thanks!
Coordinator
Dec 17, 2013 at 3:45 PM
Hi,

you might be right but in this msdn-articel the orderby-clause come before the fitler-clause.

Regards,

Daniel
Dec 17, 2013 at 3:47 PM
Edited Dec 17, 2013 at 6:33 PM
Ah, good point. “orderBy” first it is then!