If you are using the JavaScript Object Model (JSOM) for your SharePoint solution that involves taxonomy, and you need to know if there have been any changes to the taxonomy since a certain date. Then this blog may help you.
The getChanges method
I needed a way to find out if there had been any changes to the taxonomy that could be selected in a managed metadata field, since a certain date. So I’ve searched in the existing documentation and the rest of the internet for clues on how to achieve this.
The best method to match my needs was the “getChanges” method on the TermStore, TermGroup, TermSet objects (SP.Taxonomy namespace).
As the documentation describes that the function expects “changeInformation” as an argument, and will return a SP.Taxonomy.ChangedItemCollection. However, it appears to not be documented how a SP.Taxonomy.ChangeInformation object is created. Since I couldn’t find an existing example online, I thought it would be worth adding an example to the internet with this blog:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
// This is the function that really illustrates how to use the getChanges method. //tax: any object instance of TermStore, TermGroup or TermSet (from the SP.Taxonomy namespace). The changes will be retrieved from the taxonomy tree starting at this instance. //start: a JavaScript date object, all changes to the taxonomy tree after this time are requested from SharePoint. //done: a function to call if the changes are retrieved //fail: a function to call if something went wrong function getChangesFromSharePoint(tax, start, done, fail){ var context = tax.get_context(); var changeInfo = new window.SP.Taxonomy.ChangeInformation(context); changeInfo.set_startTime(start); var changes = tax.getChanges(changeInfo); context.load(changes); context.executeQueryAsync(function loadedChanges(){ done(changes); }, fail); } // This is just an example use case, where the first found termstore is used to execute the getChanges on. // for a production scenario, you probably need to be more specific which Taxonomy Object the getChanges is called on. //start: a JavaScript date object to specify the time after which all changes should be retrieved //done: function to call when changes are retrieved //fail; function to call if something went wrong function getChangesForAnyTermStore(start, done, fail) { var context = window.SP.ClientContext.get_current(); var session = new window.SP.Taxonomy.TaxonomySession.getTaxonomySession(context); var stores = session.get_termStores(); context.load(stores); context.executeQueryAsync(function storesLoaded() { var enumerator = stores.getEnumerator(); if(enumerator.moveNext()) { var termStore = enumerator.get_current(); getChangesFromSharePoint(termStore, start, done, fail); } else { fail("no term store found"); } }, fail); } //Execute this after you have loaded SP.Runtime.js, SP.js, SP.Core.js and SP.Taxonomy.js (for example by using Script On Demand) function example(){ getChangesForAnyTermStore(new Date(2018, 1, 1), function done(changes){ console.log('found ' + changes.get_count() + ' changes'); }, function fail(sender, args){ console.error(args.get_message()); }); } //A simplified example on how to use Script On Demand to load a script // in specific situations, for example if you use a custom key to refer to a script, // you may want to use the function NormalizeSodKey (I haven't had a need for NormalizeListViewSodKey yet) to ensure you use the correct key for example. // Also it may be worth to implement a retry mechanism if the ExecuteOrDelayUntilScriptLoaded takes more than 5 seconds to execute the first time. // all of this is out of scope for this blog, so I've left it out of the example. function loadJs(key, done) { var _v_dictSod = window._v_dictSod; if (_v_dictSod && !(key in _v_dictSod)) { window.SP.SOD.registerSod(key, "/_layouts/15/" + key); } window.LoadSodByKey(key); window.ExecuteOrDelayUntilScriptLoaded(done, key); } //Include these lines to execute the example automatically after the scripts are loaded. loadJs("SP.Runtime.js", function() { loadJs("SP.js", function() { loadJs("SP.core.js", function(){ loadJs("SP.Taxonomy.js", function(){ example(); }); }); }); }); |