Skip to content

OData Read

The ODataReadCL class leverages the sap.ui.model.odata.v2.ODataModel to facilitate the handling of GET (READ) requests in a promisified manner.

Constructor

In order to utilise the functionality of ODataReadCL, it is necessary to initialise the object.

Parameter Type Mandatory Default Value Description
controller sap.ui.core.mvc.Controller Yes The controller object (usually this object)
entityPath string Yes The name of the EntitySet. It can start with a "/" (slash)
modelName? string No undefined The name of the OData V2 model which can be found on the manifest.json file. Leave this parameter undefined if the name of the OData model = "" (empty string)

Tip for TypeScript

The ODataReadCL<EntityT, EntityKeysT> is a generic class and can be initialized with 2 types.

  • The EntityT type contains all properties of the EntitySet that is specified on the class constructor.
  • The EntityKeysT type contains the key properties of the EntitySet that is specified on the class constructor.

The EntityT type is used as the returning type of the read(): Promise<EntityT[]> and readByKey(keys: EntityKeysT): Promise<EntityT> methods.

The EntityKeysT type is used as the type for the keys parameter of the readByKey(keys: EntityKeysT): Promise<EntityT> method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Controller from "sap/ui/core/mvc/Controller";
import ODataReadCL from "ui5/antares/odata/v2/ODataReadCL"; // Import the class

/**
 * @namespace your.apps.namespace
 */
export default class YourController extends Controller {
  public onInit() {

  }

  public async onReadProduct() {
    // Initialize without a type
    const odata = new ODataReadCL(this, "Products"); 
  }

  public async onReadCategory() {
    // Initialize with a type
    const odata = new ODataReadCL<ICategory, ICategoryKeys>(this, "Categories"); 
  }

  public async onReadCustomer() {
    // Initialize with a model name
    const odata = new ODataReadCL(this, "Customers", "myODataModelName"); 
  }
}

interface ICategory {
  ID: string;
  name: string;
}

interface ICategoryKeys {
  ID: string;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "ui5/antares/odata/v2/ODataReadCL" // Import the class
], 
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, ODataReadCL) {
      "use strict";

      return Controller.extend("your.apps.namespace.YourController", {
        onInit: function () {

        },

        onReadProduct: async function () {
          // Initialize
          const odata = new ODataReadCL(this, "Products"); 
        },

        onReadCategory: async function () {
          // Initialize with a model name
          const odata = new ODataReadCL(this, "Categories", "myODataModelName");
        }
      });

    });

Read Request (GET EntitySet)

To send a GET (READ) request through the OData V2 model, you can use the read(): Promise<EntityT[]> method.

Info

  • The read() method runs asynchronously and can be awaited.
  • If the request is successful, the read() method will return the data (multiple) of the target entity.

Tip

By default, the read() method of the sap.ui.model.odata.v2.ODataModel class returns an object that contains a single property named results. However, the OData Read class destructs the original object and returns the value of the results property which is an array containing the data of the EntitySet. The return type is derived from the generic EntityT type.

Warning

In the event of a failed GET request, the OData Read class will generate an error message. To ensure the error is identified and addressed, it is recommended to call the read() method within a try-catch block.

Error Type

In the event of a failed GET request, the object generated by the class can contain the properties outlined below.

Returns Description
object
    headers?: object | undefined The HTTP response headers.
    message?: string | undefined The HTTP response message.
    responseText?: string | undefined The HTTP response text.
    statusCode?: string | number | undefined The status code of the HTTP request.
    statusText?: string | undefined The HTTP status text.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Controller from "sap/ui/core/mvc/Controller";
import ODataReadCL from "ui5/antares/odata/v2/ODataReadCL"; // Import the class
import { IError } from "ui5/antares/types/common"; // Import the error type
import MessageBox from "sap/m/MessageBox";

/**
 * @namespace your.apps.namespace
 */
export default class YourController extends Controller {
  public onInit() {

  }

  public async onReadProducts() {
    // Initialize with a type
    const odata = new ODataReadCL<IProducts, IProductKeys>(this, "Products"); 

    try {
      // send the http request and get the result.
      const result = await odata.read();

      result.forEach((row: IProducts) => {
        // write your logic
      });
    } catch (error) {
      // catch the error
      MessageBox.error((error as IError).message || "Request failed");
    }
  }

}

interface IProducts {
  ID: string;
  name: string;
  description: string;
  brand: string | null;
  price: number;
  currency: string;
  quantityInStock: number;
  categoryID: string | null;
  supplierID: string | null;
}

