01-25-2011 07:31 AM
Does anyone know how you would perform a search for something that contains an ampersand? I'm making these calls using the SData Client Library.
So for instance, something like this fails:
http://server/sdata/slx/dynamic/-/contacts(AccountName eq 'Mac & Cheese')?format=html
it throws a 404, or Bad Request error.
I’ve tried encoding the URL as & and %26, neither works.
The code I'm using to call looks something like this (VB.NET):
DestSDataSRR = New SDataSingleResourceRequest(DestSDataSvc)
DestSDataSRR.ResourceKind = "contacts"
DestSDataSRR.ResourceSelector = "(AccountName eq 'Mac & Cheese')"
Dim templateEntry As AtomEntry = DestSDataSRR.Read()
Is there any way to use the ampersand in a search, or could my code be changed to support it's use?
Solved! Go to Solution.
01-27-2011 08:19 AM
Unfortunately due to asp.net limitations the literal ampersand character cannot be used in predicate expressions, even if escaped.
It can however be used in the query string, meaning your example request can be made as follows:
/sdata/slx/dynamic/-/contacts?where=AccountName eq 'Mac %26 Cheese'
This is actually a more appropriate approach anyway since predicates must always resolve to a single resource.
Your original request would fail if the Mac & Cheese account had multiple contacts.
This is how you'd perform this request using the client library in C#:
var service = new SDataService("http://server/sdata/slx/dynamic/-/");
var request = new SDataResourceCollectionRequest(service)
{
ResourceKind = "contacts",
QueryValues = {{"where", "AccountName eq 'Mac & Cheese'"}}
};
var feed = request.Read();
Note that you don't need to escape special characters like literal ampersand since the client library will do that for you automatically.
02-02-2011 05:31 PM - edited 02-02-2011 05:33 PM
On a similar note, is there an issue when performing a search that contains a single quote (or apostrophe)? It seems a lookup like this: “Aron's Company” will fail, even if the apostrophe is encoded..
/sdata/slx/dynamic/-/accounts?where=AccountName eq 'Aron%27s Company'
any ideas?
Thanks
02-02-2011 08:34 PM
You have two options. You can either escape the literal apostrophe with a backslash or you can wrap your string in double quotes instead.
/sdata/slx/dynamic/-/accounts?where=AccountName eq 'Aron\'s Company'
/sdata/slx/dynamic/-/accounts?where=AccountName eq "Aron's Company"
02-02-2011 09:45 PM
02-02-2011 09:54 PM
I can only comment on the .net client library which doesn't escape literal quotes automatically.
This is intentional since expressions aren't parsed on the client side and there's no way to automatically differentiate between a string terminator and a literal quote.
I'd assume the JS library is no different for the same reasons.
02-03-2011 07:45 AM