Hello everyone, in this blog we will learn how to use query option $skiptoken in gateway OData service.
To achieve server-side paging we will use $skiptoken.
It is the process of restricting the number of entries sent to the application. This also helps to increase the performance if a huge number of entries requested. This is of two types Server-side paging and Client-side paging.
In client-side paging, the client decides how many records it wants to load and asks the server that many records. That is achieved by using $skip and $top query options
In Server side paging, even if the client requests all the records, the server will only return a portion of the results, but provide a “next link” information, which can be followed to get the next portion and so on. This we can achieve by using $skiptoken query options.
Syntax of $skiptoken
Note-: MaterialSet is the entity set name.
How to Implement $skiptoken query option in OData Service?
1. Open tcode ‘SEGW’ and create one project, by click on the ‘Create Project’ button.
2. one pop up will open. There provide project name, description and package name and click on continue. Your project created successfully.
3.Now we have to create entity and entity set. To create entity and entity set we have to right click on ‘Data Model’ than click on ‘Import’ than click on ‘DDIC Structure’.
4. Now one pop up will open. Enter your entity name, than enter ABAP Structure name ( here I am using MARA table ). ‘Create Default Entity Set’ check box must be checked. Click on ‘Next’ button.
5. New pop up will open, select the fields and click ‘Next’ button.
6. In next pop up choose key field and click ‘Finish’ button.
7. Your entity created .
8. Now click ‘Save’ and ‘Generate Runtime Objects’.
9. Now open DPC_EXT class.
10. If the class open in ‘Display’ mode than convert it to ‘Change’ mode. Select the GET_ENTITYSET method and click on REDEFINE button.
11. Now GET_ENTITYSET method is redefine and we have to write our logic inside the method. First, we have to declare one internal table and one work area. The structure should be same with our entity. we have to fetch data from MARA table into the declared internal table.
12. Now we have to declare the required variable.
LV_PAGE_SIZE – it defines the size of page
LV_TABLE_SIZE – defines the size of internal table which hold all data
LV_SKIPTOKEN – store the skiptoken value from URI
LV_INDEX_START – hold index number
LV_INDEX_END – hold index number
13. Now we have to capture the skiptoken value from URI.
14. If lv_skiptoken variable is empty than send all the records.
15. If lv_skiptoken is not empty than check internal table is empty or not. If internal table is empty than clear the skiptoken value.
16. If the table size is less than defined page size than send all the data else assign value to LV_INDEX_START and LV_INDEX_END.
17. Now we have to send the required data. From LT_ENTITYSET send the records to
ET_ENTITYSET which index number is between LV_INDEX_START and LV_INDEX_END.
18. Now we have to generate ‘Next Link’ information.
19. Now save and activate the program.
20. Again ‘Generate Runtime Objects’. we have to register our service. Open tcode ‘/iwfnd/maint_service’. Click on ‘Add Service’ button.
21. New screen will open. Enter ‘System Alias’ from search help. Click on ‘Get Services’ button than all services name will appear on screen. choose your service name and click on ‘Add Selected Services’ button.
22. One pop up will open. Provide your package name and click continue than provide your tr. Your service registered successfully.
23. Click ok back button and search your registered service. Double click on your service name. click on ‘Gateway Client’ button.
24. In URI, enter the entity set name and $skiptoken value.
HTTP Method = GET
Click ‘Execute’ button
25. It will give only 50 output because we gave page size as 50. To Check the number of record click on ‘Data Explorer’ button.
it will show data like this
26. Check the ‘Next Link’ information, it will be in the last line
to check in original payload click on ‘Original Payload’ button.
after click on ‘Original Payload’ button, you can check here
METHOD MATERIALSET_GET_ENTITYSET. DATA:LT_ENTITYSET TYPE ZCL_ZSKIP_TOKEN_MPC=>TT_MATERIAL, LS_ENTITYSET TYPE ZCL_ZSKIP_TOKEN_MPC=>TS_MATERIAL. 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. SELECT * FROM MARA INTO CORRESPONDING FIELDS OF TABLE LT_ENTITYSET. IF SY-SUBRC = 0. ********Capture the $skiptoken value from URI LV_SKIPTOKEN = IO_TECH_REQUEST_CONTEXT->GET_SKIPTOKEN( ). IF LV_SKIPTOKEN IS INITIAL. ************Send all records********************* LOOP AT LT_ENTITYSET INTO LS_ENTITYSET. APPEND LS_ENTITYSET TO ET_ENTITYSET. ENDLOOP. ENDIF. IF LV_SKIPTOKEN IS NOT INITIAL. ***********Clear skiptoken value, if internal table is empty LV_TABLE_SIZE = LINES( LT_ENTITYSET )."Count the number of records 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 data IF LV_TABLE_SIZE < LV_PAGE_SIZE. LOOP AT LT_ENTITYSET INTO LS_ENTITYSET. APPEND LS_ENTITYSET TO ET_ENTITYSET. ENDLOOP. ELSE. LV_INDEX_START = LV_SKIPTOKEN. LV_INDEX_END = LV_SKIPTOKEN + LV_PAGE_SIZE. ENDIF. ***********Show the output********************* 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. ******* Generate Next Link ES_RESPONSE_CONTEXT-SKIPTOKEN = LV_INDEX_END + 1. CONDENSE ES_RESPONSE_CONTEXT-SKIPTOKEN. ENDIF. ENDIF. ENDMETHOD.
Hope you would find this blog interesting and also help those who wants to learn $skiptoken query option.
Stay safe, stay happy