set lookup field & managed meta data field using jsom in sharepoint

1k views Asked by At

Setting lookup field & managed meta data field value using jsom. Through jsom I will need to set the value into the list .

Setting the lookup and managed metadata columns through code

1

There are 1 answers

0
Gautam Sheth On BEST ANSWER

Try and modify the below sample code:

var clientContext = new SP.ClientContext(_spPageContextInfo.webAbsoluteUrl);  
var list = clientContext.get_web().get_lists().getByTitle('TestList');  
var itemCreateInfo = new SP.ListItemCreationInformation();  
var listItem = list.addItem(itemCreateInfo);

var singleLookupColumn = new SP.FieldLookupValue();  
singleLookupColumn.set_lookupId(2);  
listItem.set_item('CustomLookup', singleLookupColumn); 

var field = list.get_fields().getByInternalNameOrTitle("TestTaxonomy");  
var taxField = clientContext.castTo(field, SP.Taxonomy.TaxonomyField);  
var taxonomyCol = new SP.Taxonomy.TaxonomyFieldValue();  
taxonomyCol.set_label("Test");  
taxonomyCol.set_termGuid("23d03b66-5be6-512b-9fe3-ff13b9b4757c");  
taxonomyCol.set_wssId(-1);  
taxField.setFieldValueByValue(listItem, taxonomyCol);


listItem.update();  
clientContext.load(listItem);  
clientContext.executeQueryAsync(function(){
    console.log("success");
},function(){
    console.log("error");
});