0

We are creating the Master Details UI5 app. I am calling the OData service and able to display the data from an entity set in Master list. On detail section, we are creating a form and I want to display the data which is coming from Navigation entity set. As I came to know that we can not call navigation entity set in a single call, how can it be done?

<EntityType Name="Product" sap:content-version="1">
    <Key>
        <PropertyRef Name="ProductID"/>
    </Key>
    <Property Name="ProductID" Type="Edm.String" Nullable="false" MaxLength="10" sap:label="Product ID" sap:updatable="false"/>
    <Property Name="TypeCode" Type="Edm.String" Nullable="false" MaxLength="2" sap:label="Type Code"/>
    <NavigationProperty Name="ToSalesOrderLineItems" Relationship="GWSAMPLE_BASIC.Assoc_Product_SalesOrderLineItems" FromRole="FromRole_Assoc_Product_SalesOrderLineItems" ToRole="ToRole_Assoc_Product_SalesOrderLineItems"/>
    <NavigationProperty Name="ToSupplier" Relationship="GWSAMPLE_BASIC.Assoc_BusinessPartner_Products" FromRole="ToRole_Assoc_BusinessPartner_Products" ToRole="FromRole_Assoc_BusinessPartner_Products"/>
</EntityType> 

I would like to show the data of ToSupplier.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Mayank Saxena
  • 89
  • 1
  • 12
  • 28
  • Does this answer your question? [How to make use of navigation property?](https://stackoverflow.com/questions/52483541/how-to-make-use-of-navigation-property) – Boghyon Hoffmann Jul 17 '21 at 12:20

1 Answers1

3

Well, you can get it in a single request actually.

Lets say that your "BusinessPartner" entity has a name field and you want to display something like this:

<Panel>
   <Text id="txtProductID" text="Product ID Comes Here"/>
   <Text id="txtSupplierName" text="Supplier BP Name Comes Here"/>
</Panel>

What you can do, is to use regular binding syntax (with relative bindings), as it is usually done in details views:

<Panel>
   <Text id="txtProductID" text="{ProductID}"/>
   <Text id="txtSupplierName" text="{ToSupplier/Name}"/>
</Panel>

If you would try this directly, you would not get anything in the "Supplier Name" text control. This is because, by default you are not requesting the ToSupplier navigation to be expanded (check out chapter 4.6 from the OData Spec).

You should do this where you are calling bindElement on the detail view (in the default template, this is done in a method called _bindView of the detail controller). The expand can be passed as a parameter to the binding itself like so:

oView.bindElement({
    path: sMyPathToAProduct,
    parameters: {
        expand: "ToSupplier"
    }
});

You can expand as many navigations as you like and as deep as you like (you can pass the navigations as a comma separated list in that parameter). The only constraint here is that your backend should support expands / might have some limitations on how deep you can go with expands.

Serban Petrescu
  • 5,127
  • 2
  • 17
  • 34
  • Another question: What If I would like to get the values from ToSupplier in my Master's Controller and would like to print that in console? – Mayank Saxena Jun 15 '17 at 20:45
  • You have two options: either you expand the `ToSupplier` navigation for all the items of the master list (you can do this directly in the XML, when you are binding the items, e.g. `items="{path: '/ProductSet', parameters: {expand: 'ToSupplier'}}"`). Then you can obtain the data itself from the binding context of the items. Or you can do a programmatic read when you need the data (look at [ODataModel#read](https://openui5.hana.ondemand.com/#docs/api/symbols/sap.ui.model.odata.v2.ODataModel.html#read)). – Serban Petrescu Jun 15 '17 at 20:54