OData "contains" vs Dynamics 365 Web API "contains"

Daniel Schmid picture Daniel Schmid · Apr 20, 2018 · Viewed 7.1k times · Source

When querying Dynamics 365 via the Web API there are several operators to choose from to filter the queried data. One of those operators is contains which actually appears twice.

One is the OData contains function (you'll find it under the heading 'Standard query functions'):

https://msdn.microsoft.com/en-us/library/gg334767.aspx#Filter%20results

Example:

$filter=contains(name,'(sample)')

The other one is an implementation of the Dynamics 365 Web API itself:

https://msdn.microsoft.com/en-us/library/mt608053.aspx

I tried with this one, but only got a Generic SQL error:

$filter=Microsoft.Dynamics.CRM.Contains(PropertyName='name',PropertyValue='(sample)')

What's the difference? And maybe someone can even tell me how to call the Web API version of contains correctly?

Answer

Arun Vinoth picture Arun Vinoth · Apr 20, 2018

Latest documentation confirms $filter=contains(name,'(sample)') is the only working syntax with web api. As Jatin says, OData filter is converted into Query Expression behind the scenes, some articles (this & this) conveys possible solution is using Like operator directly in C# Query Expression.

We don't have Like operator equivalent functions in web api.

My attempts & observations:

Microsoft.Dynamics.CRM.Contains - Generic SQL error.

$filter=like(name,'%test%') - An unknown function with name 'like' was found. This may also be a function import or a key lookup on a navigation property, which is not allowed.

$filter=contains(name, 'test') - working