Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Hi Everyone,

Recently i gone through OData Read, Create, Update and Delete operations in SMP 3.0 Native ODataSDK.

here is a sample code for a OData service


http://sapes1.sapdevcenter.com:8080/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/TravelagencyCollection

GET Request Format

[RequestBuilder setRequestType:HTTPRequestType];

  

// Enable XCSRF handling for OData requests

[RequestBuilder enableXCSRF:YES];

  

// Initialize the Requesting class with the endpoint URL

id<Requesting> getRequest = [RequestBuilder requestWithURL:[[NSURL alloc] initWithString: appEndPoint]];

  

// Add username and password for the end point

[getRequest setUsername:username];

[getRequest setPassword:password];

   

/// Set our request headers.

[getRequest setRequestMethod:@"GET"];

[getRequest addRequestHeader:@"X-Requested-With" value:@"XmlHttpRequest"];

[getRequest addRequestHeader:@"Content-Type" value:@"application/xml; charset=UTF-8"];

[getRequest addRequestHeader:@"Accept" value:@"application/xml,application/atom+xml"];

  

// This class is the delegate callback for any notifications (success and failure)

[getRequest setDelegate:self];

[getRequest setDidFinishSelector:@selector(handleReqSuccess:)];

[getRequest setDidFailSelector:@selector(handleReqFailed:)];

  

// Each request can be tagged with a number.

getRequest.requestTag = tag; // Useful for a change request.

  

// Start our request.

[getRequest startAsynchronous];


POST Request Format

[RequestBuilder setRequestType:HTTPRequestType];

[RequestBuilder enableXCSRF:YES];

id<Requesting> postRequest = [RequestBuilder requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",appEndPoint,collectionName]]];

[postRequest setRequestMethod:@"POST"];

  

[postRequest addRequestHeader:@"Content-Type" value:@"application/json; charset=UTF-8"];

[postRequest addRequestHeader:@"Accept" value:@"*/*"];

// Here cSRFToken is retrieved from the GET Response.

[postRequest addRequestHeader:@"x-csrf-token" value: cSRFToken];

NSMutableData* postData;

// GetEntry is an ODataObject which has the response for GET done on single entry from a collection

ODataEntry const *postBody = [[ODataEntry alloc]initWithEntitySchema:[[[self.serviceDocument getSchema] getCollectionByName:self.entityName] getEntitySchema]];

  

ODataPropertyValueObject* filterValue = [postBody getPropertyValueByPath:@"agencynum"];  ///agencynum is the primary key or the unique value

   

          filterValue.rawValue=@"00000127";

    

ODataPropertyValueObject* filterValue1 = [postBody getPropertyValueByPath:@"agencyname"];

    

filterValue1.rawValue=@"BestAgency";

  

//The following code can be used to build a JSON post body:

ODataEntryBody *entryBody = buildODataEntryRequestBody(postBody, ENTRY_OPERATION_CREATE, self.serviceDocument, NO, BUILD_STYLE_JSON);  

      

postData = [[entryBody.body dataUsingEncoding:NSUTF8StringEncoding]mutableCopy];

[postRequest addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%i",[postData length]]];

[postRequest setPostBody:postData];

[postRequest setDelegate:self];

  

// Each request can be tagged with a number.

          getRequest.requestTag = tag; // Useful for a change request.

// Start our request.

[postRequest startAsynchronous];

PUT Request Format


[RequestBuilder setRequestType:HTTPRequestType];

  

[RequestBuilder enableXCSRF:YES];

  

id<Requesting> putRequest = [RequestBuilder requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@(%@)",appEndPoint,collectionName,agencynum]]];

  

[putRequest setRequestMethod:@"PUT"];

[putRequest addRequestHeader:@"Content-Type" value:@"application/json; charset=UTF-8"];

  

[putRequest addRequestHeader:@"Accept" value:@"*/*"];

  

[putRequest addRequestHeader:@"x-csrf-token" value:cSRFToken];

  

NSMutableData *postData;

ODataEntry const *postBody = [[ODataEntry alloc]initWithEntitySchema:[[[self.serviceDocument getSchema] getCollectionByName:self.entityName] getEntitySchema]];

ODataPropertyValueObject* filterValue = [postBody getPropertyValueByPath:@"agencyname"];

  

filterValue.rawValue=@"NewAgencyName";

  

//The following code can be used to build a JSON post body:

ODataEntryBody *entryBody = buildODataEntryRequestBody(postBody, ENTRY_OPERATION_CREATE, self.serviceDocument, NO, BUILD_STYLE_JSON);

      

postData = [[entryBody.body dataUsingEncoding:NSUTF8StringEncoding]mutableCopy];

  

[putRequest addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%i",[postData length]]];

  

[putRequest setPostBody:postData];

  

[putRequest setDelegate:self];

  

putRequest.requestTag = tag;

  

// Start our request.

  [putRequest startAsynchronous];


DELETE Request Format


[RequestBuilder setRequestType:HTTPRequestType];

  

[RequestBuilder enableXCSRF:YES];

  

id<Requesting> deleteRequest = [RequestBuilder requestWithURL:[NSURL URLWithString:[NSString stringWithFormat@"%@/%@(%@)",appEndPoint,collectionName,agencynum]]];

  

[deleteRequest setRequestMethod:@"Delete"];

  

[deleteRequest addRequestHeader:@"Content-Type" value:@"application/json; charset=UTF-8"];

  

[deleteRequest addRequestHeader:@"Accept" value:@"*/*"];

  

[deleteRequest addRequestHeader:@"x-csrf-token" value:self.cSRFToken];

  

[deleteRequest setDelegate:self];

  

deleteRequest.requestTag = tag;

  

// Start our request.

[deleteRequest startAsynchronous];


Regards

Siva Chandu

15 Comments
Labels in this area