0

My objective is to add a new item in a SharePoint list (Form) using REST API.

My current code is:

function AddData() {
    var listName = "Form";
    CreateListItemWithDetails(listName, _spPageContextInfo.webAbsoluteUrl, function () {
        console.log("New Item has been created successfully.");
 }, function () {
     console.log("Ooops, an error occured. Please try again.");
 });
}

function CreateListItemWithDetails(listName, webUrl, success, failure) {
    var itemType = "SP.Data.FormListItem";
    var item = {
        '__metadata': { "type": itemType },
        'Title':document.getElementById('explorationescalation').value,
        'Capex Escalation':document.getElementById('capexescalation').value,
        'Opex Escalation':document.getElementById('opexescalation').value,
        'Abandonment Escalation':document.getElementById('abandonmentescalation').value,
        'Oil Price ($/bbl)':document.getElementById('oilprice').value,
        'Gas Price($/mcf)':document.getElementById('gasprice').value,
        'Opex ($/bbl)':document.getElementById('opex').value,
        'LRP':document.getElementById('lrp').value,
        'HRP':document.getElementById('hrp').value,
        'Block Type':document.getElementById('blocktype').value,
        'Production Sensitivity':document.getElementById('productionsensitivity').value,
        'Capex Sensitivity':document.getElementById('capexsensitivity').value,
        'Opex Sensitivity':document.getElementById('opexsensitivity').value,
        'WACC':document.getElementById('wacc').value,
        'Abandonment Rate':document.getElementById('abandonmentrate').value,
        'IT Rate':document.getElementById('itrate').value,
        'MAT Rate':document.getElementById('matrate').value,
        'FX Rate (INR/$)':document.getElementById('fxrate').value,
        'NCCD (INR/MT)':document.getElementById('nccd').value,
        'BBL to MT':document.getElementById('bbltomt').value,
        'Discount Date':document.getElementById('discountdate').value,
        'Regime':document.getElementById('regime').value,
        'Block Name':document.getElementById('blockname').value,
        'Project Name':document.getElementById('projectname').value,
        'Project Description':document.getElementById('projectdescription').value
        
    };

    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            success(data);
        },
        error: function (data) {
            failure(data);
        }
    });
}

However when I tried to create item using the button. I get error message in network tab while inspecting element:

"The property 'Capex Escalation' does not exist on type 'SP.Data.FormListItem'. Make sure to only use property names that are defined by the type."

with Status Code 400 of the post request. All of the above columns are already available in the list and I've double check the spellings, white spaces and data type of columns which is also correct.

Further if I comment out all of the item and values except "Title" then I can get the list item created successfully with only column Title updated.

I've also checked the request payload and it is correct.

What could be the reason that it is not able to update all fields but one? Please help!

1 Answer 1

1

You have to use the internal name of your columns in payload (var item) instead of display names.

Check below URL to find out the internal name of SharePoint column:

Find the internal name of SharePoint column

Documentation: Working with lists and list items with REST

3
  • Also, what are the data types of all columns? You have to pass column values in some specific format for few data types like date, yes/no, lookup, etc. Nov 11, 2021 at 8:58
  • 1
    Thanks it worked. For the data types, I'm only using single line of text. Nov 11, 2021 at 9:07
  • 1
    Great, glad it worked for you. Any string data/value should work for you then (for single line of text columns). Nov 11, 2021 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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