Nintex Form - working with managed metadata fields
/
This is a post I wrote late last year, and applies to the Managed Metadata Fields in Nintex Forms via some "JavaScript middle-tier magic".
Disclaimer
My conversations with the Nintex Form team back then was that at some point they might stop using the SharePoint Managed Metadata Fields and switch to their own control (similar to what they do with the people picker). Then the following JavaScript code around Managed Metadata fields would stop working.
Goals
Nintex has a JavaScript wrapper API (NF.PeoplePickerAPI) around their People Picker Control.
Create something similar that allows me to wrap a Managed Metadata Field.
SPG.ManagedMetadataApi
(function (SPG, $, undefined) {
var ctor = function (elem) {
this.$elem = $(elem);
var $taxonomyEditor = this.$elem.find(".ms-taxonomy");
if ($taxonomyEditor.length) {
this.controlObject = new Microsoft.SharePoint.Taxonomy.ControlObject($taxonomyEditor.get(0));
}
};
ctor.prototype = {
replaceTerm: function(label, termGuid) {
var self = this;
if (self.controlObject == undefined) return;
var term = new Microsoft.SharePoint.Taxonomy.Term(label + "|" + termGuid);
self.controlObject.replaceTerm(term);
},
clear: function() {
var self = this;
self.setRawText("");
},
setRawText: function (text) {
var self = this;
if (self.controlObject == undefined) return;
self.controlObject.enableControl(true);
self.controlObject.setRawText(text);
self.controlObject.retrieveTerms();
self.controlObject.validateAll();
}
};
SPG.ManagedMetadataApi = ctor;
})(window.SPG = window.SPG || {}, NWF$);
Usage
- List: My List
- Field: MMField
/*
using "normal" GET REST request doesn't bring back ManagedMetaDataFields, this is a workaround.
https://social.msdn.microsoft.com/Forums/sharepoint/en-US/92cccd65-ba4c-4870-a858-7cd0e38a0482/how-can-i-use-caml-queries-with-the-rest-api
because GetItems is a method on the List, this needs to be a POST operation.
*/
var url = _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('My List')/GetItems(query=@v1)" +
"?@v1={'ViewXml':'<View><Query><Where><Eq><FieldRef Name=\"ID\"/><Value Type=\"Integer\">" + siteID +
"</Value></Eq></Where></Query></View>'}";
var promise = NWF$.ajax({
type: "POST",
url: url,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": NWF$("#__REQUESTDIGEST").val()
},
contentType: "application/json;odata=verbose",
dataType: "json",
cache: false,
processData: true
});
promise.done(function (data) {
var row = data.d.results[0];
// similar syntax as NF.PeoplePickerApi
var $mmField = new SPG.ManagedMetadataApi("#" + fieldMMField);
$mmField.clear();
if (row.MMField) {
$mmField.replaceTerm(row.MMField.Label, row.MMField.TermGuid);
}
});