Skip to content

Instantly share code, notes, and snippets.

@stinaq stinaq/createtermset.js Secret
Created Nov 28, 2013

Embed
What would you like to do?
//Creates a Term Store Group and Term Set in the default term store with fixed GUID:s
setUpTermSet = (function (window) {
"use strict";
//NOTE. The GUIDs here should not be changed,
var lcid = 1053;
var termGroupID = "4B131288-9376-45BF-9D4D-B973066F27E0";
var termGroupName = "My Group";
var termSetID = "7C6B16A5-7212-462D-B96F-722A817D1D25";
var termSetName = "My Term Set";
//Simple error logging
var onError = function (sender,args) {
console.log(args.get_message())
};
//Creating new term group and term set with constant name and ID
var createTermSet = function (context, termStore) {
var termStoreGroup = termStore.createGroup(termGroupName, termGroupID);
var termSet = termStoreGroup.createTermSet(termSetName, termSetID, lcid);
termSet.setCustomProperty('_Sys_Nav_IsNavigationTermSet', "True");
//Some default terms
var term1 = termSet.createTerm("Search", lcid, "7E1B482E-CD82-4B4A-84D3-15F8BBCA7800");
term1.setLocalCustomProperty('_Sys_Nav_SimpleLinkUrl', '/search');
var term2 = termSet.createTerm("Start", lcid, "8902EF42-F8EF-4638-8E47-E435E3B48840");
term2.setLocalCustomProperty('_Sys_Nav_SimpleLinkUrl', '/');
var onSuccess = function () {
console.log("Created term set" + termSetName);
};
context.load(termSet);
context.executeQueryAsync(
onSuccess,
onError
);
};
//Delete a specific term set and term group and then, on success, creating a new one in its place
var deleteAndCreateTermSet = function (context, termSet, termStoreGroup, termStore) {
termSet.deleteObject();
termStoreGroup.deleteObject();
var onSuccess = function () {
console.log("Deleted " + termSetName);
createTermSet(context, termStore);
};
context.load(termSet);
context.executeQueryAsync(
onSuccess,
onError
);
};
//Deletes our term set, if it exists, and then creates it again
var resetTermSet = function (context, termStoreGroup, termStore, termSet) {
var onSuccess = function () {
console.log("Successfully loaded term store");
try{
console.log("Check if term set already exists. If it does, delet it and create a new");
termSet.get_id();
console.log(termSetName + " already exists. Deleting it.. And then creating it again");
deleteAndCreateTermSet(context, termSet, termStoreGroup, termStore);
} catch (e) {
console.log(termSetName + " did not exist. Creating it...");
createTermSet(context, termStore);
}
};
console.log("Loading existing term store..");
context.load(termSet);
context.executeQueryAsync(
onSuccess,
onError
);
};
//Initialization, getting the context and session and default term store
var init = function () {
console.log("Starting script to create term set");
var context = SP.ClientContext.get_current();
var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var termStore = taxonomySession.getDefaultSiteCollectionTermStore();
var termStoreGroup = termStore.getGroup(termGroupID);
var termSet = termStore.getTermSet(termSetID);
resetTermSet(context, termStoreGroup, termStore, termSet);
};
return {
init : init
};
})(window);
setUpTermSet.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.