I have a simple scenario, where I've a list with some custom content types. Among other fields, i have a managed metadata field.
I want to add a new item in the list through JSOM and this is where I fail. Updating the item works fine though. Here is the code snippet. I would really appreciate if i could get any help in identifying what i'm doing wrong here.
I have simplified the code here for the sake of simplicity. It works fine with i exclude the taxonomy fields but with taxonomy fields i get the exception "Field or Property ContentTypeId" does not exist"
function addItem(){
SP.SOD.executeOrDelayUntilScriptLoaded(function () {
'use strict';
var context = new SP.ClientContext(_spPageContextInfo.siteAbsoluteUrl);
var list = context.get_web().get_lists().getByTitle('<list title>');
var itemCreateInfo = new SP.ListItemCreationInformation();
var item = list.addItem(itemCreateInfo);
var field = list.get_fields().getByInternalNameOrTitle("MyTermField");
var taxField = context.castTo(field, SP.Taxonomy.TaxonomyField);
var termValue = new SP.Taxonomy.TaxonomyFieldValue();
termValue.set_label("English");
termValue.set_termGuid("term guid");
termValue.set_wssId(-1);
taxField.setFieldValueByValue(item, termValue);
item.set_item("Title", "some title");
item.update();
context.load(item);
context.executeQueryAsync(
function () {
console.log('Item created: ' + item.get_id());
//callback function
},
function (sender, args) {
console.log("exception in addItem");
});
}, 'SP.Taxonomy.js');
}