Search code examples
ecmascript-6sharepoint-2010

Issue with ECMA script for SharePoint 2010 create item - The security validation for this page is invalid and might be corrupted


I am working SharePoint 2010 on premise team site. Writing a simple Client side code to create item in a list with ECMA script when I get the below 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."

Below is my simple code:

function AddItem() 
{

 var clientContext =  new SP.ClientContext.get_current();

    var web = clientContext.get_web();

    var vListDetails = web.get_lists().getByTitle("SampleList");//

    var itemCreateInfo = new SP.ListItemCreationInformation();

    this.oListItem = vListDetails.addItem(itemCreateInfo);

    oListItem.set_item('Title' , 'ABC');//Training Name  

    oListItem.update();

    clientContext.load(oListItem);        

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {

    alert('Item created: ' + oListItem.get_id());
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', AddItem);

I have done this several time in SharePoint 2013 it always worked.

Thanks in advance


Solution

  • The issue is resolved:

    Initially I had reference only to SP.JS. The issue was resolved after adding the below references in sequence:

    <script src="/_layouts/1033/init.js" type="text/javascript"></script>
    <script src="/_layouts/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="/_layouts/sp.core.js" type="text/javascript"></script>
    <script src="/_layouts/sp.runtime.js" type="text/javascript"></script>
    <script src="/_layouts/sp.js" type="text/javascript"></script>