Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top