interface IProductKeys {
  ID: string;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "ui5/antares/odata/v2/ODataReadCL", // Import the class
    "sap/m/MessageBox"
], 
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, ODataReadCL, MessageBox) {
      "use strict";

      return Controller.extend("your.apps.namespace.YourController", {
        onInit: function () {

        },

        onReadProducts: async function () {
          // Initialize
          const odata = new ODataReadCL(this, "Products"); 

          try {
            // send the http request and get the result.
            const result = await odata.read();

            result.forEach((row) => {
              // write your logic
            });
          } catch (error) {
            // catch the error
            MessageBox.error(error.message || "Request failed");
          }          
        }
      });

    });

Read By Key Request (GET Entity)

To send a GET (READ) request to retrieve a single data through the OData V2 model, you can use the readByKey(keys: EntityKeysT): Promise<EntityT> method.

Info

  • The readByKey() method runs asynchronously and can be awaited.
  • If the request is successful, the readByKey() method will return the data (single) of the target entity.

Warning

In the event of a failed GET request, the OData Read class will generate an error message. To ensure the error is identified and addressed, it is recommended to call the readByKey() method within a try-catch block.

Error Type

In the event of a failed GET request, the object generated by the class can contain the properties outlined below.

Returns Description
object
    headers?: object | undefined The HTTP response headers.
    message?: string | undefined The HTTP response message.
    responseText?: string | undefined The HTTP response text.
    statusCode?: string | number | undefined The status code of the HTTP request.
    statusText?: string | undefined The HTTP status text.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import Controller from "sap/ui/core/mvc/Controller";
import ODataReadCL from "ui5/antares/odata/v2/ODataReadCL"; // Import the class
import { IError } from "ui5/antares/types/common"; // Import the error type
import MessageBox from "sap/m/MessageBox";

/**
 * @namespace your.apps.namespace
 */
export default class YourController extends Controller {
  public onInit() {

  }

  public async onReadSingleProduct() {
    // Initialize with a type
    const odata = new ODataReadCL<IProducts, IProductKeys>(this, "Products"); 

    try {
      // send the http request and get the result. Note: you need to specify the key values of the entity to read a single data
      const result = await odata.readByKey({
        ID: "f60481d8-3426-4a91-a6a3-5e445a7deb46"
      });

      MessageBox.information("Product with ID: " + result.ID);
    } catch (error) {
      // catch the error
      MessageBox.error((error as IError).message || "Request failed");
    }
  }

}

interface IProducts {
  ID: string;
  name: string;
  description: string;
  brand: string | null;
  price: number;
  currency: string;
  quantityInStock: number;
  categoryID: string | null;
  supplierID: string | null;
}

interface IProductKeys {
  ID: string;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "ui5/antares/odata/v2/ODataReadCL", // Import the class
    "sap/m/MessageBox"
], 
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, ODataReadCL, MessageBox) {
      "use strict";

      return Controller.extend("your.apps.namespace.YourController", {
        onInit: function () {

        },

        onReadProduct: async function () {
          // Initialize
          const odata = new ODataReadCL(this, "Products"); 

          try {
            // send the http request and get the result. Note: you need to specify the key values of the entity to read a single data
            const result = await odata.readByKey({
              ID: "f60481d8-3426-4a91-a6a3-5e445a7deb46"
            });

            MessageBox.information("Product with ID: " + result.ID);
          } catch (error) {
            // catch the error
            MessageBox.error(error.message || "Request failed");
          }          
        }
      });

    });

URL Parameters

Prior to sending the GET request with the read() or readByKey() method, it is possible to set the URL parameters using the setUrlParameters() method.

Parameter Type Mandatory Description
urlParameters Record<string, string> Yes The URL parameters of the GET request

Returns Description
Record<string, string> | undefined Returns the value that was set using setUrlParameters() method. Default value is undefined

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Controller from "sap/ui/core/mvc/Controller";
import ODataReadCL from "ui5/antares/odata/v2/ODataReadCL"; // Import the class

/**
 * @namespace your.apps.namespace
 */
export default class YourController extends Controller {
  public onInit() {

  }

  public async onReadProduct() {
    // Initialize with a type
    const odata = new ODataReadCL<IProducts, IProductKeys>(this, "Products"); 

    // set the url parameters
    odata.setUrlParameters({
      "$expand": "toProductLocations"
    });
  }

}

interface IProducts {
  ID: string;
  name: string;
  description: string;
  brand: string;
  price: number;
  currency: string;
  quantityInStock: number;
  categoryID: string;
  supplierID: string;
}

