3

I have a list with managed metadata column. In the new and edit form, I would like to set the value of that field using javascript.

Is it possible to set the Taxonomy Control (TaxonomyWebTaggingControl) from client side using javascript/jQuery. I have tried to change the value in span tag (rendered by the control) to the desired value, but it doesn't seem to be working.

3 Answers 3

5

To manipulate term values in TaxonomyWebTaggingControl control we’ll use Client Object model for Taxonomy, in particular ControlObject capabilities (it is not documented).

In order set the Taxonomy Control (TaxonomyWebTaggingControl) value on the client side the following function could be used:

//Set Taxonomy ControlObject value
//webTaggingId - TaxonomyWebTaggingControl ID 
//termValue - represents a single value held in a TaxonomyField object using the following format: NameOfTerm|GUIDOfTerm 
function setTaxonomyControlObjectValue(webTaggingId, termValue) {
    var webTaggingCtl = $get(webTaggingId);
    var taxCtlObj = new Microsoft.SharePoint.Taxonomy.ControlObject(webTaggingCtl);
    taxCtlObj.enableControl(true);
    taxCtlObj.setRawText(termValue);
    taxCtlObj.retrieveTerms();
}

Note:

a) to find TaxonomyWebTaggingControl on the client side webTaggingId identifier is used, that corresponds to ContainerId property of control

b) termValue represents a single value held in a TaxonomyField object using the following format: NameOfTerm|GUIDOfTerm


Usage

Example 1. Setting TaxonomyWebTaggingControl control value

   //Example 1. Setting TaxonomyWebTaggingControl control value during event (page load, button click and etc.)
    function initCountryTaggingControl() {
        var countriesControlId = "ctl00_PlaceHolderMain_ctl03";
        var continentTermValue = "Africa|43f3b1e7-e40c-45d2-a613-88643668d373";
        setTaxonomyControlObjectValue(countriesControlId, continentTermValue);
    }


ExecuteOrDelayUntilScriptLoaded(initCountryTaggingControl, 'ScriptForWebTaggingUI.js');

Note:

initCountryTaggingControl is executed after script ScriptForWebTaggingUI.js is loaded


P.S. If you are interested how TaxonomyWebTaggingControl works on the client side and how to access and manipulate TaxonomyWebTaggingControl on the client side please follow this article.

2
  • 1
    Works nicely, for anyone wondering how to set multivalues, just separate terms with semi-colon, so continentTermValue becomes: "Africa|43f3b1e7-e40c-45d2-a613-88643668d373;ThisTimeFor|123fdf7a-e40c-45d2-a613-88643668d373;WakaWaka|323fdf7a-140c-45d2-a613-88643668d373"
    – Jussi Palo
    Commented Apr 8, 2014 at 11:23
  • @VadimGremyachev how to find the container id... when defining the control do I need to add ID to TaxonomyWebTaggingControl or use the auto-generated id... Commented Jun 17, 2015 at 5:57
6

The solution provided by Vadim above didn't work for me, although it did help me towards an answer. I achieved this in two parts -

  1. Via jQuery / DOM get the editable DIV (the one with ID = ContainerId + 'editableRegion') and set the text value of the term you want (note, I didn't find it necessary to set the GUID of the term as well, so just "Africa" in Vadim's example).

  2. Get the ContainerId (i.e. as above, but just an ID without the 'editableRegion' part - in some solutions you see this as .parentNode.parentNode.parentNode) as a HtmlDivElement not a jQuery object. If you have it via jQuery do var myDivElement = myJQueryObject.get(0);. Then call a Microsoft function:

    var controlObject = new Microsoft.SharePoint.Taxonomy.ControlObject(document.getElementById(myDivElement));
    controlObject.validateAll();
    

You should then have a validated term (i.e. black text with a solid underline, not red text with a dashed underline)

1
  • I have a taxpicker as this : <div class="cam-taxpicker"><div class="cam-taxpicker-editor" contenteditable="true"></div><div class="cam-taxpicker-button"></div><input type="hidden" id="taxPickerKeywords_0"></div><div class="cam-taxpicker-suggestion-container"></div> Which control I should use in step 1 and which in step 2.... Also when you say to set text in step 1, is it like .text(value) ??
    – zainul
    Commented Mar 14, 2019 at 11:01
1

For some reason Vadim's solution didn't work for me either, but I managed to get it work using both Vadim's and John's code.

function setTaxonomyControlObjectValue(webTaggingId, termValue) {
   var divId = $get(webTaggingId).children[1].id;
   var webTaggingCtl = $get(divId);
   var taxCtlObj = new Microsoft.SharePoint.Taxonomy.ControlObject(webTaggingCtl);
   taxCtlObj.enableControl(true);
   taxCtlObj.setRawText(termValue);
   taxCtlObj.retrieveTerms();
   taxCtlObj.validateAll(); // with this line you don't need term guid, just name of the term
}

Your Answer

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

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