1

I am trying to save the data from a HTML form into an existing SharePoint list. I am getting error as:

Request failed. Invalid data has been used to update the list item. The field you are trying to update may be read only. undefined

This is my code:

function createListItem() {
    var clientContext = new SP.ClientContext(siteUrl);
    var listName = clientContext.get_web().get_lists().getByTitle('Nominations');
    var itemCreateInfo = new SP.ListItemCreationInformation(); 
    var txtEmpID = document.getElementById('emp_id').value; 
    var txtEmpName = document.getElementById('emp_name').value;
    var txtGrade = document.getElementById('grade').value;
    var txtEMailID = document.getElementById('email').value;

    this.oListItem = listName.addItem(itemCreateInfo);
    oListItem.set_item('EMPID', txtEmpID); 
    oListItem.set_item('EmployeeName', txtEmpName); 
    //oListItem.set_item('EmployeeName', SP.FieldUserValue.fromUser(txtEmpName) ); 
    oListItem.set_item('Grade', txtGrade); 
    oListItem.set_item('EMail', txtEMailID); 
    oListItem.update();
    clientContext.load(oListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySuccess), 
        Function.createDelegate(this, this.onQueryFail)
    );
}

function onQuerySuccess() {
    alert('Item added successfully');
}

function onQueryFail(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}

Through some R&D, I came to know that it is because of date value or people and group values. I am having date column in my list but it is not mandatory or not used. So, the problem might be from People or Group column. Here EmployeeName is People or Group column, so I modified code as:

this.oListItem = listName.addItem(itemCreateInfo);
oListItem.set_item('EMPID', txtEmpID); 
//oListItem.set_item('EmployeeName', SP.FieldUserValue.fromUser(txtEmpName) ); 
//oListItem.set_item('EmployeeName', txtEmpName);    
 var empname = clientContext.get_web().ensureUser(txtEmpName);
    clientContext.load(empname);

    if (empname) {
        oListItem.set_item('EmployeeName', empname);
    } 

But still it is displaying error, Please help.

  • refer this link to add/update people picker field - codeproject.com/Articles/824892/… – Rohit Waghela Jul 5 '17 at 11:04
  • Done the same but no result and the error is same. – user3189916 Jul 5 '17 at 11:41
  • please try to add add the ID of your EmployeeName field. e.g. oListItem.set_item('EmployeeName', IdOfEmpName); – Rohit Waghela Jul 5 '17 at 11:52
  • EMPID and EmployeeName are text field or people picker? – ThinkB4Code Jul 5 '17 at 14:41
  • EMPIDis text fieldd and EmployeeName is people picker – user3189916 Jul 6 '17 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.