interface IProductKeys {
  ID: string;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "ui5/antares/odata/v2/ODataReadCL" // Import the class
], 
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, ODataReadCL) {
      "use strict";

      return Controller.extend("your.apps.namespace.YourController", {
        onInit: function () {

        },

        onReadProduct: async function () {
          // Initialize
          const odata = new ODataReadCL(this, "Products"); 

          // set the url parameters
          odata.setUrlParameters({
            "$expand": "toProductLocations"
          });         
        }
      });

    });

Filters

To execute the read() method with filters, two different methods are available.

This method enables the user to add filters individually.

Parameter Type Mandatory Description
filter Filter Yes The filter object

This method enables users to apply multiple filters simultaneously.

Attention

This method overwrites all the filters that have been added with the addFilter() method or set with the setFilters() method.

Parameter Type Mandatory Description
filters Filter[] Yes The array of filter objects

Returns Description
Filter[] Returns all the filters that were added using the addFilter() method or set using the setFilters() method. Default value is []

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import Controller from "sap/ui/core/mvc/Controller";
import ODataReadCL from "ui5/antares/odata/v2/ODataReadCL"; // Import the class
import Filter from "sap/ui/model/Filter"; // Import Filter class
import FilterOperator from "sap/ui/model/FilterOperator"; // Import FilterOperator enum

/**
 * @namespace your.apps.namespace
 */
export default class YourController extends Controller {
  public onInit() {

  }

  public async onReadProduct() {
    // Initialize with a type
    const odata = new ODataReadCL<IProducts, IProductKeys>(this, "Products"); 
    const nameFilter = new Filter("name", FilterOperator.Contains, "smartphone");
    const priceFilter = new Filter("price", FilterOperator.GT, 1500);

    // add the filters one by one
    odata.addFilter(nameFilter);
    odata.addFilter(priceFilter);
  }

  public async onReadCategory() {
    // Initialize
    const odata = new ODataReadCL(this, "Categories"); 
    const nameFilter = new Filter("name", FilterOperator.Contains, "smartphone");
    const priceFilter = new Filter("price", FilterOperator.GT, 1500);

    // set the filters at once
    odata.setFilters([nameFilter, priceFilter]);
  }

}

interface IProducts {
  ID: string;
  name: string;
  description: string;
  brand: string;
  price: number;
  currency: string;
  quantityInStock: number;
  categoryID: string;
  supplierID: string;
}

interface IProductKeys {
  ID: string;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "ui5/antares/odata/v2/ODataReadCL", // Import the class
    "sap/ui/model/Filter", // Import Filter class
    "sap/ui/model/FilterOperator" // Import FilterOperator enum
], 
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, ODataReadCL, Filter, FilterOperator) {
      "use strict";

      return Controller.extend("your.apps.namespace.YourController", {
        onInit: function () {

        },

        onReadProduct: async function () {
          // Initialize
          const odata = new ODataReadCL(this, "Products"); 
          const nameFilter = new Filter("name", FilterOperator.Contains, "smartphone");
          const priceFilter = new Filter("price", FilterOperator.GT, 1500);

          // add the filters one by one
          odata.addFilter(nameFilter);
          odata.addFilter(priceFilter);        
        },

        onReadCategory: async function () {
          // Initialize
          const odata = new ODataReadCL(this, "Categories"); 
          const nameFilter = new Filter("name", FilterOperator.Contains, "smartphone");
          const priceFilter = new Filter("price", FilterOperator.GT, 1500);

          // set the filters at once
          odata.setFilters([nameFilter, priceFilter]);          
        }
      });

    });

Sorters

To execute the read() method with sorters, two different methods are available.

This method enables the user to add sorters individually.

Parameter Type Mandatory Description
sorter Sorter Yes The sorter object

This method enables users to apply multiple sorters simultaneously.

Attention

This method overwrites all the sorters that have been added with the addSorter() method or set with the setSorters() method.

Parameter Type Mandatory Description
sorters Sorter[] Yes The array of sorter objects

Returns Description
Sorter[] Returns all the sorters that were added using the addSorter() method or set using the setSorters() method. Default value is []

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Controller from "sap/ui/core/mvc/Controller";
import ODataReadCL from "ui5/antares/odata/v2/ODataReadCL"; // Import the class
import Sorter from "sap/ui/model/Sorter"; // Import the Sorter class

/**
 * @namespace your.apps.namespace
 */
export default class YourController extends Controller {
  public onInit() {

  }

