UNPKG

9.82 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { _SPInstance, spInvokableFactory } from "../spqueryable.js";
3import { defaultPath } from "../decorators.js";
4import { Web } from "../webs/types.js";
5import { combine, hOP, isArray } from "@pnp/core";
6import { body, TextParse } from "@pnp/queryable";
7import { odataUrlFrom } from "../utils/odata-url-from.js";
8import { spPost } from "../operations.js";
9import { escapeQueryStrValue } from "../utils/escape-query-str.js";
10import { extractWebUrl } from "../utils/extract-web-url.js";
11import { emptyGuid } from "../types.js";
12/**
13 * Ensures that whatever url is passed to the constructor we can correctly rebase it to a site url
14 *
15 * @param candidate The candidate site url
16 * @param path The caller supplied path, which may contain _api, meaning we don't append _api/site
17 */
18function rebaseSiteUrl(candidate, path) {
19 let replace = "_api/site";
20 // this allows us to both:
21 // - test if `candidate` already has an api path
22 // - ensure that we append the correct one as sometimes a web is not defined
23 // by _api/web, in the case of _api/site/rootweb for example
24 const matches = /(_api[/|\\](site|web))/i.exec(candidate);
25 if ((matches === null || matches === void 0 ? void 0 : matches.length) > 0) {
26 // we want just the base url part (before the _api)
27 candidate = extractWebUrl(candidate);
28 // we want to ensure we put back the correct string
29 replace = matches[1];
30 }
31 // we only need to append the _api part IF `path` doesn't already include it.
32 if ((path === null || path === void 0 ? void 0 : path.indexOf("_api")) < 0) {
33 candidate = combine(candidate, replace);
34 }
35 return candidate;
36}
37let _Site = class _Site extends _SPInstance {
38 constructor(base, path) {
39 if (typeof base === "string") {
40 base = rebaseSiteUrl(base, path);
41 }
42 else if (isArray(base)) {
43 base = [base[0], rebaseSiteUrl(base[1], path)];
44 }
45 else {
46 base = [base, rebaseSiteUrl(base.toUrl(), path)];
47 }
48 super(base, path);
49 }
50 /**
51 * Gets the root web of the site collection
52 *
53 */
54 get rootWeb() {
55 return Web(this, "rootweb");
56 }
57 /**
58 * Returns the collection of changes from the change log that have occurred within the list, based on the specified query
59 *
60 * @param query The change query
61 */
62 getChanges(query) {
63 const postBody = body({ query });
64 return spPost(Web(this, "getchanges"), postBody);
65 }
66 /**
67 * Opens a web by id (using POST)
68 *
69 * @param webId The GUID id of the web to open
70 */
71 async openWebById(webId) {
72 const data = await spPost(Site(this, `openWebById('${webId}')`));
73 return {
74 data,
75 web: Web([this, extractWebUrl(odataUrlFrom(data))]),
76 };
77 }
78 /**
79 * Gets a Web instance representing the root web of the site collection
80 * correctly setup for chaining within the library
81 */
82 async getRootWeb() {
83 const web = await this.rootWeb.select("Url")();
84 return Web([this, web.Url]);
85 }
86 /**
87 * Gets the context information for this site collection
88 */
89 async getContextInfo() {
90 const data = await spPost(Site([this, this.parentUrl], "_api/contextinfo"));
91 if (hOP(data, "GetContextWebInformation")) {
92 const info = data.GetContextWebInformation;
93 info.SupportedSchemaVersions = info.SupportedSchemaVersions.results;
94 return info;
95 }
96 else {
97 return data;
98 }
99 }
100 /**
101 * Deletes the current site
102 *
103 */
104 async delete() {
105 const site = await Site(this, "").select("Id")();
106 const q = Site([this, this.parentUrl], "_api/SPSiteManager/Delete");
107 await spPost(q, body({ siteId: site.Id }));
108 }
109 /**
110 * Gets the document libraries on a site. Static method. (SharePoint Online only)
111 *
112 * @param absoluteWebUrl The absolute url of the web whose document libraries should be returned
113 */
114 async getDocumentLibraries(absoluteWebUrl) {
115 const q = Site([this, this.parentUrl], "_api/sp.web.getdocumentlibraries(@v)");
116 q.query.set("@v", `'${escapeQueryStrValue(absoluteWebUrl)}'`);
117 const data = await q();
118 return hOP(data, "GetDocumentLibraries") ? data.GetDocumentLibraries : data;
119 }
120 /**
121 * Gets the site url from a page url
122 *
123 * @param absolutePageUrl The absolute url of the page
124 */
125 async getWebUrlFromPageUrl(absolutePageUrl) {
126 const q = Site([this, this.parentUrl], "_api/sp.web.getweburlfrompageurl(@v)");
127 q.query.set("@v", `'${escapeQueryStrValue(absolutePageUrl)}'`);
128 const data = await q();
129 return hOP(data, "GetWebUrlFromPageUrl") ? data.GetWebUrlFromPageUrl : data;
130 }
131 /**
132 * Creates a Modern communication site.
133 *
134 * @param title The title of the site to create
135 * @param lcid The language to use for the site. If not specified will default to 1033 (English).
136 * @param shareByEmailEnabled If set to true, it will enable sharing files via Email. By default it is set to false
137 * @param url The fully qualified URL (e.g. https://yourtenant.sharepoint.com/sites/mysitecollection) of the site.
138 * @param description The description of the communication site.
139 * @param classification The Site classification to use. For instance 'Contoso Classified'. See https://www.youtube.com/watch?v=E-8Z2ggHcS0 for more information
140 * @param siteDesignId The Guid of the site design to be used.
141 * You can use the below default OOTB GUIDs:
142 * Topic: 00000000-0000-0000-0000-000000000000
143 * Showcase: 6142d2a0-63a5-4ba0-aede-d9fefca2c767
144 * Blank: f6cc5403-0d63-442e-96c0-285923709ffc
145 * @param hubSiteId The id of the hub site to which the new site should be associated
146 * @param owner Optional owner value, required if executing the method in app only mode
147 */
148 async createCommunicationSite(title, lcid = 1033, shareByEmailEnabled = false, url, description, classification, siteDesignId, hubSiteId, owner) {
149 return this.createCommunicationSiteFromProps({
150 Classification: classification,
151 Description: description,
152 HubSiteId: hubSiteId,
153 Lcid: lcid,
154 Owner: owner,
155 ShareByEmailEnabled: shareByEmailEnabled,
156 SiteDesignId: siteDesignId,
157 Title: title,
158 Url: url,
159 });
160 }
161 async createCommunicationSiteFromProps(props) {
162 // handle defaults
163 const request = {
164 Classification: "",
165 Description: "",
166 HubSiteId: emptyGuid,
167 Lcid: 1033,
168 ShareByEmailEnabled: false,
169 SiteDesignId: emptyGuid,
170 WebTemplate: "SITEPAGEPUBLISHING#0",
171 WebTemplateExtensionId: emptyGuid,
172 ...props,
173 };
174 return spPost(Site([this, extractWebUrl(this.toUrl())], "/_api/SPSiteManager/Create"), body({ request }));
175 }
176 /**
177 *
178 * @param url Site Url that you want to check if exists
179 */
180 async exists(url) {
181 return spPost(Site([this, extractWebUrl(this.toUrl())], "/_api/SP.Site.Exists"), body({ url }));
182 }
183 /**
184 * Creates a Modern team site backed by Office 365 group. For use in SP Online only. This will not work with App-only tokens
185 *
186 * @param displayName The title or display name of the Modern team site to be created
187 * @param alias Alias of the underlying Office 365 Group
188 * @param isPublic Defines whether the Office 365 Group will be public (default), or private.
189 * @param lcid The language to use for the site. If not specified will default to English (1033).
190 * @param description The description of the site to be created.
191 * @param classification The Site classification to use. For instance 'Contoso Classified'. See https://www.youtube.com/watch?v=E-8Z2ggHcS0 for more information
192 * @param owners The Owners of the site to be created
193 */
194 async createModernTeamSite(displayName, alias, isPublic, lcid, description, classification, owners, hubSiteId, siteDesignId) {
195 return this.createModernTeamSiteFromProps({
196 alias,
197 classification,
198 description,
199 displayName,
200 hubSiteId,
201 isPublic,
202 lcid,
203 owners,
204 siteDesignId,
205 });
206 }
207 async createModernTeamSiteFromProps(props) {
208 // handle defaults
209 const p = Object.assign({}, {
210 classification: "",
211 description: "",
212 hubSiteId: emptyGuid,
213 isPublic: true,
214 lcid: 1033,
215 owners: [],
216 }, props);
217 const postBody = {
218 alias: p.alias,
219 displayName: p.displayName,
220 isPublic: p.isPublic,
221 optionalParams: {
222 Classification: p.classification,
223 CreationOptions: [`SPSiteLanguage:${p.lcid}`, `HubSiteId:${p.hubSiteId}`],
224 Description: p.description,
225 Owners: p.owners,
226 },
227 };
228 if (p.siteDesignId) {
229 postBody.optionalParams.CreationOptions.push(`implicit_formula_292aa8a00786498a87a5ca52d9f4214a_${p.siteDesignId}`);
230 }
231 return spPost(Site([this, extractWebUrl(this.toUrl())], "/_api/GroupSiteManager/CreateGroupEx").using(TextParse()), body(postBody));
232 }
233};
234_Site = __decorate([
235 defaultPath("_api/site")
236], _Site);
237export { _Site };
238export const Site = spInvokableFactory(_Site);
239//# sourceMappingURL=types.js.map
\No newline at end of file