How to fill in a Nintex managed metadata control dynamically?

  • 5 November 2014
  • 5 replies
  • 29 views

Badge +1

Hi Nintex Community,

 

I try to automatically fill in a managed metadata control based on user profil properties values and I wonder If there is a way to reach this dynamically with javascript?

 

Thanks.


5 replies

Badge +1

Hi everyone,

 

I finally managed to populate a Nintex managed metadata control with jquery!

 

Here is the explained code :

 

/* Modify the metadata field */ var metadataField = $('#' + fieldID).find('div[role="textbox"]'); metadataField.html("Term");  /* Update the hidden input value (Useless if terms have unique name) */ var hiddenField = $('#' + fieldID).find('input[type="hidden"]'); hiddenField.val("Term|00000000-0000-0000-0000-000000000000");  /* Get the parent HTML element (same id without "editableRegion" at the end)*/ var parentId = metadataField.attr("id").replace("editableRegion",""); var element = $('#' +parentId).get(0);  /* Validate my metadata field */ var controlObject = new Microsoft.SharePoint.Taxonomy.ControlObject(element); controlObject.validateAll();

 

Hope it'ill help.

Badge

Thank you so much Canatan, you saved my day!

In my case it was easier to assign a css class name to the control and then replace this line:

var metadataField = $('#' + fieldID).find('div[role="textbox"]');

for this one:

var metadataField = $('.class_name').find('div[role="textbox"]');

For some reason it didn't find the control with the field id.

Thanks anyway!

Badge +9

Catatan

Are you talking about a Metadata Control Nintex Forms 2013 ?  I am using Office 365 Forms and there aren't Managed Metadata Controls as far as I can see.  I have managed metadata columns  in my custom list but Nintex Forms ignores them ;-(

Daniel

Badge +5

I'm not clear on how you implemented this in Nintex.  Could you provide more detail on the application of the solution?

 

Thank you.

 

Ken

Badge +3

Hi Ken,

Here is a very condensed how-to:

  1. Set a managed metadata control on your Nintex Form
    215597_pastedImage_2.png
  2. Give the control a JavaScript Client ID (under the "Advanced" section of the control's settings)
    215587_pastedImage_1.png
  3. Write a script to get a particular term (you need the Term Label and GUID both). Below is an extremely abbreviated way to get a specific Metadata_Field from a specific List Item:
    function getAssociatedTerm(ID) {

    var ctx = new SP.ClientContext.get_current(); // Get the client context
    var web = ctx.get_web(); // Get the current web (the site where the target list resides)
    var list = web.get_lists().getByTitle('ListName'); // Get the target list
    var listItem = list.getItemById(ID); // Get the target list item by ID
    ctx.load(listItem, ['Metadata_Field']); // Load the item into the context, including an array of specific fields you want to access
    ctx.executeQueryAsync(function () { // execute the query and formally load the list item. This is an abridged implementation of executeQueryAsync()
    console.log('success!')
    });
    var metadataField = listItem.get_item('Metadata_Field'); // Assign the metadata field value to a variable
    var termId, termLabel
    termId = metadataField.TermGuid// Get the term GUID 
    termLabel = metadataField.Label// Get the term Label
    // Send the Term Label & GUID to another function to update the Nintex Form Control
    setGift_Recipient(termLabel,termId);
    }
  4. Adapt Canatan's script (here I made it into a function):
    function setGift_Recipient(termLabel, termId) {
    if (termLabel && termId) {
    console.log("setting field value...");
    // Get the target list
    var newTerm = termLabel + "|" + termId// You must format the string this way in order for SharePoint to recognize it as a managed term

    // Adapted from Canatan Yilmaz's post
    var metadataField = NWF$('#' + Gift_RecipientControl).find('div[role="textbox"]'); 
    metadataField.html(termLabel);
    /* Update the hidden input value (Useless if terms have unique name) */
    var hiddenField = NWF$('#' + Gift_RecipientControl).find('input[type="hidden"]');
    hiddenField.val(newTerm);
    /* Get the parent HTML element (same id without "editableRegion" at the end)*/
    var parentId = metadataField.attr("id").replace("editableRegion", "");
    var element = $('#' + parentId).get(0); /* Validate my metadata field */
    var controlObject = new Microsoft.SharePoint.Taxonomy.ControlObject(element);
    controlObject.validateAll();
    }
    }
  5. Write a script to run when the form is loaded & ready. 
    NWF$(document).ready(function () { // start execution as soon as the form is done loading
    var sourceItemId = GetUrlKeyValue('sourceItemId'); // Get the 'sourceItemId' value from the querystring (e.g. NewItem.aspx?sourceItemId=7) [this is how we know which item to query in getAssociatedTerm() ]
    if (sourceItemId) {
    console.log("?sourceItemId:", sourceItemId);
    getAssociatedTerm(sourceItemId); // Call the above function to get the target term 
    }
    });
  6. Copy & Paste all of your code into the Custom JavaScript section of the Form Settings. Make sure you write your code correctly, because the form will simply fail to load if there are any syntax errors. 
    215599_pastedImage_3.png

6. Save & Publish the form.

Reply