7

How is the & symbol handled in the following query in oData?

/vendordataservice.svc/vDataMapper_SourceMapVendor?&$filter=startswith(ParentName,'AT&T')&$top=7&$skip=0

I'm using EF3.5 and SQL2008. When I send that to my oData service I get no data back.

makerofthings7
  • 60,103
  • 53
  • 215
  • 448
  • Are you producing this yourself? Shouldn't you be encoding that? – Craig Stuntz Nov 19 '10 at 21:57
  • @Craig - That GET string is created by my Telerik control when a filter is applied. Should they be encoding it? I'm not sure, but perhaps. What other chars should be encoded? Is it supposed to be HTML encoded? – makerofthings7 Nov 20 '10 at 03:41
  • The `&` character is reserved in a URI, per the RFC. Yes, it should be encoded. – Craig Stuntz Nov 20 '10 at 03:49
  • 1
    Answer helped with my problem as well. http://stackoverflow.com/questions/33138561/dynamics-crm-odata-query-that-checks-if-the-file-attachment-file-name-contains Thanks – Cyrus Oct 15 '15 at 03:34

3 Answers3

10

Do not use the “JavaScript String replace() Method”. It will replace the first occurrence of the special characters. If you have 2 occurance of the same special characters in the filtering parameter, it will fail. So use the regular expression to replace the characters.

function replaceSpecialCharacters(attribute) {
  // replace the single quotes
     attribute = attribute.replace(/'/g, "''");

     attribute = attribute.replace(/%/g, "%25");
     attribute = attribute.replace(/\+/g, "%2B");
     attribute = attribute.replace(/\//g, "%2F");
     attribute = attribute.replace(/\?/g, "%3F");

     attribute = attribute.replace(/#/g, "%23");
     attribute = attribute.replace(/&/g, "%26");
     return attribute;
}

Also pay attention, since the replacements also contains % then % itself should be replaced at the beginning

Reza
  • 18,865
  • 13
  • 88
  • 163
6

Here is a list of characters that should be encoded prior to sending to SQL server over HTTP:

http://msdn.microsoft.com/en-us/library/aa226544(SQL.80).aspx

Yes, the '&' symbol is one of them.

Rami A.
  • 10,302
  • 4
  • 44
  • 87
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
0

if the filter parameter is seen as a single word you can append ASCII value of single quote before and after the param like this%27AT&T%27

elemes
  • 11
  • 2