$skiptoken Query Option in SAP OData Service

0
13600

Hello everyone, in this tutorial we will learn how to use query option $skiptoken in 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

$skiptoken OData Query Option

Query option $skiptoken is used to limit the amount of entries sent to the application. This also helps in boost-up the performance if huge number of entries requested.A next link is provided back to the application to get the next bulk of data.

Server side paging in OData service is achieved by adapting the “$skiptoken”.

Syntax

http://<server>:<port>/sap/opu/odata/sap/<service_name>/ProductsSet?$skiptoken=10

How it works?

  1. You have to define the page size in the server i.e GET_ENTITYSET method in the DPC_EXT class.
  2. If entries in the entity set are more than the page size,divide the entries by page size.
  3. Send the requested set of entries to the application based on skiptoken value and also the next link to next set of entries. For more details see the image below.

skiptoken logic

 

How to Implement $skiptoken query option in OData Service?

In this section we will adjust the OData service to respond to $skiptoken query option. $skiptoken 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.Go to ABAP Workbench2. Code inside the method PRODUCTSSET_GET_ENTITYSET will look like below i.e before adjusting the service to $skiptoken query option.

DATA:      ls_order          TYPE /iwbep/s_mgw_sorting_order,
           lt_products       TYPE STANDARD TABLE OF bapi_epm_product_header,
           ls_products       TYPE bapi_epm_product_header,
           ls_entityset      TYPE zcl_zdemo_gw_srv_mpc=>ts_products,
           lt_entityset      TYPE zcl_zdemo_gw_srv_mpc=>tt_products,
           lv_max_rows       TYPE bapi_epm_max_rows,
           ls_filter         TYPE /iwbep/s_mgw_select_option,
           lr_product_id     TYPE TABLE OF  bapi_epm_product_id_range,
           ls_product_id     TYPE bapi_epm_product_id_range,
           ls_select_options TYPE /iwbep/s_cod_select_option.

* >> Check if $filter option is available
* Get the filter option for product ID
    READ TABLE it_filter_select_options
        INTO ls_filter
        WITH KEY property = 'ProductId'.
    IF sy-subrc = 0.
      LOOP AT ls_filter-select_options INTO ls_select_options.
        MOVE-CORRESPONDING ls_select_options TO ls_product_id.
        APPEND ls_product_id TO lr_product_id.
      ENDLOOP.
    ENDIF.

* Call the BAPI by providing the filter options
* if no filter, BAPI will get the entire list of products
    CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST'
      TABLES
        headerdata        = lt_products
        selparamproductid = lr_product_id.
    IF lt_products IS NOT INITIAL.
      LOOP AT lt_products INTO ls_products.
        MOVE-CORRESPONDING ls_products TO ls_entityset.
        APPEND ls_entityset TO et_entityset.
      ENDLOOP.
    ENDIF.

 

3. In the above code snippet, we have retrieved the entire list of products.Now we will adjust the code to adapt it to the $skiptoken query option to restrict the amount of data sent to the application by paging in the server.

4. Put the below code in the method PRODUCTSSET_GET_ENTITYSET to respond to the $skiptoken query option.

    DATA:  ls_order          TYPE /iwbep/s_mgw_sorting_order,
           lt_products       TYPE STANDARD TABLE OF bapi_epm_product_header,
           ls_products       TYPE bapi_epm_product_header,
           ls_entityset      TYPE zcl_zdemo_gw_srv_mpc=>ts_products,
           lt_entityset      TYPE zcl_zdemo_gw_srv_mpc=>tt_products,
           lv_max_rows       TYPE bapi_epm_max_rows,
           ls_filter         TYPE /iwbep/s_mgw_select_option,
           lr_product_id     TYPE TABLE OF  bapi_epm_product_id_range,
           ls_product_id     TYPE bapi_epm_product_id_range,
           ls_select_options TYPE /iwbep/s_cod_select_option.

* >> Check if $filter option is available
* Get the filter option for roduct ID
    READ TABLE it_filter_select_options
        INTO ls_filter
        WITH KEY property = 'ProductId'.
    IF sy-subrc = 0.
      LOOP AT ls_filter-select_options INTO ls_select_options.
        MOVE-CORRESPONDING ls_select_options TO ls_product_id.
        APPEND ls_product_id TO lr_product_id.
      ENDLOOP.
    ENDIF.

* Call the BAPI by providing the filter options
* if no filter, BAPI will get the entire list of products
    CALL FUNCTION 'BAPI_EPM_PRODUCT_GET_LIST'
      TABLES
        headerdata        = lt_products
        selparamproductid = lr_product_id.
    IF lt_products IS NOT INITIAL.
      LOOP AT lt_products INTO ls_products.
        MOVE-CORRESPONDING ls_products TO ls_entityset.
        APPEND ls_entityset TO lt_entityset.
      ENDLOOP.
    ENDIF.

    DATA: lv_page_size   TYPE i VALUE 50,  " Define the Page Size/
          lv_index_start TYPE i,
          lv_skiptoken   TYPE string,
          lv_index_end   TYPE i,
          lv_table_size  TYPE i.


* Obtain the $skiptoken value if it exists in the URL
    lv_skiptoken = io_tech_request_context->get_skiptoken( ).

* Clear the skiptoken, if the result is empty
    DESCRIBE TABLE lt_entityset LINES lv_table_size.
    IF lv_table_size IS INITIAL.
      CLEAR es_response_context-skiptoken.
      EXIT.
    ENDIF.

* If the table size is less than the predefined page size,
* send all the data
    IF lv_table_size < lv_page_size.
      et_entityset = lt_entityset.

* If the table size is beyond the predefined page size,
*    cut the whole table into pieces with the page size
    ELSE.
      lv_index_start = lv_skiptoken.
      lv_index_end   = lv_skiptoken + lv_page_size.
    ENDIF.

    LOOP AT lt_entityset INTO ls_entityset.
      IF sy-tabix > lv_index_start AND
         sy-tabix <= lv_index_end.
        APPEND ls_entityset TO et_entityset.
      ENDIF.
    ENDLOOP.

*  Next Link
    es_response_context-skiptoken = lv_index_end + 1.
    CONDENSE es_response_context-skiptoken.

5. What we did in the above step.First we got all the products and then check $skiptoken query option is requested if so, then send only the bulk of data based of the page size.

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 by adding the $skiptoken=10 query option, we should get the only the set of entries(page size) in the response with link to next set of entries. Test the service by providing the following URI with $skiptoken=10.

/sap/opu/odata/sap/ZDEMO_GW_SRV_SRV/ProductsSet?$skiptoken=0′.

/sap/opu/odata/sap/ZDEMO_GW_SRV_SRV/ProductsSet?$skiptoken=10′.

Skiptoken Output

Congrats!! you have successfuly implemented $skiptoken query option in OData service.Stay tuned to us for more SAP Gateway OData tutorials.

Please feel free to comment and let us know your feedback. Subscribe for more updates

If you liked it, please share it! Thanks!