1

I have a managed meta data column in my list "Employee" named "Office Location". the metadata column refers to a TermStore "headquarter". How do i get all the values of the termstore in javascript to use in in a dropdown in a form

1 Answer 1

0

Sample script for your reference. myStronglyTypedObj referenced from here.

<script type="text/javascript">

        if (myStronglyTypedObj === undefined) {
            var myStronglyTypedObj = {};
        }

        myStronglyTypedObj = {
            // Object Globals 
            "g": {},
            // Pre-initialization functions ensure scripts SharePoint dependencies are loaded
            "preinit": function () {
                // Load necessary libraries
                SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
                    // Register what you need from SharePoint (in this case the term store)
                    SP.SOD.registerSod('sp.taxonomy.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js'));
                    // Load the registered items
                    SP.SOD.executeFunc('sp.taxonomy.js', 'SP.Taxonomy.TaxonomySession', myStronglyTypedObj.init());
                });
            },
            // The main Initialization function
            "init": function () {
                /* Check if SP.Taxonomy actually exists yet
                 * PLEASE NOTE that it's common that these objects aren't available, even if you've properly loaded them in the "preinit" function. 
                 * This bit of code checks if the object is available, and if it's not, waits for 200ms and then tries again until this object is loaded
                 */
                if (SP.Taxonomy) {
                    console.log("SP.Taxonomy ready... continuing scripts...");
                    myStronglyTypedObj.therest();
                } else {
                    console.log("SP.Taxonomy not ready...  set timeout and try again after 200ms");
                    setTimeout(myStronglyTypedObj.init, 200);
                }
            },
            "therest": function () {
                getTermsFormTermSet();
            }
        }
        // Run whatever pre-initialisation checks you need to, and then load "preinit" as the starting point, for instance 
        myStronglyTypedObj.preinit();
        function getTermsFormTermSet() {
            var clientContext = new SP.ClientContext.get_current();            
            var list = clientContext.get_web().get_lists().getByTitle("Mydoc2");            
            var field = list.get_fields().getByInternalNameOrTitle("MeataField");
            var txField = clientContext.castTo(field, SP.Taxonomy.TaxonomyField);

            clientContext.load(field);
            clientContext.load(txField);
            clientContext.executeQueryAsync(function () {
                var termSetId = txField.get_termSetId().toString();
                var tSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
                var ts = tSession.getDefaultSiteCollectionTermStore();
                var tset = ts.getTermSet(termSetId);               
                var terms = tset.getAllTerms();
                clientContext.load(terms);

                clientContext.executeQueryAsync(
                  function () {
                      var enumerator = terms.getEnumerator();
                      while (enumerator.moveNext()) {
                          var spTerm = enumerator.get_current();
                          console.log(spTerm);
                      }

                  }, function (err) {
                      alert(err.get_message());
                  });
            },
             function error(err) {
                 alert(err.get_message());
             });

        }
    </script>
2
  • txField.get_termSetId().toString(); gives a error saying get_termSetID doesnot exist on type clientobj Nov 26, 2018 at 11:00
  • The error seems caused by sp.taxonomy.js not loaded correctly, if you use developer tool to debug the script, will you see the library loaded correctly?
    – Lee
    Nov 27, 2018 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.