0

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');
}
2

You can easily create a list item with managed meta data field as :

function createaListItem() {
  var clientContext = new SP.ClientContext.get_current();
  var oList = clientContext.get_web().get_lists().getByTitle('Listname');    
  var itemCreateInfo = new SP.ListItemCreationInformation();
  this.oListItem = oList.addItem(itemCreateInfo);    
  oListItem.set_item('Title', "Title");  
  oListItem.set_item('UserInfo', "643ffb26-e631-4fb6-bab9-b34849eac0b2");   
  oListItem.update();
  clientContext.load(oListItem);       
  clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), 
         Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded() {
  alert('Item created: ' + oListItem.get_id());
}

function onQueryFailed(sender, args) {
  alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
1

This might seem a bit straight forward, but have you tried simply calling item.set_item(<field InternalName>, <term guid>)?

I did a simple test where I created a site column which pointed to a term store, then a content type which used this site column. Then I added this content type to a list. Finally I used this code to create an item without a problem:

var ctx = SP.ClientContext.get_current();
var item = ctx.get_web().get_lists().getByTitle('Test').addItem();
item.set_item('MyTerm', '5caa9ac6-148c-490f-8bed-3948960c0946');
item.update();
ctx.executeQueryAsync();

Also including item.set_item('ContentTypeId', <content type id>) proved to work just fine.

  • Thanks eirikb, I tried it but doesn't work for me. same exception. – Sanjay Bhagia Apr 10 '14 at 21:18
  • Just for the sake of it, could you try these steps: 1) Check ULS for additional exceptions. 2) Try using CSOM, remember fiddler if it works and 3) Try with REST (if 2013, or Lists.asmx(SPServices)) – eirikb Apr 10 '14 at 21:34
  • Nothing in ULS, REST is also limited when it comes to managed metadata (i tried it but had issues if i have multi value taxonomy fields). Haven't tried CSOM though.. can try it out. Thanks – Sanjay Bhagia Apr 11 '14 at 9:37
  • Sorry @SanjayBhagia I am clueless. CSOM should not work any better if the others fail, especially since JSOM fails. I'm baffled that it works for you on update, but not insert. I can't see the link between that, and content type and taxonomy. Is it 2013 or 2010? (doubt taht matters). Could you try on a clean list/spweb/spsite/web app/server/something? – eirikb Apr 11 '14 at 13:18
  • there was nothing wrong with the code. Apparently there was problem with the content type id. i was passing the parent content type id, it should be the complete (list) content type. So it worked well. Though i'm still having problem with two fields in two content types. it complains the same, field or property doesn't exist. the same fields work just fine in other content types though. Looking for the issue :) – Sanjay Bhagia Apr 17 '14 at 12:55
1

The line:

taxField.setFieldValueByValue(item, termValue);

is intended for setting Taxonomy field value but there is no Taxonomy field (taxField) initialization in the specified code sample.

Probably it should be like this:

var field = list.get_fields().getByInternalNameOrTitle('<tax field name>'); //get tax field
var taxField = context.castTo(field, SP.Taxonomy.TaxonomyField);
taxField.setFieldValueByValue(item, termValue); //set tax field value
  • Thanks Vadmim! my apologies, i have that piece of code. in the process of extracting the code for this post, i accidentally removed setting the field also. have update the post now. – Sanjay Bhagia Apr 10 '14 at 21:07
  • Sanjay, no worries :) – Vadim Gremyachev Apr 10 '14 at 21:09

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.