cancel
Showing results for 
Search instead for 
Did you mean: 

Read the Key of a value from XSOData

former_member242922
Participant
0 Kudos

Hello Experts,

Could someone help me with my below query? Thanks in advance 🙂

Consider the below odata service

d: {  results: [{   __metadata: { type: "", uri: "", },                          ID: 1001,
    Name: "SAP",
    Address: "Germany",
    Phone: "43230986"}

When I make a call to the odata from my view.xml as,

  <Text text= {ID} /> 

  <Text text= {Name} />

  <Text text= {Address} />

  <Text text= {Phone} />

I get my output as ,

1001 SAP Germany 43230986

Is there a way, where I can consume the key itself from the odata?

As in, I should get my output as,

ID Name Address Phone

Accepted Solutions (0)

Answers (3)

Answers (3)

rajkumarnarasimman
Active Contributor
0 Kudos

Hi Shiny,

"I am looking for something of this way, where I can access the metadata via view.xml. I refer to 

It depends on the Odatamodel namespace and UI version which is used. Please refer the API reference for OdataModel

https://sapui5.hana.ondemand.com/#/api/sap.ui.model.odata.ODataModel

In Namespace sap.ui.model.odata and namespace sap.ui.model.odata.v2, method getServiceMetadata is available. If UI version is greater than equal to 1.38, you can use v2 namespace. In the above link which is shared by you, refers the ODATA.V2 namespace, inorder to use it, UI version should match and metadataLoaded event should be trigger to fetch ServiceMetadata else the method return value as undefined as shown below.

If namespace sap.ui.model.odata(Deprecated) is used, We can use the same method getServiceMetadata to fetch the entitytype fields assigned in metadata.

"URL
var surl = "/sap/opu/odata/sap/ZGW_SO_SRV/"; 
"Model
var oModel = new sap.ui.model.odata.ODataModel(surl, true);

By navigating the below path, you can fetch the entity property name

var meta = oModel.getServiceMetadata(); 
var name1 = meta.dataServices.schema[0].entityType[0].property[1].name

Check the detailed path for service metadata in console Output as shown below

Regards

Rajkumar Narasimman

former_member365727
Active Contributor
0 Kudos

use the method read

//Step 1: get instance of model (assuming model name as 'myModel' as defined in manifest.json)
var oModel = this.getOwnerComponent().getModel('myModel');

//Step 2: read data from model(assuming entity set name as 'myEntity' with key field as 'ID')
oModel.read("/myEntity(ID='1001')", {
   success: function(oData, response){
     console.log(oData);   //this contains data
   },
   error: function(oError ){
     console.log("Error in reading Data:", oError);
  }
});

rajkumarnarasimman
Active Contributor
0 Kudos

Hi Shiny,

Use getServiceMetadata to read the Metadata information from the model.

https://www.sap.com/developer/tutorials/xsa-sapui5-metadata.html

Regards

Rajkumar Narasimman

former_member242922
Participant
0 Kudos

Thanks Raj... 🙂

I am looking for something of this way, where I can access the metadata via view.xml. I refer to https://sapui5.hana.ondemand.com/#/sample/sap.ui.table.sample.OData2/code/View.view.xml.

Any help on this?

Shiny