The security validation for this page is invalid and might be corrupted in SharePoint Rest API

If you are working in REST API in SharePoint, then you might face an error as: ‘The security validation for this page is invalid and might be corrupted. Please use your web browser’s back button to try your operation again.’ I will show you how to fix it.

We will learn why this error occurs and how to fix it: “the security validation for this page is invalid and might be corrupted,” as well as its solution.

the security validation for this page is invalid and might be corrupted

The security validation for this page is invalid and might be corrupted

I have a SharePoint list named Project as shown below. When I was creating an item in the list using the SharePoint REST API, I encountered the error: ‘The security validation for this page is invalid and might be corrupted.’ Please use your web browser’s back button to try your operation again.

The security validation for this page is invalid and might be corrupted SharePoint

To create a list item in the SharePoint Project List, I have used the following REST API code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $("#createProjectBtn").click(function(event) {
            event.preventDefault(); 
                
            var productName = $("#productName").val();
            var status = $("#status").val();

            createProjectListItem(productName, status);
        });
    });

    function createProjectListItem(productName, status) {
        var endpointUrl = "https://szg52.sharepoint.com/sites/Sales/_api/web/lists/getbytitle('Project')/items";

        var itemData = {
            "__metadata": { "type": "SP.Data.ProjectListItem" }, 
            "Title": productName, 
            "Status": status 
        };

        $.ajax({
            url: endpointUrl,
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(itemData),
            headers: {
                "Accept": "application/json;odata=verbose",
            },
            success: function(data) {
                console.log("List item created successfully:", data);
            },
            error: function(xhr) {
                console.error("Failed to create list item:", xhr);
                var errorMessage = xhr.status + ': ' + xhr.statusText;
                if (xhr.responseJSON && xhr.responseJSON.error) {
                    errorMessage += '\n' + xhr.responseJSON.error.message.value;
                }
                alert(errorMessage);
            }
        });
    }
</script>
<h2>Create Project List Item</h2>
<form id="createProjectForm">
    <label for="productName">Product Name:</label>
    <input type="text" id="productName" name="productName"><br><br>
    
    <label for="status">Status:</label>
    <select id="status" name="status">
        <option value="New">New</option>
        <option value="Working On">Working On</option>
        <option value="Completed">Completed</option>
    </select><br><br>

    <button type="button" id="createProjectBtn">Create Project</button>
</form>

When I ran the code, I encountered the error below: The security validation for this page is invalid and may be corrupted.

the security validation for this page is invalid and might be corrupted

This error typically occurs in SharePoint when the security validation token (X-RequestDigest) used in the AJAX request is either missing or invalid.

In the next section, we will see how to resolve this error: The security validation for this page is invalid and may be corrupted while working with the SharePoint REST API.

Solution:

If you mark this part in the above code, in the header of the Ajax Post request, we didn’t define the ‘X-RequestDigest.’ token. As the ‘X-RequestDigest’ validation token is missing from the code, it is throwing the error.

 $.ajax({
            url: endpointUrl,
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(itemData),
            headers: {
                "Accept": "application/json;odata=verbose",
      
            }

To fix this error, we need to add the validation token, i.e., “X-RequestDigest”: $(“#__REQUESTDIGEST”).val(). The Ajax post request will look like this:

$.ajax({
            url: endpointUrl,
            type: "POST",
            contentType: "application/json;odata=verbose",
            data: JSON.stringify(itemData),
            headers: {
                "Accept": "application/json;odata=verbose",
               "X-RequestDigest": $("#__REQUESTDIGEST").val()
            }

This is how we can fix the error: ‘ The security validation for this page is invalid and might be corrupted. Please use your web browser’s back button to try your operation again.’

Conclusion

In this tutorial, we saw how to fix the error: the security validation for this page is invalid and might be corrupted. please use your web browser’s back button to try your operation again.

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…