4

Hoping someone might be able to shed some light on a problem I'm having adding new items to a SharePoint Online list, using JSOM?

I have modified some code I found here on stack exchange, the code works fine but, ONLY when I hard code the values I want in to the code. As soon as I try and use a variable that contains the values from the form, it fails with an unexpected error.

Can anyone offer any advice on why this is happening, please?

The lines in questions are below, like this they work:

lmi.set_termLabel('IT');  
oListItem.set_item('Title', 'Test');

If I change them to the below they stop working:

lmi.set_termLabel(txtTerm); 
oListItem.set_item('Title', txtTitle);

Full code

function ExecuteTaxonomyFunctions(txtTitle,txtTerm)  
     {  
          var siteUrl = '/sites/rob';


          var context = new SP.ClientContext(siteUrl);   
          var list = context.get_web().get_lists().getByTitle("jQuery");  

          var field = list.get_fields().getByInternalNameOrTitle("Dept");  
          var txField = context.castTo(field, SP.Taxonomy.TaxonomyField);  
          context.load(field);  
          context.load(txField);  
          //Get the response from server to get the termsetId  
          context.executeQueryAsync(function(){  
            //Get the term set ID  
               var termSetId = txField.get_termSetId().toString();  
               var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);  
               var termStore = taxSession.getDefaultSiteCollectionTermStore();  
               var termSet = termStore.getTermSet(termSetId);  

               // Get the matching terms based on names  
               var lmi = new SP.Taxonomy.LabelMatchInformation(context);  
               lmi.set_lcid(1033);  
               lmi.set_termLabel('IT'); //Variable used here will stop item from being created
               lmi.set_trimUnavailable(true);  


               var termMatches = termSet.getTerms(lmi);  
               context.load(termMatches);  
               //Get the matching term and then add to list item  
               context.executeQueryAsync(function () {  
               if (termMatches && termMatches.get_count() > 0)  
               {  
                   // Get the first matching term. As I know it has only one  
                   // You need to iterate and get the correct term if you have same name terms  
                   var term = termMatches.get_item(0);  

                  // Add Item to list   
                   var itemCreateInfo = new SP.ListItemCreationInformation();  
                   var oListItem = list.addItem(itemCreateInfo);  
                   oListItem.set_item('Title', 'Test');  //Variable used here will stop item from being created
                   var termFieldValue = new SP.Taxonomy.TaxonomyFieldValue();  
                   termFieldValue.set_label(term.get_name());  
                   termFieldValue.set_termGuid(term.get_id().toString());  
                   termFieldValue.set_wssId(-1);  
                   txField.setFieldValueByValue(oListItem, termFieldValue);  
                   oListItem.update();       

                   context.executeQueryAsync(function () {  
                     alert('Item Added');  
                    }, function (sender, args) {  
                      alert(args.get_message() + '\n' + args.get_stackTrace());  
                    });  

               }  
             }, function (sender, args) {  
                alert(args.get_message() + '\n' + args.get_stackTrace());  
             });  
          },function(sender,args){  
               alert(args.get_message());  
          });  
     }
  • I'd show the codeblock in that ISNT working, not that is. Can you show where/how the ExecuteTaxonomyFunctions is being called? – Mike Jul 3 '17 at 17:35
  • Hi, this is the code block that isn't working. It does work when I hard code the values as shown in the first code section, but change it to use a variable as shown in the second code section, and the whole thing fails. See comments in 3rd code section: lmi.set_termLabel('IT'); //Variable used here will stop item from being created – Marmalade Jul 4 '17 at 8:29
  • technically, you have hardcoded the values in the code block above, stating it doesn't work when using a variable. How are you calling the function? There may be a disconnect or the data type of the variable is incorrect. – Mike Jul 18 '17 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.