UNPKG

sp-js-provisioning

Version:
272 lines 14 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 }; } }; 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, 4, , 5]); this._initContext(web); 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 3: _b.sent(); return [3 /*break*/, 5]; case 4: error_1 = _b.sent(); _super.prototype.scope_ended.call(this, error_1); throw error_1; case 5: return [2 /*return*/]; } }); }); }; /** * Provision a content type * * @param contentType - Content type */ ContentTypes.prototype.processContentType = function (contentType) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var contentTypeId, spContentType, error_2; return __generator(this, function (_c) { switch (_c.label) { case 0: _c.trys.push([0, 4, , 5]); 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."); _super.prototype.log_info.call(this, 'processContentType', "Processing content type [" + contentType.Name + "] (" + contentTypeId + ")"); spContentType = this.jsomContext.web .get_contentTypes() .getById(contentTypeId); 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 1: _c.sent(); if (!contentType.FieldRefs) return [3 /*break*/, 3]; return [4 /*yield*/, this.processContentTypeFieldRefs(contentType, spContentType)]; case 2: _c.sent(); _c.label = 3; case 3: return [3 /*break*/, 5]; case 4: error_2 = _c.sent(); throw error_2; case 5: return [2 /*return*/]; } }); }); }; /** * Add a content type * * @param web - The web * @param contentType - Content type */ ContentTypes.prototype.addContentType = function (web, contentType) { return __awaiter(this, void 0, void 0, function () { var error_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); _super.prototype.log_info.call(this, 'addContentType', "Adding content type [" + contentType.Name + "] (" + contentType.ID + ")"); return [4 /*yield*/, web.contentTypes.add(contentType.ID, contentType.Name, contentType.Description, contentType.Group)]; case 1: return [2 /*return*/, _a.sent()]; case 2: error_3 = _a.sent(); throw error_3; case 3: return [2 /*return*/]; } }); }); }; /** * 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 _loop_1, this_1, index, error_4; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); _loop_1 = function (index) { var fieldReference = contentType.FieldRefs[index]; var existingFieldLink = this_1.context.contentTypes[contentType.Name].FieldRefs.find(function (fr) { return fr.Name === fieldReference.Name; }); var fieldLink = void 0; if (existingFieldLink) { fieldLink = spContentType .get_fieldLinks() .getById(new SP.Guid(existingFieldLink.ID)); } else { _super.prototype.log_info.call(this_1, 'processContentTypeFieldRefs', "Adding field ref " + fieldReference.Name + " to content type [" + contentType.Name + "] (" + contentType.ID + ")"); var siteField = this_1.jsomContext.web .get_fields() .getByInternalNameOrTitle(fieldReference.Name); var 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); } }; this_1 = this; for (index = 0; index < contentType.FieldRefs.length; index++) { _loop_1(index); } spContentType.update(true); return [4 /*yield*/, ExecuteJsomQuery(this.jsomContext)]; case 1: _a.sent(); _super.prototype.log_info.call(this, 'processContentTypeFieldRefs', "Successfully processed field refs for content type [" + contentType.Name + "] (" + contentType.ID + ")"); return [3 /*break*/, 3]; case 2: error_4 = _a.sent(); _super.prototype.log_info.call(this, 'processContentTypeFieldRefs', "Failed to process field refs for content type [" + contentType.Name + "] (" + contentType.ID + ")", { error: error_4.args && error_4.args.get_message() }); return [3 /*break*/, 3]; case 3: 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', 'FieldLinks') .expand('FieldLinks')()]; case 1: _a.contentTypes = (_b.sent()).reduce(function (object, contentType) { var ct = { ID: contentType.Id.StringValue, Name: contentType.Name, FieldRefs: contentType['FieldLinks'].map(function (fieldLink) { return ({ ID: fieldLink.Id, Name: fieldLink.Name, Required: fieldLink.Required, Hidden: fieldLink.Hidden }); }) }; object[ct.Name] = ct; return object; }, {}); return [2 /*return*/]; } }); }); }; return ContentTypes; }(HandlerBase)); export { ContentTypes }; //# sourceMappingURL=contenttypes.js.map