Ado
Ado

Reputation: 65

SharePoint REST API URL

Hi I am using JavaScript for accessing SharePoint and I can successfully get the List content as long as the ListName is a single word, but when the ListName has two words by JavaSript fails.

For example:

url: siteUrl + "/_api/web/lists/getbytitle('Product')/items" = OK
url: siteUrl + "/_api/web/lists/getbytitle('Product Name')/items" = NO OK

Thank you for any help.

EDIT Click to see the error I get

Upvotes: 1

Views: 610

Answers (2)

Lost in Alabama
Lost in Alabama

Reputation: 1653

Try it without the space in the list name, like this:

url: siteUrl + "/_api/web/lists/getbytitle('ProductName')/items"

Upvotes: 1

Karol Klepacki
Karol Klepacki

Reputation: 2098

You can use encodeURIComponent() function. It'll convert your string to browser-acceptable format.

encodeURIComponent("getbytitle('Product Name')") will be getbytitle('Product%20Name')"

So in your case if title can be changed it should be:

var title = "Can be few words";
var siteUrl = "blabla";
var url = siteUrl + "/_api/web/lists/getbytitle('" + encodeURIComponent(title) + "')/items";

You can read more here

Upvotes: 0

Related Questions