Last active
April 8, 2016 16:44
-
-
Save chrisobriensp/13e69f9dfb9d8221c05c to your computer and use it in GitHub Desktop.
A sample showing how to use "JSOM-based provisioning" to bind taxonomy fields in SharePoint/Office 365.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| window.COB = window.COB || {}; | |
| window.COB.TermSetName = "Countries"; | |
| window.COB.TermSetLocale = "1033"; | |
| window.COB.FieldInternalName = "COB_Countries"; | |
| window.COB.JsomProvisioning = function () { | |
| var context, | |
| termStore, | |
| termSets, | |
| termSet, | |
| termSetName, | |
| fieldInternalName, | |
| termSetLocale, | |
| field, | |
| taxField, | |
| dialog, | |
| dialogTitle = "Please wait", | |
| dialogMessage = "We are getting your site ready..", | |
| dialogHeight = 500, | |
| dialogWidth = 500, | |
| initTaxObjects = function (fieldToBindInternalName, termSetToBindName, termSetToBindLocale) { | |
| console.log("In window.COB.JsomProvisioning.initTaxObjects().."); | |
| fieldInternalName = fieldToBindInternalName; | |
| termSetName = termSetToBindName; | |
| termSetLocale = termSetToBindLocale; | |
| // show dialog during execution of our provisioning steps.. | |
| dialog = SP.UI.ModalDialog.showWaitScreenWithNoClose(dialogTitle, dialogMessage, dialogHeight, dialogWidth); | |
| $('#jsomProvisioningMessage').append("<br /><div>Attempting to fetch term set '" + termSetName + "' and field '" + fieldInternalName + "'."); | |
| context = SP.ClientContext.get_current(); | |
| var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context); | |
| context.load(taxonomySession); | |
| termStore = taxonomySession.getDefaultSiteCollectionTermStore(); | |
| termSets = taxonomySession.getTermSetsByName(termSetName, termSetLocale); | |
| context.load(termStore); | |
| context.load(termSets); | |
| field = context.get_site().get_rootWeb().get_fields().getByInternalNameOrTitle(fieldInternalName); | |
| context.load(field); | |
| context.executeQueryAsync(onInitTaxObjectsSuccess, onInitTaxObjectsFail); | |
| }, | |
| onInitTaxObjectsSuccess = function () { | |
| console.log("In window.COB.JsomProvisioning.onInitTaxObjectsSuccess().."); | |
| var termSetEnumerator = termSets.getEnumerator(); | |
| while (termSetEnumerator.moveNext()) { | |
| var currentTermSet = termSetEnumerator.get_current(); | |
| var currentTermSetName = currentTermSet.get_name(); | |
| if (currentTermSetName === termSetName) { | |
| console.log("Found term set " + termSetName); | |
| termSet = currentTermSet; | |
| } | |
| } | |
| if (termSet != undefined) { | |
| console.log("Term set identified, proceeding.."); | |
| bindTaxonomyField() | |
| } | |
| else { | |
| var msg = "Failed to find term set '" + termSetName + "'. Please check this exists in your Term Store!"; | |
| console.log(msg); | |
| $('#jsomProvisioningMessage').append(msg); | |
| if (dialog != null) { | |
| dialog.close(); | |
| } | |
| } | |
| }, | |
| onInitTaxObjectsFail = function (sender, args) { | |
| alert('Failed to initialise taxonomy objects! Error:' + sender.statusCode); | |
| }, | |
| bindTaxonomyField = function () { | |
| console.log("In window.COB.JsomProvisioning.bindTaxonomyField().."); | |
| taxField = context.castTo(field, SP.Taxonomy.TaxonomyField); | |
| taxField.set_sspId(termStore.get_id().toString()); | |
| taxField.set_termSetId(termSet.get_id()); | |
| taxField.updateAndPushChanges(true); | |
| context.executeQueryAsync(onBindTaxFieldSuccess, onBindTaxFieldFail); | |
| }, | |
| onBindTaxFieldSuccess = function () { | |
| console.log("In window.COB.JsomProvisioning.onBindTaxFieldSuccess().."); | |
| // now we need to close the dialog, BUT I'm pausing for 4 seconds here for demo purposes using setTimeout() - things go too | |
| // fast otherwise. The setTimeout() should be removed in real use.. | |
| setTimeout(function () { | |
| $('#jsomProvisioningMessage').append('<br /><div>Taxonomy field bound successfully..</div>'); | |
| if (dialog != null) { | |
| dialog.close(); | |
| } | |
| }, 4000); | |
| }, | |
| onBindTaxFieldFail = function (sender, args) { | |
| if (dialog != null) { | |
| dialog.close(); | |
| } | |
| alert('Failed to bind taxonomy field! Error:' + sender.statusCode); | |
| } | |
| return { | |
| execute: function () { | |
| console.log("In window.COB.JsomProvisioning.execute().."); | |
| SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () { | |
| console.log("Loaded sp.js"); | |
| SP.SOD.registerSod('sp.taxonomy.js', SP.Utilities.Utility.getLayoutsPageUrl('sp.taxonomy.js')); | |
| SP.SOD.executeFunc('sp.taxonomy.js', 'SP.Taxonomy.TaxonomySession', function () { | |
| console.log("Loaded sp.taxonomy.js"); | |
| SP.SOD.executeFunc('sp.ui.dialog.js', 'SP.UI.ModalDialog.showWaitScreenWithNoClose', function () { | |
| console.log("Loaded all files.."); | |
| initTaxObjects(window.COB.FieldInternalName, window.COB.TermSetName, window.COB.TermSetLocale); | |
| }); | |
| }); | |
| }); | |
| } | |
| } | |
| }(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Chris,
I wonder if you have the code for creating field as part of this. On line 24, the field was passed in. I have been struggling with creating taxonomy site column. I would greatly appreciate if you could give me some recommendations. Thanks SO much, Chris.
function createTaxonomyFieldInHost(hostUrl, groupNm, fieldNm, displayNm) {
_hostwebUrl = hostUrl;
_fieldName = fieldNm;
}
//Success Create a field
function successCreatedTaxonomyField(data) {
var msg = "";
msg += _fieldName + "";