Showing results for 
Search instead for 
Do you mean 
Community Home Request Access Read Blogs Share Your Ideas Search Community View My Settings
Reply
Highlighted
New Member
Posts: 20
Registered: ‎08-23-2010
Accepted Solution

Using ampersand in SData query

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?

Highlighted
Employee
Posts: 27
Registered: ‎08-06-2009

Re: Using ampersand in SData query

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.

Highlighted
New Member
Posts: 20
Registered: ‎08-23-2010

Using apostrophe or single quote in SData query

[ Edited ]

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

Highlighted
Employee
Posts: 27
Registered: ‎08-06-2009

Re: Using apostrophe or single quote in SData query

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"

Highlighted
Gold Super Contributor
Posts: 3,087
Registered: ‎03-19-2009

Re: Using apostrophe or single quote in SData query

What does the two client libs do (the js one and the .NET one)?

--
RJ Ledger - rjledger@rjlSystems.net +1 603.369.3047 x101

".. Innovators in Mobility - Experts in Workflow Automation..."
http://www.rjlSystems.net - blog: www.rjlSystems.net/blog.html
Highlighted
Employee
Posts: 27
Registered: ‎08-06-2009

Re: Using apostrophe or single quote in SData query

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.

Highlighted
New Member
Posts: 20
Registered: ‎08-23-2010

Re: Using apostrophe or single quote in SData query

This is working. Thanks again!