Show TOC

How to Add Multiple FiltersLocate this document in the navigation structure

Context

Multiple filters can be added using the $top parameter to limit the results if the handling of $top is implemented on the server side.

Procedure

  1. Follow the steps detailed in the How to Implement the GetAll Function with Filter: Contact.

If you want to add multiple filter conditions, follow the steps below:

  1. Use the WCF EF 4.0 AddQueryOption method to pass the system query name ($filter in our case) and the filter condition:
    //var serviceresponse = serviceContext.BusinessPartnerCollection.Execute() as QueryOperationResponse<GWDEMO.BusinessPartner>;
    and add the following code snippet
    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>; 
    }
    
    
    When we run the code, only 5 business partners with country code=DE should be fetched in Outlook.

Combining Filter Conditions

Context

In this section, we will see how to combine filter conditions by use the logical operator OR.

Procedure

  1. From the Solution Explorer window expand the SAP Service Reference.
  2. Double click on the App.config file to open it.
  3. Add new configuration for country codes by providing two values:
    • DE
    • US
    <add key="CountryCode1" value="DE" />
    		<add key="CountryCode2" value="US" />
    
  4. In the Solution Explorer region navigate to Start of the navigation path Project folder Next navigation step Outlook Next navigation step Contacts Next navigation step Template (displays the name as given while creating the template) Next navigation step <Template>BusinessApplication.cs End of the navigation path.
    Note The name of the project folder and BusinessApplication may vary depending on the template name provided in the GWM wizard.
  5. Search for the GetAllData method in the BusinessApplication.cs.
  6. Write the following custom code to read the configurations from the App.config file
    string countryCode1 =  ConfigurationReaderHandler.Instance.GetConfigValue("CountryCode1");
    string countryCode2 =  ConfigurationReaderHandler.Instance.GetConfigValue("CountryCode2");
    

When a project is generated, the SAP.IW.GWM.Common dll will be created in the References folder under the Solution Explorer region. In this DLL, there is a class ConfigurationReaderHandler that contains the method GetConfigValue that is used to read the configuration maintained in App.config at runtime, hence in the above code, we are calling the method in the ConfigurationReaderHandler class.

  1. To fetch the filtered data from the SAP system, let us use the $filter feature provided by the OData standards.
  2. Use the WCF EF 4.0 AddQueryOption method to pass the system query name ($filter in our case) and the filter condition.
    //var serviceresponse = serviceContext.BusinessPartnerCollection.Execute() as QueryOperationResponse<GWDEMO.BusinessPartner>;
    and add the following code snippet
    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>;
    }
    
    
    Note The value of the filter condition may depend on the available filterable properties of the selected OData service. In case of the GWDEMO service, the CountryCode property of the BusinessPartnerCollection can be used to fetch the list of EPM accounts filtered on country code.

Verifying the Filters in Outlook

Prerequisites

  • Verify the filter condition on a system where Microsoft Office Outlook Professional edition is installed
  • Clear up any previous projects with the same name from the Outlook test system before testing this functionality

Procedure

  1. Run the project.
  2. In Outlook, double click on the EPM_Accounts menu.
  3. Click on GetAll to get the filtered list of records from SAP system.
    The Business Partners with Country code =’DE’ OR Country code =’US’ are fetched.