AddQueryOption Issue


When using the $this->OrderBy() function to add an option to a query combined with $this->Select(), for any reason prepare a uriRequest like:

"http://[SERVER NAME]/[APPLICATION NAME]/[OData]/[TABLE]?$orderby=No,&$select=No,Description,Unit_Price" Which returned a SERVER error...

Expression expected at position 3.

This because of the comma after the value of $orderby. Obviously the SERVER were expecting other value.

To fix error:

Open DataServiceQuery.php in your OData Library and add the below code inside the public function Execute():

//Add this before setting up the $httpRequest variable
$query = str_replace(',&','',$query);

So the function should look like:
public function Execute()
   $queryOption = self::buildQueryOption();
   $query = $this->_entitySetUrl . '?' . $queryOption;
   $requestVersion = Resource::DataServiceVersion_1;

   //We need to set DataServiceVersion header as 2.0, in 4 cases:
   //a. If query url contains inlinecount option
   //b. If query url contains count option
   //c. If query url contains skiptoken option
   //d. If query url contains select option
   if((strpos($query, '$inlinecount') !== FALSE)||
      (strpos($query, '$count') !== FALSE)||
      (strpos($query, '$skiptoken') !== FALSE)||
      (strpos($query, '$select') !== FALSE))
       $requestVersion = Resource::DataServiceVersion_2;

   $query = str_replace(',&','&',$query);
   $httpRequest = $this->_context->CreateRequest($query,
   return $this->_context->ExecuteAndProcessResult($httpRequest, Resource::DataServiceVersion_2);

This should fix the problem.