0

Please note that I'm doing this for the first time.

I want to add an element to a list via JavaScript. Therefore, I tried this:

function fillStandard() {
    console.log("Standard");
    var posInvest = "Invest";

    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('Position');
    var item = new SP.ListItemCreationInformation();

    var oListItem = oList.addItem(item);
    oListItem.set_item('PosType', posInvest);

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

function onQuerySucceeded(sender, args) {
    console.log("successfully executed");
}

function onQueryFailed(sender, args) {
    console.log("failed");
    alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}  

The problem is that my onQueryFailed tells me:

request failed The field or property PosType does not exist
undefined 

But when I look at my list Position it does have a column called PosType:

posType

What am I doing wrong?

2

While using JSOM we need to use the internal name (and not the display name of column which you see in the list) of the SharePoint column in code.

By looking at an error you are getting I suspect that you are using the wrong name of your PosType field.

Check the internal name of your column by using the method given in below article and then use that name in your code instead of PosType.

Find the internal name of SharePoint column.

Also you need to update() the list item before loading it in the clientContext.

var oListItem = oList.addItem(item);
oListItem.set_item('PosType', posInvest);
oListItem.update();  //Call this method before load() 

clientContext.load(oListItem);

Reference How to: Create, Update, and Delete List Items Using JavaScript.

| improve this answer | |
  • Thanks for your reply. I looked it up, but the internal name of that column is also "PosType"... – JavaTeachMe2018 Jan 7 at 17:16
  • Try using the update() method as given in updated answer above. – Ganesh Sanap Jan 7 at 17:33

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.