  public async onReadProduct() {
    // Initialize with a type
    const odata = new ODataReadCL<IProducts, IProductKeys>(this, "Products"); 
    const nameSorter = new Sorter("name");
    const priceSorter = new Sorter("price", true); // descending

    // add the sorters one by one
    odata.addSorter(nameSorter);
    odata.addSorter(priceSorter);
  }

  public async onReadCategory() {
    // Initialize
    const odata = new ODataReadCL(this, "Categories"); 
    const nameSorter = new Sorter("name");
    const priceSorter = new Sorter("price", true); // descending

    // set the sorters at once
    odata.setSorters([nameSorter, priceSorter]);
  }

}

interface IProducts {
  ID: string;
  name: string;
  description: string;
  brand: string;
  price: number;
  currency: string;
  quantityInStock: number;
  categoryID: string;
  supplierID: string;
}

interface IProductKeys {
  ID: string;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "ui5/antares/odata/v2/ODataReadCL", // Import the class
    "sap/ui/model/Sorter" // Import the Sorter class
], 
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, ODataReadCL, Sorter) {
      "use strict";

      return Controller.extend("your.apps.namespace.YourController", {
        onInit: function () {

        },

        onReadProduct: async function () {
          // Initialize
          const odata = new ODataReadCL(this, "Products"); 
          const nameSorter = new Sorter("name");
          const priceSorter = new Sorter("price", true); // descending

          // add the sorters one by one
          odata.addSorter(nameSorter);
          odata.addSorter(priceSorter);      
        },

        onReadCategory: async function () {
          // Initialize
          const odata = new ODataReadCL(this, "Categories"); 
          const nameSorter = new Sorter("name");
          const priceSorter = new Sorter("price", true); // descending

          // set the sorters at once
          odata.setSorters([nameSorter, priceSorter]);         
        }
      });

    });

Additional Response Info

The read() and the readByKey() methods return the data of the target entity. However, you may require further information such as the status code and headers.

Once the read() or the readByKey() function has been completed, the getResponse() method can be utilized to obtain further details.

Returns Description
object
    $reported?: boolean | undefined
    body?: string | undefined The HTTP body
    headers?: object | undefined The HTTP response headers.
    statusCode?: string | number | undefined The status code of the HTTP request.
    statusText?: string | undefined The HTTP status text.
    _imported?: boolean | undefined
    data?: { results: EntityT[] } | undefined The data that was fetched

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Controller from "sap/ui/core/mvc/Controller";
import ODataReadCL from "ui5/antares/odata/v2/ODataReadCL"; // Import the class
import { IError } from "ui5/antares/types/common"; // Import the error type
import MessageBox from "sap/m/MessageBox";

/**
 * @namespace your.apps.namespace
 */
export default class YourController extends Controller {
  public onInit() {

  }

  public async onReadProduct() {
    // Initialize with a type
    const odata = new ODataReadCL<IProducts, IProductKeys>(this, "Products"); 

    try {
      // send the http request and get the result
      const result = await odata.readByKey({
        ID: "3ccb5dd2-cc12-483a-b569-a6ec844f8f0b"
      });

      MessageBox.information(result.ID + " was fetched.");

      // get the additional response info
      const response = odata.getResponse();

      if (response) {
        console.log("Status Code: " + response.statusCode);
      }
    } catch (error) {
      // catch the error
      MessageBox.error((error as IError).message || "Request failed");
    }
  }

}

interface IProducts {
  ID: string;
  name: string;
  description: string;
  brand: string;
  price: number;
  currency: string;
  quantityInStock: number;
  categoryID: string;
  supplierID: string;
}

interface IProductKeys {
  ID: string;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "ui5/antares/odata/v2/ODataReadCL", // Import the class
    "sap/m/MessageBox"
], 
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, ODataReadCL, MessageBox) {
      "use strict";

      return Controller.extend("your.apps.namespace.YourController", {
        onInit: function () {

        },

        onReadProduct: async function () {
          // Initialize
          const odata = new ODataReadCL(this, "Products"); 

          try {
            // send the http request and get the result
            const result = await odata.readByKey({
              ID: "3ccb5dd2-cc12-483a-b569-a6ec844f8f0b"
            });

            MessageBox.information(result.ID + " was fetched.");

            // get the additional response info
            const response = odata.getResponse();

            if (response) {
              console.log("Status Code: " + response.statusCode);
            }            
          } catch (error) {
            // catch the error
            MessageBox.error(error.message || "Request failed");
          }          
        }
      });

    });