16

Trying to use the SP2013 REST API to ping a library with the title "Prep & Admin Docs" and I can't use getbyID as this snippet will be used in multiple sites where the GUID of the list will vary.

I've tried multiple methods of encoding but nothing seems to be working.

/_api/lists/getbytitle('Prep & Admin Docs')
/_api/lists/getbytitle('Prep & Admin Docs')
/_api/lists/getbytitle('Prep %26 Admin Docs')

When I check /_api/lists I can see the list there and the title shows up exactly as I have it. But I can't seem to use that string in the query. It's most likely something simple that I'm missing.

Is there a way to query by EntityTypeName?

3
  • Any help from my answer? Commented Dec 23, 2015 at 4:02
  • 2
    It did not help me... M facing the same issue Commented Aug 18, 2016 at 9:28
  • Please consider marking the answers that provide a solution as "solution". There was a missing quote, that maybe the reason why it didnt work for you.
    – Alberto S.
    Commented Feb 25, 2020 at 17:58

5 Answers 5

8

EntityTypeName is not needed. List title is the only need. If your list title contains any special character, then you can use encodeURI().

For example

var listNameAsencodeURI = encodeURI('Prep & Admin Docs');

It will output something like

"Prep%20&%20Admin%20Docs"

Now use above output in API end-point

/_api/lists/getbytitle('Prep%20&%20Admin%20Docs')

OR

"/_api/lists/getbytitle('"+encodeURI('Prep & Admin Docs')+"')"

Note: All special characters except , / ? : @ & = + $ # are needed to be encoded if we use them in list name.

3
  • 3
    I have tried your suggestions for the URL but still m getting error " A potentially dangerous Request.Path value was detected from the client (&). ". Can u please help. Commented Aug 18, 2016 at 9:27
  • 1
    I'm too getting this issue. Please help me to resolve this error. Commented Aug 26, 2016 at 5:37
  • What's your title? Commented Aug 26, 2016 at 6:18
5

Reading some of the replies, I'd like to supplement with the following article:

http://xkr.us/articles/javascript/encode-compare/

I am on Office 365 and I made a library where I tried your approaches. Note that there might be differences between the latest version of SP and the one running on premise.

Performing some tests I found that both & and %26 work fine for me, but not if I try using &.

The other thing worth noting is that the GetByTitle method takes a string, as indicated by the ' characters. This means that when using the various escaping methods in javascript, the ' will never be escaped, so you have to do that yourself.

For example if you have a list named Bob's Bacon & Eggs you'd use /_api/Lists/GetByTitle('Bob''s Bacon & Eggs') to access the data.

If you wish to have a variable for the name of the list, you could escape it using the following approach: "/_api/Lists/GetByTitle('" + encodeURIComponent(listName).replace(/\'/g, "''") + "')"

5

Try querying the endpoint as: /_api/web/lists/getbytitle('Pre%20%26%20Admin%20Docs')

More details on this good article here

enter image description here

In the case of working out the "&", just replace it with "%26", as the picture shows.

hope this will help you!

0

There's a big difference in SPOnline and on-premises SP. I haven't been able to find a way to make this work with any sort of encoding with SP2013 on-prem, but it works with no endcoding at all to the SPOnline REST API.

/_api/lists/getbytitle('Prep & Admin Docs')

It's another one of those maddening ways that SP is consistently inconsistent.

-1

This work for me. See the screenshot. Strange !!!

/_api/web/lists/getbytitle(Pre%20&%20Admin%20Docs')

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.