Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member215851
Participant
0 Kudos

Introduction: What is $skiptoken?

When working with OData services, a developer might use the $top and $skip commands so that the client application can control paging.

However, some client applications might be coded inefficiently or even with malicious intent, so you might like to enforce Server Side Paging supported by the OData protocol. This means that the server does not return all results at once, but only a subset or “page” of all results.

If you choose to implement this, the client usually needs to know how many results there as a whole. To find this out, you can append the $inlinecount option to your query, like this:

http://<server>/ODataService/EntitySet?$inlinecount=allpages

The results will include a total count ‘inline’, and a url generated to get the next page of results.

The generated url includes a $skiptoken, that is the equivalent of a cursor or bookmark, that instructs the server where to resume:

http://<server>/ODataService/EntitySet?$skiptoken=4

Note: This feature is available for SP07 onwards.

Implementing a custom JavaScript to support Server Side Paging for your SOAP data source

In order to support the scenario above, $top and $skip are most likely used along with $skiptoken. Hence the operation should handle $top, $skip and $skiptoken OData operation together.

Steps in the SAP Mobile Platform Tools

  1. Create an OData Service Implementation Project
  2. Create an OData Model that includes a “Products” entity set for getProduct operation, which can have properties ProductID,ProductName, ProductDescription,,SupplierId,isDeleted

3.  Right click on odatasrv file on the lefthand side of your Service Implementation Project and choose “Select data source”.

4. Select your entity set and choose the query CRUD operation. Select the data source SOAP Service.

5. Specify your required wsdl file and choose the required query operation from the list of soap operations and click on finish


6. Right click on Query and select Define Custom Code

7. Select the script type as either JavaScript or Groovy script. In this example we chose to implement the custom code using JavaScript.

8. The skip token value has to be fetched from the OData request URI and this value must be sent to the web service operation as an input parameter. A hashmap must be created in the processRequestData function with the input parameters (skipTokenID, top, skip) that will be passed in the URI. These parameters must then be set to message body as follows:

function
processRequestData(message) {

     
importPackage(com.sap.gateway.ip.core.customdev.api);
     
importPackage(java.util);
    
importPackage(com.sap.gateway.core.ip.component.commons);

importPackage(com.sap.gateway.ip.core.customdev.logging);    
importPackage(com.sap.gateway.ip.core.customdev.util);

     var uriInfo = message.getHeaders().get(ODataExchangeHeaderProperty.UriInfo.toString());

      var top = "";

      var skip = "";

      var productId = "";

      if (uriInfo.getTop() !=null)

             top = uriInfo.getTop();

      if (uriInfo.getSkip()!= null)

                skip = uriInfo.getSkip();

        if (uriInfo.getSkipToken() != null)

                productId = uriInfo.getSkipToken();

  

      var parentMap = new LinkedHashMap();

     // Add the Top and Skip productId and skipToken values to map

           parentMap.put("ProductId", productId);

           parentMap.put("top", top);

           parentMap.put("skip", "");

           parentMap.put("skiptoken", "2");

    // Logger

           log.logErrors(LogMessage.TechnicalError, "Values set correctly");

    // Set the message body back

           message.setBody(parentMap);

     return message;

}

9. In our sample implementation, the OData request would look as follows:

https://localhost:8083/gateway/odata/SAP/SKIPTOKEN;v=1/ProductSet?$skipToken=101

The resulting outgoing SOAP request body should then look as follows:

 
<soapenv:Body>

  <soapenv:Body>

     <web:getProducts>

     <web:ProductId>101</web:ProductId>

     <web:top></web:top>

     <web:skip></web:skip>

            <web:skiptoken>2</web:skiptoken>

           </web:getProducts>

  </soapenv:Body>

In this example, the $skipToken value equals 101 which is the ProductID of the last product. $skipToken value = 2 means, you will get only 2 entries at a time. This returns the next two entries. In this operation, moreEntries gives the information, if there are some more entries still there or not.

 
<soapenv:Body>

<getProductsResponse xmlns="http://webservice.SalesOrder.test.org">
<getProductsReturn>
<moreEntries>true</moreEntries>
<prod>            
<PRODUCTID>103</PRODUCTID>            
<ProductDesc>5L Olive Oil Can</ProductDesc>             
<SupplierId>10</SupplierId>

</prod>
<prod>
<PRODUCTID>104</PRODUCTID>
<ProductDesc>FreshAir Gum</ProductDesc>

<SupplierId>11</SupplierId>         
</prod>

</getProductsReturn>   
</getProductsResponse>

</soapenv:Body>

10. Once the web service response is returned, the response includes the entries and information whether there exist some more entries. These entries have to be added in the result set in processResponseData function. Also, we need to set the next the $skipToken value using the
following code:

     message.setHeader(ODataExchangeHeaderProperty.SkipToken.toString(), token);

11. Right click on Query and select Define Response Mapping

12. Do the response mappings from your entity set to your WSDL file as required.

13. Right Click on your Service Implementation Project and select “Generate and Deploy Integration Content”. This will deploy the bundle to your
SMP server.

Now fire an OData Request https://localhost:8083/gateway/odata/SAP/SKIPTOKEN;v=1/ProductSet    on the browser and response will give the list of entries with link tag having a value similar to this: ?$skiptoken='MYID'

The support of skipToken and its implementation depends on web service, so you will have to adapt this example for your specific service.