UNPKG

@pnp/sp

Version:

pnp - provides a fluent api for working with SharePoint REST

259 lines • 10.5 kB
import { __decorate } from "tslib"; import { _SPInstance, spInvokableFactory, SPQueryable } from "../spqueryable.js"; import { defaultPath } from "../decorators.js"; import { Web } from "../webs/types.js"; import { combine, hOP, isArray } from "@pnp/core"; import { body, TextParse } from "@pnp/queryable"; import { odataUrlFrom } from "../utils/odata-url-from.js"; import { spPatch, spPost } from "../operations.js"; import { extractWebUrl } from "../utils/extract-web-url.js"; import { emptyGuid } from "../types.js"; /** * Ensures that whatever url is passed to the constructor we can correctly rebase it to a site url * * @param candidate The candidate site url * @param path The caller supplied path, which may contain _api, meaning we don't append _api/site */ function rebaseSiteUrl(candidate, path) { let replace = "_api/site"; // this allows us to both: // - test if `candidate` already has an api path // - ensure that we append the correct one as sometimes a web is not defined // by _api/web, in the case of _api/site/rootweb for example const matches = /(_api[/|\\](site|web))/i.exec(candidate); if ((matches === null || matches === void 0 ? void 0 : matches.length) > 0) { // we want just the base url part (before the _api) candidate = extractWebUrl(candidate); // we want to ensure we put back the correct string replace = matches[1]; } // we only need to append the _api part IF `path` doesn't already include it. if ((path === null || path === void 0 ? void 0 : path.indexOf("_api")) < 0) { candidate = combine(candidate, replace); } return candidate; } let _Site = class _Site extends _SPInstance { constructor(base, path) { if (typeof base === "string") { base = rebaseSiteUrl(base, path); } else if (isArray(base)) { base = [base[0], rebaseSiteUrl(base[1], path)]; } else { base = [base, rebaseSiteUrl(base.toUrl(), path)]; } super(base, path); } /** * Gets the root web of the site collection * */ get rootWeb() { return Web(this, "rootweb"); } /** * Returns the collection of changes from the change log that have occurred within the list, based on the specified query * * @param query The change query */ getChanges(query) { const postBody = body({ query }); return spPost(Web(this, "getchanges"), postBody); } /** * Opens a web by id (using POST) * * @param webId The GUID id of the web to open */ async openWebById(webId) { const data = await spPost(Site(this, `openWebById('${webId}')`)); return { data, web: Web([this, extractWebUrl(odataUrlFrom(data))]), }; } /** * Gets a Web instance representing the root web of the site collection * correctly setup for chaining within the library */ async getRootWeb() { const web = await this.rootWeb.select("Url")(); return Web([this, web.Url]); } /** * Deletes the current site * */ async delete() { const site = await Site(this, "").select("Id")(); const q = Site([this, this.parentUrl], "_api/SPSiteManager/Delete"); await spPost(q, body({ siteId: site.Id })); } /** * Gets the document libraries on a site. Static method. (SharePoint Online only) * * @param absoluteWebUrl The absolute url of the web whose document libraries should be returned */ async getDocumentLibraries(absoluteWebUrl) { const q = Site([this, this.parentUrl], "_api/sp.web.getdocumentlibraries(@v)"); q.query.set("@v", `'${absoluteWebUrl}'`); const data = await q(); return hOP(data, "GetDocumentLibraries") ? data.GetDocumentLibraries : data; } /** * Gets the site url from a page url * * @param absolutePageUrl The absolute url of the page */ async getWebUrlFromPageUrl(absolutePageUrl) { const q = Site([this, this.parentUrl], "_api/sp.web.getweburlfrompageurl(@v)"); q.query.set("@v", `'${absolutePageUrl}'`); const data = await q(); return hOP(data, "GetWebUrlFromPageUrl") ? data.GetWebUrlFromPageUrl : data; } /** * Creates a Modern communication site. * * @param title The title of the site to create * @param lcid The language to use for the site. If not specified will default to 1033 (English). * @param shareByEmailEnabled If set to true, it will enable sharing files via Email. By default it is set to false * @param url The fully qualified URL (e.g. https://yourtenant.sharepoint.com/sites/mysitecollection) of the site. * @param description The description of the communication site. * @param classification The Site classification to use. For instance 'Contoso Classified'. See https://www.youtube.com/watch?v=E-8Z2ggHcS0 for more information * @param siteDesignId The Guid of the site design to be used. * You can use the below default OOTB GUIDs: * Topic: 00000000-0000-0000-0000-000000000000 * Showcase: 6142d2a0-63a5-4ba0-aede-d9fefca2c767 * Blank: f6cc5403-0d63-442e-96c0-285923709ffc * @param hubSiteId The id of the hub site to which the new site should be associated * @param owner Optional owner value, required if executing the method in app only mode */ async createCommunicationSite(title, lcid = 1033, shareByEmailEnabled = false, url, description, classification, siteDesignId, hubSiteId, owner) { return this.createCommunicationSiteFromProps({ Classification: classification, Description: description, HubSiteId: hubSiteId, Lcid: lcid, Owner: owner, ShareByEmailEnabled: shareByEmailEnabled, SiteDesignId: siteDesignId, Title: title, Url: url, }); } async createCommunicationSiteFromProps(props) { // handle defaults const request = { Classification: "", Description: "", HubSiteId: emptyGuid, Lcid: 1033, ShareByEmailEnabled: false, SiteDesignId: emptyGuid, WebTemplate: "SITEPAGEPUBLISHING#0", WebTemplateExtensionId: emptyGuid, ...props, }; return spPost(Site([this, extractWebUrl(this.toUrl())], "/_api/SPSiteManager/Create"), body({ request })); } /** * * @param url Site Url that you want to check if exists */ async exists(url) { return spPost(Site([this, extractWebUrl(this.toUrl())], "/_api/SP.Site.Exists"), body({ url })); } /** * Creates a Modern team site backed by Office 365 group. For use in SP Online only. This will not work with App-only tokens * * @param displayName The title or display name of the Modern team site to be created * @param alias Alias of the underlying Office 365 Group * @param isPublic Defines whether the Office 365 Group will be public (default), or private. * @param lcid The language to use for the site. If not specified will default to English (1033). * @param description The description of the site to be created. * @param classification The Site classification to use. For instance 'Contoso Classified'. See https://www.youtube.com/watch?v=E-8Z2ggHcS0 for more information * @param owners The Owners of the site to be created */ async createModernTeamSite(displayName, alias, isPublic, lcid, description, classification, owners, hubSiteId, siteDesignId) { return this.createModernTeamSiteFromProps({ alias, classification, description, displayName, hubSiteId, isPublic, lcid, owners, siteDesignId, }); } async createModernTeamSiteFromProps(props) { // handle defaults const p = Object.assign({}, { classification: "", description: "", hubSiteId: emptyGuid, isPublic: true, lcid: 1033, owners: [], }, props); const postBody = { alias: p.alias, displayName: p.displayName, isPublic: p.isPublic, optionalParams: { Classification: p.classification, CreationOptions: [`SPSiteLanguage:${p.lcid}`, `HubSiteId:${p.hubSiteId}`], Description: p.description, Owners: p.owners, }, }; if (p.siteDesignId) { postBody.optionalParams.CreationOptions.push(`implicit_formula_292aa8a00786498a87a5ca52d9f4214a_${p.siteDesignId}`); } return spPost(Site([this, extractWebUrl(this.toUrl())], "/_api/GroupSiteManager/CreateGroupEx").using(TextParse()), body(postBody)); } update(props) { return spPatch(this, body(props)); } /** * Set's the site's `Site Logo` property, vs the Site Icon property available on the web's properties * * @param logoProperties An instance of ISiteLogoProperties which sets the new site logo. */ setSiteLogo(logoProperties) { return spPost(SPQueryable([this, extractWebUrl(this.toUrl())], "_api/siteiconmanager/setsitelogo"), body(logoProperties)); } }; _Site = __decorate([ defaultPath("_api/site") ], _Site); export { _Site }; export const Site = spInvokableFactory(_Site); export var SiteLogoType; (function (SiteLogoType) { /** * Site header logo */ SiteLogoType[SiteLogoType["WebLogo"] = 0] = "WebLogo"; /** * Hub site logo */ SiteLogoType[SiteLogoType["HubLogo"] = 1] = "HubLogo"; /** * Header background image */ SiteLogoType[SiteLogoType["HeaderBackground"] = 2] = "HeaderBackground"; /** * Global navigation logo */ SiteLogoType[SiteLogoType["GlobalNavLogo"] = 3] = "GlobalNavLogo"; })(SiteLogoType || (SiteLogoType = {})); export var SiteLogoAspect; (function (SiteLogoAspect) { SiteLogoAspect[SiteLogoAspect["Square"] = 0] = "Square"; SiteLogoAspect[SiteLogoAspect["Rectangular"] = 1] = "Rectangular"; })(SiteLogoAspect || (SiteLogoAspect = {})); //# sourceMappingURL=types.js.map