首页 > SAP > ODATA > $top and $skip Query option in SAP OData Service
2019
08-29

$top and $skip Query option in SAP OData Service

Hello everyone, in this tutorial we will learn how to use query option $top and $skip in SAP netweaver gateway OData service. Before proceeding further we assume that you know how to build OData service in sap gateway. Access all SAP Netweaver Gateway tutorials here.Lets get started

1.What $top and $skip query option does ?

Query option $top and $skip are used to restrict the amount of data retrieved from backend system. Client side paging is possible by using this query option.$top=n query option will retrieve the top n records from the OData service feed/collection. $skip=x will go with $top=n query option, it will retrieve  top n records by skipping the first x records from the OData service feed/collection.

2.Supported System Version

SAP NetWeaver Gateway Release-采购向厂商下P/O之后,必须透过一定权限经由主管对此张P/O做 release后才能收料 2.0 Support Package >=03

3.Business Example

A gateway OData service is getting all the products from the back-end system.

You want only top 5/first 5 records from the list of products, you can get that by adding $top=5 to the OData service URI.
You want top 5 records by skipping the first 3 records, you can get that by adding $top=5&$skip=3 to the OData service URI.

4.Syntax

http://<server>:<port>/sap/opu/odata/sap/<service_name>/ProductsSet?$top=5

http://<server>:<port>/sap/opu/odata/sap/<service_name>/ProductsSet?$top=5&skip=3

5.How to Implement $top and $skip query option in OData Service

In this section we will adjust the OData service to respond to $top and $skip query option. $top and $skip query option should be applied only to Entity Type sets. We assume that you have already build the OData service to get the list of products. if you have not click here.

1. After successful creation of OData service. Go back to service builder SEGW, Navigate to the ABAP workbench for the Product Entity set.

2. Code inside the method PRODUCTSSET_GET_ENTITYSET will look like below i.e before adjusting the service to $top and $skip query option.

3. In the above code snippet, we have retrieved the list of products generally by providing the max rows.Now we will adjust the code to adapt it to the $top and $skip query option. $top and $skip query options are accessible in the method by using below 2 lines of code.

4. Put the below code in the method PRODUCTSSET_GET_ENTITYSET to respond to the $top and $skip query option.

5. What we did in the above step.First we got all the products and then get $top and $skip query options value from the service request.Next we applied them on to the BAPI result data to get the desired data as per the $top and $skip query options.

6. From code perspective we are ready, so lets test the service by using the sap gateway client /IWFND/GW_CLIENT.

7. If we execute the service without adding $top and $skip query option, we should get all the products available in the system.

If we add the $top=5 query option to the service we should get the top 5 records. Test the service by providing the following URI with $top=5.

/sap/opu/odata/sap/ZDEMO_GW_SRV_SRV/ProductsSet?$top=5′.

If we add the $top=5&$skip=2 query option to the service we should get the top 5 records by skipping the first 2 records. Test the service by providing the following URI with $top=5&$skip=2

/sap/opu/odata/sap/ZDEMO_GW_SRV_SRV/ProductsSet?$top=5’&skip=2′.

Stay tuned to us for more SAP Netweaver Gateway tutorials.

Thank you. Please feel free to comment and let us know your feedback.

最后编辑:
作者:yangsen
有需要可在文章留言或者邮件yan252@163.com给我。