In this SharePoint tutorial, we will see how to fix the error: “Microsoft.SharePoint.Client.InvalidClientQueryException” ,”message”:
{“lang”:”en-US”,”value”:”The expression \”web/lists/getbytitle(‘Sales’)/
items(undefined)\” is not valid.” in SharePoint REST API.
We will also discuss why we got this error: “The expression \”web/lists/ getbytitle(‘Sales’)/ items(undefined)\” is not valid.” as well as its solution. Refer to the error image below.

Error: Microsoft.SharePoint.Client.InvalidClientQueryException
I have a SharePoint list called Sales. I wanted to delete a list item in this sales list using the SharePoint REST API.

To delete the SharePoint list items, I have used the below SharePoint rest api code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<h2>Delete All Items from SharePoint Sales List</h2>
<script>
$(document).ready(function() {
$("#deleteAllItemsBtn").click(function(event) {
event.preventDefault();
// Delete all items using X-RequestDigest token
deleteAllItems();
});
});
function deleteAllItems() {
var siteUrl = "https://szg52.sharepoint.com/sites/Sales";
var listName = "Sales";
var endpointUrl = siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items";
// Get the X-RequestDigest token value from the page
var requestDigest = $("#__REQUESTDIGEST").val();
$.ajax({
url: endpointUrl,
type: "GET",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": requestDigest
},
success: function(data) {
// Loop through each item and delete it
var items = data.d.results;
for (var i = 0; i < items.length; i++) {
var itemId = items.Id;
var deleteUrl = siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + itemId + ")";
deleteItem(deleteUrl, requestDigest);
}
},
error: function(xhr) {
console.error("Failed to fetch items:", xhr);
var errorMessage = JSON.stringify(xhr);
alert(errorMessage);
}
});
}
function deleteItem(url, requestDigest) {
$.ajax({
url: url,
type: "POST",
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": requestDigest,
"X-HTTP-Method": "DELETE",
"If-Match": "*"
},
success: function(data) {
console.log("Item deleted successfully:", url);
},
error: function(xhr) {
console.error("Failed to delete list item:", xhr);
var errorMessage = JSON.stringify(xhr);
alert(errorMessage);
}
});
}
</script>
<button id="deleteAllItemsBtn">Delete All Items</button>When I ran this above code, I got the error as: Microsoft.SharePoint.Client.InvalidClientQueryException”, “message”:
{“lang”:”en-US”,”value”:”The expression \”web/lists/getbytitle(‘Sales’)/
items(undefined)\” is not valid.”

This error indicates that a problem occurred with the REST API query being sent to the SharePoint server. Specifically, the query attempted to delete an item from a list using an undefined or invalid item ID.
To fix the error, follow the steps below:
In this part of the code, we are looping through the items to retrieve each item’s ID and store it in the variable itemId. However, instead of correctly accessing the ID using ‘var itemId = items[i].Id;’, we have used ‘var itemId = items.Id;’, which returns undefined.”
success: function(data) {
// Loop through each item and delete it
var items = data.d.results;
for (var i = 0; i < items.length; i++) {
var itemId = items.Id;
var deleteUrl = siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + itemId + ")";
deleteItem(deleteUrl, requestDigest);
}
},If we don’t have the item ID, we can’t get the item, so we can’t delete it from the SharePoint list. You can change the code like below:
success: function(data) {
// Loop through each item and delete it
var items = data.d.results;
for (var i = 0; i < items.length; i++) {
var itemId = items[i].Id;
var deleteUrl = siteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + itemId + ")";
deleteItem(deleteUrl, requestDigest);
}
},This is how we can fix the SharePoint rest api error: microsoft.sharepoint.client.invalidclientqueryexception
Conclusion
In this Sharepoint tutorial, we saw how to fix the error microsoft.sharepoint.client.invalidclientqueryexception while working with SharePoint REST API.
You may also like the following tutorials:
- An Entry Without a Type Name Was Found Rest API
- Get Current User using SharePoint Rest API
- Get SharePoint List Items using REST API
- The security validation for this page is invalid and might be corrupted

After working for more than 18 years in Microsoft technologies like SharePoint, Microsoft 365, and Power Platform (Power Apps, Power Automate, and Power BI), I thought will share my SharePoint expertise knowledge with the world. Our audiences are from the United States, Canada, the United Kingdom, Australia, New Zealand, etc. For my expertise knowledge and SharePoint tutorials, Microsoft has been awarded a Microsoft SharePoint MVP (12 times). I have also worked in companies like HP, TCS, KPIT, etc.
Exact copy of Bijay Kumar article
http://www.sharepointdotnet.com/2017/07/sharepoint-online-rest-api.html