TaxonomyField SetValue method implementation

May 20, 2012 at 3:35 AM
Edited May 20, 2012 at 3: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");
                break;
.....

 

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); }
                        ,500);
                          }
              var getTaxParam = function(val){return decodeURIComponent(val.substring(0,val.indexOf('|')));}
              var setSPField = function(fieldName,fieldType,val){ 
                                var o = new qppfSPFormField(fieldName,fieldType);
                                o.setValue(val);
                                }
              setTax('$Resources:RTSProjects,ProjectObjective',getTaxParam(paramsGET['ProjectObjective']));
              setTax('$Resources:RTSProjects,ProjectTrackingFieldTaskName',getTaxParam(paramsGET['TaskName']));
              setSPField('$Resources:RTSProjects,ProjectYear','SPFieldChoice',paramsGET['ProjectYear']);
              setSPField('$Resources:RTSProjects,ProjectTrackingFieldTaskOrder','SPFieldChoice',paramsGET['TaskOrder']);
          }
                

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

Coordinator
May 20, 2012 at 9:58 AM
Edited May 20, 2012 at 10:00 AM

Hello,

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,

Patrick

May 13, 2015 at 11:11 AM
Hi,

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.

Thanks
Milan
May 13, 2015 at 11:55 AM
Edited May 13, 2015 at 11:58 AM
Hi,

case "SPTaxonomyFieldType":
        //Original
        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...

Thanks
Milan