TaxonomyField SetValue method implementation

May 20, 2012 at 2:35 AM
Edited May 20, 2012 at 2:36 AM

First it's great piece of work and helped my project big time.  For strange reason, the getValue for Taxonomy field is not implemented.  So I wrote my own.  I would like to share it with you:



case "SPTaxonomyFieldType":
            	var editableRegion = this.InputControl.find('div[id$="editableRegion"]');
				result = $.trim($(editableRegion).text());
                //alert("not yet implemented");


And I discovered the Taxonomy setValue method  didn't work when I tried to preset (in NewForm.aspx scenario) Taxonomy fields with Url query parameter values.  That's because the field object can still be in loading mode (calling web services, maybe) even after document.ready().  So I did a workaround using a setTimeout lambda/block approach:

          function projectTagging()
              var setTax = function(fieldName,value){
                    setTimeout(function(){ var o = new qppfSPFormField(fieldName,'SPTaxonomyFieldType');
                    if(o.InputControl.length>0) o.setValue(value); 
                        else setTax(fieldName,value); }
              var getTaxParam = function(val){return decodeURIComponent(val.substring(0,val.indexOf('|')));}
              var setSPField = function(fieldName,fieldType,val){ 
                                var o = new qppfSPFormField(fieldName,fieldType);

I hope it can be helpful for those that are looking to preset Taxonomy field using the client approach.  Thanks again for your sharing.  J

May 20, 2012 at 8:58 AM
Edited May 20, 2012 at 9:00 AM


indeed I did not implement the getValue function for the Taxonomyfield. Thank you very much for sharing your code and using this solution. I am going to implement this and some other additions in the next minor release.

You are right about the fact that the document.ready is not exactly the right time to initialize Taxonomy fields. The problem lies on SharePoint libraries not being loaded at this time. I have a handy solution for you that ensures that you do not have to use some timeout variables.

Try out the function below. This ensures that the objects are loaded correctly for the Taxonomyfield. I will update the documentation accordingly:

ExecuteOrDelayUntilScriptLoaded(SetTaxonomyFields, "sp.js");


The method you want to call is the "SetTaxonomyFields". You can name it as you want.

Thank you very much again for sharing.

Have a nice day,


May 13, 2015 at 10:11 AM

using the code

var editableRegion = this.InputControl.find('div[id$="editableRegion"]');
result = $.trim($(editableRegion).text());

it gives blank result.

I am using code in SP 2013.

May 13, 2015 at 10:55 AM
Edited May 13, 2015 at 10:58 AM

case "SPTaxonomyFieldType":
        this.InputControl = jQuery('div[class="ms-taxonomy-fieldeditor"][title^="' + this.SPFieldDisplayName + '"]');
         ////this might happen when you selected the taxonomy field
        if (this.InputControl.length == 0) {
            this.InputControl = jQuery('div[class="ms-taxonomy-fieldeditor ms-taxonomy-fieldeditor-standard ms-taxonomy-writing"][title^="' + this.SPFieldDisplayName + '"]');
while executing "this.InputControl.length" length is 0 for SP 2013. any hint or Idea very appreciable...