microsoft.sharepoint.client.invalidclientqueryexception in SharePoint REST API

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.

microsoft.sharepoint.client.invalidclientqueryexception

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.

invalidclientqueryexception

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.”

microsoft.sharepoint.client.invalidclientqueryexception

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:

>

Build a High-Performance Project Management Site in SharePoint Online

User registration Power Apps canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial FREE PDF Download

FREE Power Platform Tutorial PDF

Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…