UNPKG

sp-js-provisioning

Version:
375 lines 17.6 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; import initSpfxJsom, { ExecuteJsomQuery } from 'spfx-jsom'; import { HandlerBase } from './handlerbase'; /** * Describes the Taxonomy Object Handler. * * Provisions a term group, its term sets and terms into the default site * collection term store using JSOM (so explicit term-set/term GUIDs from the * template are preserved). Idempotent: anything that already exists by ID is * left untouched. */ var Taxonomy = /** @class */ (function (_super) { __extends(Taxonomy, _super); /** * Creates a new instance of the Taxonomy handler. */ function Taxonomy(config) { return _super.call(this, 'Taxonomy', config) || this; } /** * Provisioning taxonomy * * @param web - The web * @param taxonomy - The taxonomy part of the template * @param context - Provisioning context */ Taxonomy.prototype.ProvisionObjects = function (web, taxonomy, context) { var _a; return __awaiter(this, void 0, void 0, function () { var spfxJsom, lcid, group, _b, _c, termSetDef, termSet, e_1_1, error_1; var e_1, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: this.context = context; return [4 /*yield*/, initSpfxJsom(context.web.ServerRelativeUrl, { loadTaxonomy: true })]; case 1: spfxJsom = _e.sent(); this.jsomContext = spfxJsom.jsomContext; this.termStore = spfxJsom.defaultTermStore; _super.prototype.scope_started.call(this); _e.label = 2; case 2: _e.trys.push([2, 14, , 15]); if (!this.termStore) { throw new Error('No default site collection term store is available.'); } this.jsomContext.clientContext.load(this.termStore); return [4 /*yield*/, ExecuteJsomQuery(this.jsomContext)]; case 3: _e.sent(); lcid = this.termStore.get_defaultLanguage(); return [4 /*yield*/, this.ensureGroup(taxonomy.TermGroup)]; case 4: group = _e.sent(); _e.label = 5; case 5: _e.trys.push([5, 11, 12, 13]); _b = __values((_a = taxonomy.TermSets) !== null && _a !== void 0 ? _a : []), _c = _b.next(); _e.label = 6; case 6: if (!!_c.done) return [3 /*break*/, 10]; termSetDef = _c.value; return [4 /*yield*/, this.ensureTermSet(group, termSetDef, lcid)]; case 7: termSet = _e.sent(); return [4 /*yield*/, this.ensureTerms(termSet, termSetDef, lcid)]; case 8: _e.sent(); _e.label = 9; case 9: _c = _b.next(); return [3 /*break*/, 6]; case 10: return [3 /*break*/, 13]; case 11: e_1_1 = _e.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 13]; case 12: try { if (_c && !_c.done && (_d = _b.return)) _d.call(_b); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 13: return [3 /*break*/, 15]; case 14: error_1 = _e.sent(); _super.prototype.scope_ended.call(this, error_1); throw error_1; case 15: return [2 /*return*/]; } }); }); }; /** * Ensure the term group exists, creating it with the specified ID if missing. * * @param groupDef - Term group definition */ Taxonomy.prototype.ensureGroup = function (groupDef) { return __awaiter(this, void 0, void 0, function () { var groupId, existing, group; return __generator(this, function (_a) { switch (_a.label) { case 0: groupId = new SP.Guid(groupDef.Id); existing = this.termStore.getGroup(groupId); return [4 /*yield*/, this.exists(existing)]; case 1: if (_a.sent()) { _super.prototype.log_info.call(this, 'ensureGroup', "Term group " + groupDef.Name + " already exists"); return [2 /*return*/, existing]; } _super.prototype.log_info.call(this, 'ensureGroup', "Creating term group " + groupDef.Name); group = this.termStore.createGroup(groupDef.Name, groupId); this.termStore.commitAll(); return [4 /*yield*/, ExecuteJsomQuery(this.jsomContext)]; case 2: _a.sent(); return [2 /*return*/, group]; } }); }); }; /** * Ensure the term set exists under the group, creating it with the specified * ID if missing. * * @param group - Parent term group * @param termSetDef - Term set definition * @param lcid - Default language of the term store */ Taxonomy.prototype.ensureTermSet = function (group, termSetDef, lcid) { return __awaiter(this, void 0, void 0, function () { var termSetId, existing, termSet; return __generator(this, function (_a) { switch (_a.label) { case 0: termSetId = new SP.Guid(termSetDef.Id); existing = this.termStore.getTermSet(termSetId); return [4 /*yield*/, this.exists(existing)]; case 1: if (_a.sent()) { _super.prototype.log_info.call(this, 'ensureTermSet', "Term set " + termSetDef.Name + " already exists"); return [2 /*return*/, existing]; } _super.prototype.log_info.call(this, 'ensureTermSet', "Creating term set " + termSetDef.Name); termSet = group.createTermSet(termSetDef.Name, termSetId, lcid); if (termSetDef.Description) { termSet.set_description(termSetDef.Description); } if (typeof termSetDef.IsOpenForTermCreation === 'boolean') { termSet.set_isOpenForTermCreation(termSetDef.IsOpenForTermCreation); } this.termStore.commitAll(); return [4 /*yield*/, ExecuteJsomQuery(this.jsomContext)]; case 2: _a.sent(); return [2 /*return*/, termSet]; } }); }); }; /** * Ensure all terms exist in the term set (creating missing ones with their * specified IDs and custom properties) and apply the custom sort order. * * @param termSet - Parent term set * @param termSetDef - Term set definition * @param lcid - Default language of the term store */ Taxonomy.prototype.ensureTerms = function (termSet, termSetDef, lcid) { var _a; return __awaiter(this, void 0, void 0, function () { var terms, created, terms_1, terms_1_1, termDef, termId, existing, term, e_2_1; var e_2, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: terms = (_a = termSetDef.Terms) !== null && _a !== void 0 ? _a : []; created = false; _c.label = 1; case 1: _c.trys.push([1, 6, 7, 8]); terms_1 = __values(terms), terms_1_1 = terms_1.next(); _c.label = 2; case 2: if (!!terms_1_1.done) return [3 /*break*/, 5]; termDef = terms_1_1.value; termId = new SP.Guid(termDef.Id); existing = this.termStore.getTerm(termId); return [4 /*yield*/, this.exists(existing)]; case 3: if (_c.sent()) { _super.prototype.log_info.call(this, 'ensureTerms', "Term " + termDef.Name + " already exists"); return [3 /*break*/, 4]; } _super.prototype.log_info.call(this, 'ensureTerms', "Creating term " + termDef.Name); term = termSet.createTerm(termDef.Name, lcid, termId); this.applyCustomProperties(term, termDef); created = true; _c.label = 4; case 4: terms_1_1 = terms_1.next(); return [3 /*break*/, 2]; case 5: return [3 /*break*/, 8]; case 6: e_2_1 = _c.sent(); e_2 = { error: e_2_1 }; return [3 /*break*/, 8]; case 7: try { if (terms_1_1 && !terms_1_1.done && (_b = terms_1.return)) _b.call(terms_1); } finally { if (e_2) throw e_2.error; } return [7 /*endfinally*/]; case 8: if (!created) return [3 /*break*/, 10]; this.termStore.commitAll(); return [4 /*yield*/, ExecuteJsomQuery(this.jsomContext)]; case 9: _c.sent(); _c.label = 10; case 10: return [4 /*yield*/, this.applySortOrder(termSet, terms)]; case 11: _c.sent(); return [2 /*return*/]; } }); }); }; /** * Apply custom properties to a newly created term. * * @param term - The term * @param termDef - Term definition */ Taxonomy.prototype.applyCustomProperties = function (term, termDef) { var e_3, _a; if (!termDef.CustomProperties) return; try { for (var _b = __values(Object.keys(termDef.CustomProperties)), _c = _b.next(); !_c.done; _c = _b.next()) { var key = _c.value; term.setCustomProperty(key, termDef.CustomProperties[key]); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_3) throw e_3.error; } } }; /** * Apply the custom sort order of a term set based on the `SortOrder` of its * terms. * * @param termSet - The term set * @param terms - Term definitions */ Taxonomy.prototype.applySortOrder = function (termSet, terms) { return __awaiter(this, void 0, void 0, function () { var ordered; return __generator(this, function (_a) { switch (_a.label) { case 0: ordered = terms .filter(function (term) { return typeof term.SortOrder === 'number'; }) .sort(function (a, b) { return a.SortOrder - b.SortOrder; }); if (ordered.length < 2) return [2 /*return*/]; termSet.set_customSortOrder(ordered.map(function (term) { return term.Id; }).join(':')); this.termStore.commitAll(); return [4 /*yield*/, ExecuteJsomQuery(this.jsomContext)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; /** * Load a taxonomy client object and return whether it exists on the server. * * @param clientObject - The taxonomy client object (group/set/term) */ Taxonomy.prototype.exists = function (clientObject) { return __awaiter(this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: this.jsomContext.clientContext.load(clientObject); _b.label = 1; case 1: _b.trys.push([1, 3, , 4]); return [4 /*yield*/, ExecuteJsomQuery(this.jsomContext)]; case 2: _b.sent(); return [2 /*return*/, !clientObject.get_serverObjectIsNull()]; case 3: _a = _b.sent(); return [2 /*return*/, false]; case 4: return [2 /*return*/]; } }); }); }; return Taxonomy; }(HandlerBase)); export { Taxonomy }; //# sourceMappingURL=taxonomy.js.map