1

I am trying to create a column in a list in SharePoint 2013 but I receive the following error. Anybody know what it means.

Request failed. 'FirstName' is an unexpected token. The expected token is '"' or '''. Line 1, position 32. undefined

    function addFields(NewField) {

    var clientContext = new SP.ClientContext.get_current();
    var oWebsite = clientContext.get_web();
    //Getting reference to the list
    oList = clientContext.get_web().get_lists().getByTitle('Departmental Square Feet');
    console.log("Got the list");
    // Get filed collection
    var fldCollection = oList.get_fields();

    var f1 = clientContext.castTo(fldCollection.addFieldAsXml('<Field Type="Text" DisplayName='+NewField+' Name='+NewField+' />', true, SP.AddFieldOptions.addToDefaultContentType), SP.FieldText);
    console.log("About to set the field");
    f1.set_title(NewField);
    f1.set_description("sample desc");
    console.log("About to update");
    f1.update();       


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

function onQuerySucceeded() {
    alert("List Field Updated");
}

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

addFields("FirstName");

2

Problem Solved, I have tried running your code in my test environment and found the issue. It was arising due to improper formation of String when defining your field as XML. The Error says you should have provided the field's DisplayName and Name in double quotes. viz:

'<Field Type="Text" DisplayName="+NewField+" Name="+NewField+" />'

Below is the Complete test code and screen shot:

<input type="button" value="No SPS" onClick="addFields();" style="color:black; background-color:grey" />

<script type="text/javascript">
function addFields() {
    var NewField = "FirstName";
    var clientContext = new SP.ClientContext.get_current();
    var oWebsite = clientContext.get_web();
    //Getting reference to the list
    oList = clientContext.get_web().get_lists().getByTitle('MyList');
    console.log("Got the list");
    // Get filed collection
    var fldCollection = oList.get_fields();

    var f1 = clientContext.castTo(fldCollection.addFieldAsXml('<Field Type="Text" DisplayName="+NewField+" Name="+NewField+" />', true, SP.AddFieldOptions.addToDefaultContentType), SP.FieldText);
    console.log("About to set the field");
    f1.set_title(NewField);
    f1.set_description("sample desc");
    console.log("About to update");
    f1.update();       


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

function onQuerySucceeded() {
    alert("List Field Updated");
}

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

</script>

enter image description here

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.