UNPKG

sp-js-provisioning

Version:
379 lines 20.1 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."); }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; import initSpfxJsom, { ExecuteJsomQuery } from 'spfx-jsom'; import { HandlerBase } from './handlerbase'; /** * Describes the Content Types Object Handler */ var ContentTypes = /** @class */ (function (_super) { __extends(ContentTypes, _super); /** * Creates a new instance of the ObjectSiteFields class */ function ContentTypes(config) { return _super.call(this, 'ContentTypes', config) || this; } /** * Provisioning Content Types * * @param web - The web * @param contentTypes - The content types * @param context - Provisioning context */ ContentTypes.prototype.ProvisionObjects = function (web, contentTypes, context) { return __awaiter(this, void 0, void 0, function () { var _a, error_1; var _this = this; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = this; return [4 /*yield*/, initSpfxJsom(context.web.ServerRelativeUrl)]; case 1: _a.jsomContext = (_b.sent()).jsomContext; this.context = context; _super.prototype.scope_started.call(this); _b.label = 2; case 2: _b.trys.push([2, 5, , 6]); return [4 /*yield*/, this._initContext(web)]; case 3: _b.sent(); return [4 /*yield*/, contentTypes .sort(function (a, b) { if (a.ID < b.ID) { return -1; } if (a.ID > b.ID) { return 1; } return 0; }) .reduce(function (chain, contentType) { return chain.then(function () { return _this.processContentType(contentType); }); }, Promise.resolve())]; case 4: _b.sent(); return [3 /*break*/, 6]; case 5: error_1 = _b.sent(); _super.prototype.scope_ended.call(this, error_1); throw error_1; case 6: return [2 /*return*/]; } }); }); }; /** * Provision a content type * * @param contentType - Content type */ ContentTypes.prototype.processContentType = function (contentType) { var _a, _b, _c, _d, _e, _f, _g; return __awaiter(this, void 0, void 0, function () { var contentTypeId, existingContentType, spContentType, _h, error_2; return __generator(this, function (_j) { switch (_j.label) { case 0: _j.trys.push([0, 7, , 8]); contentTypeId = (_a = contentType.ID) !== null && _a !== void 0 ? _a : (_b = this.context.contentTypes[contentType.Name]) === null || _b === void 0 ? void 0 : _b.ID; if (!contentTypeId) throw new Error("Content type with name '" + contentType.Name + "' does not exist in the web."); existingContentType = this.context.contentTypes[contentTypeId]; if (!existingContentType) return [3 /*break*/, 1]; _h = this.jsomContext.web.get_contentTypes().getById(contentTypeId); return [3 /*break*/, 3]; case 1: return [4 /*yield*/, this.createContentType(contentType, contentTypeId)]; case 2: _h = _j.sent(); _j.label = 3; case 3: spContentType = _h; _super.prototype.log_info.call(this, 'processContentType', "Processing content type [" + contentType.Name + "] (" + contentTypeId + ")"); spContentType.set_name(contentType.Name); if (contentType.Description) { spContentType.set_description(contentType.Description); } if (contentType.Group) { spContentType.set_group(contentType.Group); } spContentType.update(true); return [4 /*yield*/, ExecuteJsomQuery(this.jsomContext)]; case 4: _j.sent(); if (!contentType.FieldRefs) return [3 /*break*/, 6]; return [4 /*yield*/, this.processContentTypeFieldRefs(contentType, spContentType)]; case 5: _j.sent(); _j.label = 6; case 6: this.context.contentTypes[contentTypeId] = { ID: contentTypeId, Name: contentType.Name, Description: (_d = (_c = contentType.Description) !== null && _c !== void 0 ? _c : existingContentType === null || existingContentType === void 0 ? void 0 : existingContentType.Description) !== null && _d !== void 0 ? _d : '', Group: (_f = (_e = contentType.Group) !== null && _e !== void 0 ? _e : existingContentType === null || existingContentType === void 0 ? void 0 : existingContentType.Group) !== null && _f !== void 0 ? _f : '', FieldRefs: (_g = contentType.FieldRefs) !== null && _g !== void 0 ? _g : [] }; this.context.contentTypes[contentType.Name] = this.context.contentTypes[contentTypeId]; return [3 /*break*/, 8]; case 7: error_2 = _j.sent(); throw error_2; case 8: return [2 /*return*/]; } }); }); }; ContentTypes.prototype.createContentType = function (contentType, contentTypeId) { return __awaiter(this, void 0, void 0, function () { var ctInfo, ctInfoWithId, spContentType; return __generator(this, function (_a) { switch (_a.label) { case 0: _super.prototype.log_info.call(this, 'createContentType', "Creating content type [" + contentType.Name + "] (" + contentTypeId + ")"); ctInfo = new SP.ContentTypeCreationInformation(); ctInfo.set_name(contentType.Name); ctInfoWithId = ctInfo; ctInfoWithId.set_id(contentTypeId); if (contentType.Description) { ctInfo.set_description(contentType.Description); } if (contentType.Group) { ctInfo.set_group(contentType.Group); } spContentType = this.jsomContext.web.get_contentTypes().add(ctInfo); return [4 /*yield*/, ExecuteJsomQuery(this.jsomContext)]; case 1: _a.sent(); return [2 /*return*/, spContentType]; } }); }); }; ContentTypes.prototype.getExistingFieldLink = function (currentFieldLinks, fieldReference) { var _this = this; var fieldReferenceId = this.normalizeGuid(fieldReference.ID); return currentFieldLinks.find(function (fieldLink) { return (fieldLink.Name === fieldReference.Name || _this.normalizeGuid(fieldLink.ID) === fieldReferenceId); }); }; ContentTypes.prototype.normalizeGuid = function (guid) { return (guid !== null && guid !== void 0 ? guid : '').replace(/[{}]/g, '').toLowerCase(); }; ContentTypes.prototype.getCurrentFieldLinks = function (spContentType) { return __awaiter(this, void 0, void 0, function () { var fieldLinks, currentFieldLinks, enumerator, fieldLink; return __generator(this, function (_a) { switch (_a.label) { case 0: fieldLinks = spContentType.get_fieldLinks(); return [4 /*yield*/, ExecuteJsomQuery(this.jsomContext, [{ clientObject: fieldLinks }])]; case 1: _a.sent(); currentFieldLinks = []; enumerator = fieldLinks.getEnumerator(); while (enumerator.moveNext()) { fieldLink = enumerator.get_current(); currentFieldLinks.push({ ID: fieldLink.get_id().toString(), Name: fieldLink.get_name(), Required: fieldLink.get_required(), Hidden: fieldLink.get_hidden() }); } return [2 /*return*/, currentFieldLinks]; } }); }); }; /** * Adding content type field refs * * @param contentType - Content type * @param spContentType - SP content type */ ContentTypes.prototype.processContentTypeFieldRefs = function (contentType, spContentType) { return __awaiter(this, void 0, void 0, function () { var fieldRefs, currentFieldLinks, _a, _b, _c, index, fieldReference, existingFieldLink, fieldLink, siteField, fieldLinkCreationInformation, fieldRefNames; var e_1, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: fieldRefs = contentType.FieldRefs; return [4 /*yield*/, this.getCurrentFieldLinks(spContentType)]; case 1: currentFieldLinks = _e.sent(); try { for (_a = __values(fieldRefs.entries()), _b = _a.next(); !_b.done; _b = _a.next()) { _c = __read(_b.value, 2), index = _c[0], fieldReference = _c[1]; existingFieldLink = this.getExistingFieldLink(currentFieldLinks, fieldReference); fieldLink = void 0; if (existingFieldLink) { fieldLink = spContentType .get_fieldLinks() .getById(new SP.Guid(existingFieldLink.ID)); } else { _super.prototype.log_info.call(this, 'processContentTypeFieldRefs', "Adding field ref " + fieldReference.Name + " to content type [" + contentType.Name + "] (" + contentType.ID + ")"); siteField = this.jsomContext.web .get_fields() .getByInternalNameOrTitle(fieldReference.Name); fieldLinkCreationInformation = new SP.FieldLinkCreationInformation(); fieldLinkCreationInformation.set_field(siteField); fieldLink = spContentType .get_fieldLinks() .add(fieldLinkCreationInformation); } if (contentType.FieldRefs[index].hasOwnProperty('Required')) { fieldLink.set_required(contentType.FieldRefs[index].Required); } if (contentType.FieldRefs[index].hasOwnProperty('Hidden')) { fieldLink.set_hidden(contentType.FieldRefs[index].Hidden); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_b && !_b.done && (_d = _a.return)) _d.call(_a); } finally { if (e_1) throw e_1.error; } } spContentType.update(true); return [4 /*yield*/, ExecuteJsomQuery(this.jsomContext) // Reorder field links to match the order specified in the schema ]; case 2: _e.sent(); fieldRefNames = fieldRefs .map(function (fr) { return fr.Name; }) .filter(Boolean); if (!(fieldRefNames.length > 1)) return [3 /*break*/, 4]; _super.prototype.log_info.call(this, 'processContentTypeFieldRefs', "Reordering field refs for content type [" + contentType.Name + "] (" + contentType.ID + ")"); spContentType.get_fieldLinks().reorder(fieldRefNames); spContentType.update(true); return [4 /*yield*/, ExecuteJsomQuery(this.jsomContext)]; case 3: _e.sent(); _e.label = 4; case 4: _super.prototype.log_info.call(this, 'processContentTypeFieldRefs', "Successfully processed field refs for content type [" + contentType.Name + "] (" + contentType.ID + ")"); return [2 /*return*/]; } }); }); }; ContentTypes.prototype._initContext = function (web) { return __awaiter(this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = this.context; return [4 /*yield*/, web.contentTypes .select('Id', 'Name', 'Description', 'Group', 'FieldLinks') .expand('FieldLinks')()]; case 1: _a.contentTypes = (_b.sent()).reduce(function (object, contentType) { var _a, _b; var ct = { ID: contentType.Id.StringValue, Name: contentType.Name, Description: (_a = contentType.Description) !== null && _a !== void 0 ? _a : '', Group: (_b = contentType.Group) !== null && _b !== void 0 ? _b : '', FieldRefs: contentType['FieldLinks'].map(function (fieldLink) { return ({ ID: fieldLink.Id, Name: fieldLink.Name, Required: fieldLink.Required, Hidden: fieldLink.Hidden }); }) }; object[ct.Name] = ct; object[ct.ID] = ct; return object; }, {}); return [2 /*return*/]; } }); }); }; return ContentTypes; }(HandlerBase)); export { ContentTypes }; //# sourceMappingURL=contenttypes.js.map