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: 
miltonc
Product and Topic Expert
Product and Topic Expert

Update:  The code for the sample application and the How To Guide for this series is now published.

In my previous blog, I had talked about the onboarding process to uniquely identify a device with the SMP Server.  The next logical progression in this blog series is to write about how devices can retrieve data from the backend and also how devices can make changes to the backend data.  As part of this blog, I will try and post code for another sample application that illustrates how to retrieve data and make changes to backend using the SAP Mobile Platform SDK (hereinafter referred to as “SMP SDK” or “SDK”).

CRUD operations

In order to retrieve data from the backend, an HTTP GET request is made along with the application connection id in the headers.

HTTP Request

URL: http://<hostname>:<port>/<appid>/<CollectionName>

Method: GET

Header: { X-SMP-APPCID : <app connection id> }



To accomplish this using the SMP SDK, the following steps need to be taken.  Creating an ODataStore instance and calling OpenAsync is only done once for a session.  From there on, it’s a matter of calling ScheduleReadEntitySet method any number of times.



The ODataStore library is used to interact with the SMP Server to submit HTTP requests to either GET data or perform CUD operations.  An ODataStore instance is required to interact with an OData Service.  The ODataStore hides a lot of complexities and makes interacting with OData source fairly easy for the developer.   To create an instance of the ODataStore, the developer can use either one of the 2 constructors.  The default value for the 2nd EntityFormat parameter is XML format. However, using JSON format considerably reduces the network traffic.


public ODataStore(string serviceUri, ODataStore.EntityFormat entityFormat = ODataStore.EntityFormat.XML);
public ODataStore(Uri serviceUri, ODataStore.EntityFormat entityFormat = ODataStore.EntityFormat.XML);
public enum EntityFormat
{
    JSON = 0,
    XML = 1,
}
// Sample code snippet for creating an ODataStore
var store = new ODataStore(uri);


Once an instance of ODataStore is created, the method OpenAsync is called.  This method retrieves the service document and the metadata document.   When making the OpenAsync call, it is also necessary to pass in the user credentials and the application connection id as header values.


var client = new SAP.Net.Http.HttpClient( new System.Net.Http.HttpClientHandler { Credentials = new NetworkCredential(“user", “password") }, true);
client.DefaultRequestHeaders.TryAddWithoutValidation("X-SMP-APPCID",appconnid);
await store.OpenAsync(client);


The ScheduleReadEntitySet method is used to schedule an HTTP GET request.  This method takes the collection name as a parameter.  The Response object is then called asynchronously to submit the request.


var execution = store.ScheduleReadEntitySet(collectionName);
var response = await execution.Response;


The response object is then cast as an ODataEntitySet and can be immediately bound to an UI control.  The ODataEntitySet is an IObservableCollection which allows the UI controls to automatically update themselves when the collection is changed.


var response = await execution.Response;
this.EntitySet = (SAP.Data.OData.Online.ODataEntitySet)((IODataResponseSingle)response).Payload;
// Bind this.EntitySet directly to UI controls


Making changes to the backend

The ScheduleCreateEntity method is used to create an entity in the backend.  This method takes an ODataEntity and the CollectionName as parameters.  An entity is created locally and passed in as parameter to the ScheduleCreateEntity method.


var entity = new SAP.Data.OData.Online.ODataEntity(“TypeName");
entity.Properties["ID"].Value = XYZ;
entity.Properties["Name"].Value = “<XYZ>";
store.AllocateProperties(entity, SAP.Data.OData.Store.PropertyCreationMode.All);
var execution = store.ScheduleCreateEntity(entity, collectionName);
var response = await execution.Response;


Similarly, the ScheduleUpdateEntity is used to update an entity in the backend.  This method takes an ODataEntity as a parameter.  It is recommended to use the DeepCopy() method to make a real copy of an entity (not just a reference copy).  Make the necessary updates on the temporary ODataEntity. This way, even if the update operation fails (network disconnects, server error or whatever), the original entity is still intact and the app would not be in an inconsistent state.


var copiedEntity = entity.DeepCopy();      
copiedEntity.Properties[“Name"].Value = newName;
var execution = store.ScheduleUpdateEntity(copiedEntity);
var response = await execution.Response;


The ScheduleDeleteEntity method is used to delete an entity in the backend.



var execution = store.ScheduleDeleteEntity(entity);
var response = await execution.Response;


Update:  The code for the sample application and the How To Guide is now published.  The Windows Store Flight Sample Application Series illustrates the various techniques involved in creating an online Windows Store application using the Windows SMP 3.0 SP05 SDK.  This sample application series uses the universal Windows app project templates to build apps that can run on Windows Phones, tablets, laptops and workstations.

In conclusion, in this blog I have highlighted some of the important methods involved in performing CRUD operations against an OData Source using the SAP Mobile Platform.  This current version of SMP SDK only supports online functionality for Windows.  However, future releases of the SMP SDK will support additional functionality.  In upcoming blogs, I will talk more about the newer functionality and also provide more code samples to help build Windows applications using the new SAP Mobile Platform SDK.  Also, if you have questions building Windows applications, please feel free to reach out to me.

24 Comments