Show TOC

How to Add Filtering to QueriesLocate this document in the navigation structure

You can add filters in the calls to the service using the $top parameter.

Prerequisites

Make sure that the OData service supports filtering for the specific property.

Context

Filters are important if you want to limit the results retrieved by the service based on the selected filter criteria, and to enhance the performance of your back-end system.

Use the method, WCF EF AddQueryOption, to pass the system query name ($filter in our case), and the filter condition.
Note $top option code is generated by default, and available if the handling of $top has been done on the server side.

Procedure


Inserting Filters

Context

To insert a filter:

Procedure

  1. From the Solution Explorer, open the class, Start of the navigation path Excel  Next navigation step  ExcelManager  Next navigation step  ExcelDataManager.cs  End of the navigation path
  2. Go the method, BindExcelTableWithRecords.
    Add comments to the line:
    //var serviceresponse = serviceContext.SalesOrderSet.AddQueryOption("$top",ExcelSettingManager.MaxRecords).Execute() as QueryOperationResponse<ZTEST_SEPM_SRV.SalesOrder>; 
  3. Insert a new line below the commented line for the code snippet below.
    Insert the following:
    var serviceresponse = serviceContext.SalesOrderSet.AddQueryOption("$top", ExcelSettingManager.MaxRecords).AddQueryOption("$filter", "SoId gt '500000223'").Execute() as QueryOperationResponse<ZTEST_SEPM_SRV.SalesOrder >;
  4. Rebuild and run the project for your Excel add-in application.
  5. From Microsoft Excel, click on the Fetch button, and check that the results are displayed according to the filter.

Combining Filter Conditions Using the OR Operator

Context

To combine filter conditions using the OR operator:

Procedure

  1. From the Solution Explorer, open the class, Start of the navigation path Excel  Next navigation step  ExcelManager  Next navigation step  ExcelDataManager.cs End of the navigation path.
  2. Go to the method, BindExcelTableWithRecords.
    Add a comment to the line:
     //var serviceresponse = serviceContext.SalesOrderSet.AddQueryOption("$top",ExcelSettingManager.MaxRecords).Execute() as QueryOperationResponse<ZTEST_SEPM_SRV.SalesOrder>;
  3. Insert a new line below the commented line for the code snippet below.
    Insert the following:
     var serviceresponse = serviceContext.SalesOrderSet.AddQueryOption("$top", ExcelSettingManager.MaxRecords).AddQueryOption("$filter", "SoId gt '500000239' or SoId eq '500000223'").Execute() as QueryOperationResponse<ZTEST_SEPM_SRV.SalesOrder>; 
  4. Rebuild and run the project for Excel add-in application.
  5. In Microsoft Excel, click on the Fetch button, and check that the results are displayed according to the filter.
    For more information on how to build filter expressions, see http://www.odata.org/documentation/uri-conventions/#SystemQueryOptionsInformation published on non-SAP site.

Inserting Filter Values Using the Configuration File

Context

Alternatively, you can add the filter values into the App.config file in order to enable application flexibility.

To add filter values into the configuration file and implement it in the generated code:

Procedure

  1. From the Solution Explorer, open the configuration file, Start of the navigation path <Your Excel Project Name>  Next navigation step  SAP Service Reference  Next navigation step  App.config End of the navigation path.
  2. Add the new configuration for a filter: <add key="filterSoID" value="'500000223'"/>.
  3. From the Solution Explorer, open the class, Start of the navigation path Excel  Next navigation step  ExcelManager  Next navigation step  ExcelDataManager.cs End of the navigation path
  4. Go the method, BindExcelTableWithRecords.
    Add the following code to read the configurations from the App.config file:
     string myfilter = ConfigurationReaderHandler.Instance.GetConfigValue("filterSoID"); 
  5. Add a comment to the line as shown below.
    //var serviceresponse = serviceContext.SalesOrderSet.AddQueryOption("$top",ExcelSettingManager.MaxRecords).Execute() as QueryOperationResponse<ZTEST_SEPM_SRV.SalesOrder>;
  6. Insert a new line below the commented line for the code snippet below.
    Insert the following
    var serviceresponse = serviceContext.SalesOrderSet.AddQueryOption("$top", ExcelSettingManager.MaxRecords).AddQueryOption("$filter", string.Format("SoId gt {0}", myfilter)).Execute() as QueryOperationResponse<ZTEST_SEPM_SRV.SalesOrder>;
  7. Rebuild and run the Excel add-in project.
  8. In Microsoft Excel, click on the Fetch button, and check that the results are displayed according to the filter configurations.