Show TOC

How to Work with FiltersLocate this document in the navigation structure

This topic gives an overview of filters.

System query options are query string parameters that you can specify to control the amount, and the order of the data that an OData service returns for the resource identified by the URI. The names of all system query options are prefixed with a “$” character.

A URI with a $filter system query option identifies a subset of the entries from the collection of entries. The subset is determined by selecting only the entries that satisfy the predicate expression specified by the query option. The expression language that is used in $filter operators supports references to properties and literals. The literal values can be strings enclosed in single quotes, numbers, and boolean values (true or false).

Adding a Filter

The $filter option can be used to get only entries that satisfy the condition. For example, to fetch the records from the contact collection whose countrycode is DE, use the code snippet provided below:

From the Solution Explorer window, open your Start of the navigation path [Project folder]  Next navigation step SAP Service Reference Next navigation step  App.config End of the navigation path in the code window.

Open the App.config file and use the example code below in the the section, appSettings:

<appSettings> 
        <add key="CountryCode" value="DE"/>
 </appSettings>

Below is an example code snippet to read the configuration value:

string countryCode = ConfigurationReaderHandler.Instance.GetConfigValue("CountryCode")

For an example,.in the Contacts01BusinessApllication.cs class under Start of the navigation path Outlook Next navigation step Contact Next navigation step Contacts01 End of the navigation path, use the method, GetAllData to read the configuration maintained in the App.config at runtime.

Find the line below in the code:

//var serviceresponse = serviceContext.BusinessPartnerCollection.Execute() as QueryOperationResponse<GWDEMO.BusinessPartner>
Replace with the code sample below to read the filtered list from the serviceCode snippet to fetch records:
QueryOperationResponse<GWDEMO.BusinessPartner> serviceresponse = null;
                if (string.IsNullOrEmpty(countryCode))
                {
                    serviceresponse = serviceContext.BusinessPartnerCollection.Execute() as QueryOperationResponse<GWDEMO.BusinessPartner>; 
                }
                else
                {
                    serviceresponse = serviceContext.BusinessPartnerCollection.AddQueryOption("$filter", string.Format("CountryCode eq '{0}'", countryCode)).Execute() as QueryOperationResponse<GWDEMO.BusinessPartner>;
                }
Adding More than One Query Parameter

You can use more than one system query option, including logical operators, filter,and top.

1. Using the ‘$top’ parameter

You can use the $top parameter on the server side, to limit the results returned.

From the Solution Explorer window, open your Start of the navigation path [Project folder]  Next navigation step SAP Service Reference Next navigation step  App.config End of the navigation path in the code window.

In the ConfigurationReaderHandler class of SAP.IW.GWM.Common dll, use the method, GetConfigValue to read the configuration maintained in the App.config at runtime.

Below is a sample code snippet to read the filtered list from the service:

//var serviceresponse = serviceContext.BusinessPartnerCollection.Execute() as QueryOperationResponse<GWDEMO.BusinessPartner>;

Use the following sample code snippet to get only five records from the contact collection whose country code is DE.

Code to read the countrycode value from the App.config file:

string countryCode =  ConfigurationReaderHandler.Instance.GetConfigValue("CountryCode");

QueryOperationResponse<GWDEMO.BusinessPartner> serviceresponse = null;
                if (string.IsNullOrEmpty(countryCode))
                {
                    serviceresponse = serviceContext.BusinessPartnerCollection.Execute() as QueryOperationResponse<GWDEMO.BusinessPartner>; 
                }
                else
                {
                    serviceresponse = serviceContext.BusinessPartnerCollection.AddQueryOption("$filter", string.Format("CountryCode eq '{0}'&$top=5", countryCode)).Execute() as QueryOperationResponse<GWDEMO.BusinessPartner>; 
}

Based on the code sample above, only five business partners with country code=’DE’ is fetched in Outlook.

2. Using Logical Operators to Combine Filter Conditions

This section represents an extended variant of the above example. Here you will see how logical operations can be incorporated in filter queries. For example, the logical operator OR can be used as follows:

2.1 Create a Configuration Value in the App.config File

From the Solution Explorer window, open your Start of the navigation path [Project folder]  Next navigation step SAP Service Reference Next navigation step  App.config End of the navigation path in the code window.

Application configuration files contain settings specific to an application. This file contains configuration and settings that the application can read at runtime.

Start by adding a new configuration for country codes in the section appSettings. Provide two values for country code ‘DE’ and ‘US’ as shown below:

<appSettings> 
  <add key="CountryCode1" value="DE" />
  <add key="CountryCode2" value="US" />
</appSettings> 

2.2 Read Configuration Value Using GWM API

In the ConfigurationReaderHandler class of SAP.IW.GWM.Common dll, use the method, GetConfigValue to read the configuration maintained in the App.config at runtime.

string countryCode1 =  ConfigurationReaderHandler.Instance.GetConfigValue("CountryCode1");
string countryCode2 =  ConfigurationReaderHandler.Instance.GetConfigValue("CountryCode2");
Code snippet to read the filtered list from the service:
//var serviceresponse = serviceContext.BusinessPartnerCollection.Execute() as QueryOperationResponse<GWDEMO.BusinessPartner>; 

You can use the following code example:

QueryOperationResponse<GWDEMO.BusinessPartner> serviceresponse = null;
if (string.IsNullOrEmpty(countryCode1) && string.IsNullOrEmpty(countryCode2))
{
serviceresponse = serviceContext.BusinessPartnerCollection.Execute() as QueryOperationResponse<GWDEMO.BusinessPartner>; 
 }
 else
 {
   serviceresponse = serviceContext.BusinessPartnerCollection.AddQueryOption("$filter", string.Format("CountryCode eq '{0}' or CountryCode eq '{1}' ", countryCode1,countryCode2)).Execute() as QueryOperationResponse<GWDEMO.BusinessPartner>;
}

Test using Outlook. You should be able to fetch business partners with the country code =’DE’ or country code =’US’.