When interacting with SAP OData services, one of the most powerful features is the ability to shape the API response according to your needs. This is done through queries, allowing API consumers (clients) to filter, sort, paginate, and format the data.
This post will explain what a query is, how you can identify and use queries, and provide a list of commonly used OData query parameters along with examples.
What Is a Query?
A query is a mechanism that allows an API consumer to request specific data, filtering, sorting, and formatting the result set according to certain parameters. Instead of receiving all the data at once, you can use a query to retrieve just the information you need, which makes interactions with the API more efficient and tailored.
You pass queries as query string parameters. These parameters are attached to the end of the URL and modify the API response.
This allows you to:
- Filter the data by a specific condition (e.g., “show only products in the ‘Electronics’ category”).
- Sort the data by a particular field (e.g., “order products by price in descending order”).
- Select specific fields to include in the response (e.g., “only return product names and prices”).
How Can I Find the Query?
A typical SAP OData request URL looks like this:
https://<server>:<port>/sap/opu/odata/sap/<service_name>/Products?$select=Category,Name
- https://: The protocol used for communication (HTTPS ensures data security).
- <server>:<port>: The address of the SAP server hosting the OData service.
- /sap/opu/odata/sap/<service_name>: The path to the OData service (in this case,
<service_name>
is the name of your OData service). - /Products: This is the specific entity set or resource being requested (in this case, a collection of products).
- ?$select=Category,Name: This is the query string, which modifies the request to return only the
Category
andName
fields from theProducts
entity set.
The query string begins with a ?
and consists of key-value pairs separated by &
. Each pair represents a query parameter and its corresponding value.
The Query String Explained
An example query string might look like this:
code$filter=Category eq 'Electronics'&$orderby=Price desc&$top=10
- Key: The name of the query parameter (e.g.,
$filter
,$orderby
). - Value: The instruction or condition for the parameter (e.g.,
Category eq 'Electronics'
). - Multiple query parameters can be used in a single request, each separated by an ampersand (
&
).
Common Query Parameters in SAP OData
Here is a list of common OData query parameters, their functions, and examples of how to use them:
- $filter
- Used to filter results based on specific criteria.
- Example:
?$filter=Name eq 'John' and Age gt 25
- This filters the data to return only records where the
Name
is “John” and theAge
is greater than 25.
- $orderby
- Used to sort the results by one or more properties.
- Example:
?$orderby=Name asc
- This orders the data by the
Name
field in ascending order.
- $top
- Specifies the number of records to return from the top of the result set.
- Example:
?$top=10
- This returns only the top 10 results from the dataset.
- $skip
- Skips a specified number of records before returning the result set (useful for pagination).
- Example:
?$skip=5
- This skips the first 5 records and returns the rest.
- $select
- Restricts the response to include only specific properties of an entity.
- Example:
?$select=Name,Age
- This returns only the
Name
andAge
fields of the entity.
- $expand
- Expands related entities inline, allowing navigation properties to be included in the response.
- Example:
?$expand=Orders
- This includes the related
Orders
entity in the response.
- $count
- Returns the total number of entities in the collection.
- Example:
?$count=true
- This returns the count of entities alongside the actual data.
- $format
- Specifies the format of the response data (e.g., JSON or XML).
- Example:
?$format=json
- This requests the response data in JSON format.
- $search
- Performs a full-text search on the data.
- Example:
?$search='Product123'
- This performs a search for the term
'Product123'
across relevant fields.
- $inlinecount (now often replaced by
$count
)- Returns the count of items in the result set along with the data.
- Example:
?$inlinecount=allpages
- This includes the total number of entities in the response, which is helpful when paginating.
- $batch
- Used for batch processing of multiple requests in a single call.
- Example:
/sap/opu/odata/sap/your_service/$batch
- This combines multiple requests into a single HTTP call for efficiency.
- $skiptoken
- Used for paging through large result sets by retrieving the next set of records.
- Example:
?$skiptoken=<token_value>
- This retrieves the next “page” of results, starting from where the last request ended.
Example SAP OData Query in Action
Let’s say you want to retrieve the top 5 products in the “Furniture” category, ordered by their release date in descending order. You also only want to see the ProductName
and ReleaseDate
fields in the response.
Your request might look like this:
https://<server>:<port>/sap/opu/odata/sap/ZPRODUCT_SRV/Products?$filter=Category eq 'Furniture'&$orderby=ReleaseDate desc&$top=5&$select=ProductName,ReleaseDate
Breaking it down:
?$filter=Category eq 'Furniture'
: Filters the products to only return those in the “Furniture” category.&$orderby=ReleaseDate desc
: Orders the products by release date in descending order.&$top=5
: Limits the results to the top 5 products.&$select=ProductName,ReleaseDate
: Only includes theProductName
andReleaseDate
fields in the response.
Conclusion
Understanding SAP OData queries is essential for efficiently working with large datasets and APIs. By mastering the use of query parameters, you can tailor the API responses to meet your exact needs, retrieving just the right data in the right format.
Whether you’re filtering by specific conditions, ordering your results, or expanding related entities, queries allow you to control how the data is returned, making your interactions with the API both precise and powerful.
Pro Tip: Always check your SAP OData service documentation to ensure the available query parameters and their supported values for specific entities.
Encode Message Payload to Base64 on CPI!
How to use Base64 message encoder in SAP Integration Suite.
Subscribe for moreMy First Interface on CPI!
Learn how to develop your first iFlow on SAP Integration Suite within 7 minutes!
Subscribe for moreSIGN UP TODAY!
Sign up to receive our monthly newsletter and special deals!