Case insensitive filtering of OData service in SAPUI5

Filtering JSON model is case insensitive, but for OData models this is not the case. OData service filtering is case sensitive by design. In order to achieve case insensitive searching or filtering we can use the good old way of lowering the filter string and the string we search in.

var oFilter = new Filter(
     "tolower(LastName)",
     FilterOperator.Contains,
     "'" + filterString.toLowerCase().replace("'","''") + "'"
     );

When we use a search string like "O'donnell" this translates into:

$filter=substringof(%27o%27%27donnell%27, tolower(FirstName))

As per OData specification the text we search in can be lowered using tolower():
tolower(FirstName) 


The searched text has to be lowered as well using the javascript function toLowerCase(). In addition it is surrounded by quotes to ensure it is a string when added to the odata filter. And finally to avoid problems if the search string contains quotes we have to escape them by replacing with two quotes.

Happy coding!