Experiences with SAP Gateway

"My foots always in my mouth i just can't stomach defeet" – Hilltop Hoods

Posting to Gateway with SAPUI5

leave a comment »

It has been a while since my last post, been busy building apps on Android.

Anyway thought I would quickly share how easy it is to post to SAP Netweaver Gateway using the SAPUI5 library.

Image

To start with you need to reference the datajs library.

jQuery.sap.require("sap.ui.model.odata.datajs");

Once you have referenced it, you can use the library to post as follows.

For this example I extended the existing “New Contact” popup form on the Shell layout to capture the needed input, to get the values from the popup I did the following

var firstName = sap.ui.getCore().byId("firstNameTextField").getValue(),
lastName = sap.ui.getCore().byId("lastNameTextField").getValue(),
handle = sap.ui.getCore().byId("handleTextField").getValue(),
email = sap.ui.getCore().byId("emailTextField").getValue(),
status = sap.ui.getCore().byId("statusTextField").getValue();

I then used the values to create the data entry object

var contactEntry = {Handle: handle,
FirstName: firstName,
LastName: lastName,
Email: email,
Status: status};

Declare a variable the service uri

var serviceURI = "http://<host>:<port>/sap/opu/sdata/sap/<service>/Contacts";

Then build a request based on the form data and service uri, making sure you add the applicable XML headers

var request =
{ headers: {"X-Requested-With": "XMLHttpRequest",
"Accept": "application/atom+xml,application/atomsvc+xml,application/xml",
"Content-Type": "application/atom+xml",
"DataServiceVersion": "2.0" },
requestUri: serviceURI,
method: "POST",
user: "developer",
password: "ch4ngeme",
data: contactEntry };

Finally post the request, passing the callback functions
OData.request( request,
function (data) {
//Success Callback
sap.ui.commons.MessageBox.show("New contact saved successfully.", sap.ui.commons.MessageBox.Icon.SUCCESS, "Contact Saved", sap.ui.commons.MessageBox.Action.OK);
},
function (err) {
//Error Callback:
..
}
)

I am hoping with future releases this can be incorporated into the sap.ui.model.odata.ODataModel

Advertisement

Written by rsol1

April 5, 2012 at 3:34 pm

Posted in Uncategorized

Tagged with , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: