Follow in Twitter & Facebook


Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools


Sunday, July 23, 2017

SharePoint online Rest API Microsoft.SharePoint.Client.InvalidClientQueryException The expression is not valid. Bad Request

Recently while working with Rest API in SharePoint online to delete all items from a 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"}


Solution:



In the Rest API code I was writting 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);
    });    

Hope this will be helpful.




0 on: "SharePoint online Rest API Microsoft.SharePoint.Client.InvalidClientQueryException The expression is not valid. Bad Request"