cancel
Showing results for 
Search instead for 
Did you mean: 

Binding OData to XML view

JBrook
Explorer
0 Kudos
723

I have a simple requirement to get OData from the backend to populate the sap.m.select on my XML view. It works if I use a hard code json array in the app. or if I use a JS view but not with an XML view.

In my controller.js I have:

var sServiceUrl = "/sap/opu/odata/sap/ZRE_FACILITY_SRV/";
var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);

OData.request
({
requestUri: "/sap/opu/odata/sap/ZRE_FACILITY_SRV/BusEntitySet",
method: "GET",
headers:
{
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/atom+xml",
"dataType" : "json",
"DataServiceVersion": "2.0"
}
},
function (oData, response) {

var oModel1 = new sap.ui.model.json.JSONModel();
oModel1.setSizeLimit(oData.results.length);
oModel1.setData(oData.results);
var oSelect = this.getView().byId("SelectID");
oSelect.setModel(oModel1, "appData1");


//oModel1.setProperty("/modelData", oData.results);
//var oSelect = this.getView().byId("SelectID");
//var oTemplate = new sap.ui.core.Item({key: "{Swenr}", text: "{Xwetext}" });
//oSelect.setModel(oModel1);
//oSelect.bindAggregation("items", "/modelData", oTemplate);

},
function (err) {alert("Error in getting Data");
} );

then in my view.xml I have

<Label text="Business entity" />
<Select id="SelectID" items="{ path: 'appData1>/BusEntitySet'}">
<core:Item key="{appData1>Swenr}" text="{appData1>Xwetext}" />
</Select>

Accepted Solutions (0)

Answers (2)

Answers (2)

CristianBabei
Contributor

Hi,

If you are using SAPUI5 and calling an odata service, you can use all the methos of the model.

var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);

The ODataModel, has a own method for reading, (Also, I strongly recommend you to use OData.v2), an example of a read request would be :

var oModel = new sap.ui.model.odata.v2.ODataModel(sServiceUrl, true);
oModel.read("/EntitySet",{
     success : function(oData){
     alert("Success");<br>     //...
     },

     error : function(oEvent){
     //....
     }
});

Try this 🙂

Hope it helps

former_member365727
Active Contributor
0 Kudos

this seems to be very much old kind of approach on accessing oData. Why not use manifest.json for instantiating oData models...and you can perform the binding to xml view