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