Recently while working with Rest API in SharePoint Online to delete all items from a SharePoint list, I got the below error. I have added the code inside a script editor web part in a web part page in SharePoint Online. When I run the code it gave the below error:
{"readyState":4,"responseText":"{\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en-US\",\"value\":\"The expression \\\"Web/Lists/getByTitle('Announcements')/getItemById(item.ID)\\\" is not valid.\"}}}","responseJSON":{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"The expression \"Web/Lists/getByTitle('Announcements')/getItemById(item.ID)\" is not valid."}}},"status":400,"statusText":"Bad Request"}
The error looks like below:
Microsoft.SharePoint.Client.InvalidClientQueryException: The expression is not valid. Bad Request
In the Rest API code I was writing like below:
var items = data.d.results;
for(var item in items){
var url = "/_api/Web/Lists/getByTitle('MyList')/getItemById(item.ID)"
deleteItem(url);
}
Here into the URL variable, I was passing like item.ID which I have put inside double quote as a string.
Then I have modified like below and it started working.
$.each(data.d.results, function (key, value) {
var id=value.ID;
var url = "/_api/Web/Lists/getByTitle('Announcements')/getItemById("+id+")";
deleteItem(url);
});
You may like following SharePoint Rest API tutorials:
- Create and delete a file using Rest API in SharePoint Online/2013/2016
- Create, Update and Delete SharePoint List Item using Rest API
- Create a column in a list using Rest API in SharePoint Online/2013/2016
- Get list items using Rest API in SharePoint Online/2013/2016
- Create and Delete List using Rest API in SharePoint Online/2013/2016
- Access Rest API using Postman in SharePoint Online/2013/2016
- Display SharePoint list item using AngularJS REST API SharePoint online
- CRUD operations in SharePoint 2013 list item using rest api
- Insert and Retrieve items to SharePoint Online list using Rest API
This SharePoint Rest API tutorial explains, how to solve error Microsoft.SharePoint.Client.InvalidClientQueryException The expression is not valid. Bad Request.
Bhawana Rathore is a Microsoft MVP (3 times in Office Apps & Services) and a passionate SharePoint Consultant, having around 10 years of IT experience in the industry, as well as in .Net technologies. She likes to share her technical expertise in EnjoySharePoint.com and SPGuides.com
Exact copy of Bijay Kumar article
http://www.sharepointdotnet.com/2017/07/sharepoint-online-rest-api.html