UNPKG

@pnp/cli-microsoft365

Version:

Manage Microsoft 365 and SharePoint Framework projects on any platform

112 lines 4.76 kB
import { z } from 'zod'; import { globalOptionsZod } from '../../../../Command.js'; import request from '../../../../request.js'; import { formatting } from '../../../../utils/formatting.js'; import { spo } from '../../../../utils/spo.js'; import { validation } from '../../../../utils/validation.js'; import SpoCommand from '../../../base/SpoCommand.js'; import commands from '../../commands.js'; import { cli } from '../../../../cli/cli.js'; export const options = z.strictObject({ ...globalOptionsZod.shape, id: z.uuid().optional().alias('i'), title: z.string().optional().alias('t'), url: z.string().refine(url => validation.isValidSharePointUrl(url) === true, { message: 'Specify a valid SharePoint site URL' }).optional().alias('u') }); class SpoTenantSiteGetCommand extends SpoCommand { get name() { return commands.TENANT_SITE_GET; } get description() { return 'Retrieves the tenant site information'; } get schema() { return options; } getRefinedSchema(schema) { return schema.refine(o => [o.id, o.title, o.url].filter(v => v !== undefined).length === 1, { error: `Specify exactly one of the following options: 'id', 'title', or 'url'.` }); } async commandAction(logger, args) { if (this.verbose) { await logger.logToStderr(`Retrieving tenant site information for site '${args.options.url || args.options.id || args.options.title}'...`); } try { let siteUrl; if (args.options.url) { siteUrl = args.options.url; } else if (args.options.id) { siteUrl = await this.getSiteUrlById(args.options.id, logger); if (this.verbose) { await logger.logToStderr(`Retrieved tenant site URL for site '${args.options.id}'...`); } } else { siteUrl = await this.getSiteUrlByTitle(args.options.title, logger); if (this.verbose) { await logger.logToStderr(`Retrieved tenant site URL for site '${args.options.title}'...`); } } const site = await spo.getSiteAdminPropertiesByUrl(siteUrl, false, logger, this.verbose); await logger.log(site); } catch (err) { this.handleRejectedODataJsonPromise(err); } } async getSiteUrlById(id, logger) { if (this.verbose) { await logger.logToStderr(`Retrieving tenant site URL for site '${id}'...`); } const adminUrl = await spo.getSpoAdminUrl(logger, this.debug); const requestOptions = { url: `${adminUrl}/_api/SPO.Tenant/sites('${id}')?$select=Url`, headers: { accept: 'application/json;odata=nometadata' }, responseType: 'json' }; const res = await request.get(requestOptions); return res.Url; } async getSiteUrlByTitle(title, logger) { if (this.verbose) { await logger.logToStderr(`Retrieving tenant site URL for site '${title}'...`); } const adminUrl = await spo.getSpoAdminUrl(logger, this.debug); const viewXml = `<View><Query><Where><And><IsNull><FieldRef Name="TimeDeleted"/></IsNull><Eq><FieldRef Name="Title"/><Value Type='Text'>${formatting.escapeXml(title)}</Value></Eq></And></Where></Query><ViewFields><FieldRef Name="Title"/><FieldRef Name="SiteUrl"/><FieldRef Name="SiteId"/></ViewFields></View>`; const requestOptions = { url: `${adminUrl}/_api/web/lists/GetByTitle('DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS')/RenderListDataAsStream`, headers: { accept: 'application/json;odata=nometadata' }, responseType: 'json', data: { parameters: { ViewXml: viewXml, DatesInUtc: true } } }; const res = await request.post(requestOptions); const rows = res.Row; if (rows.length === 0) { throw `The specified site '${title}' does not exist.`; } if (rows.length > 1) { const resultAsKeyValuePair = rows.reduce((acc, cur) => { acc[cur.SiteUrl] = { url: cur.SiteUrl }; return acc; }, {}); const selection = await cli.handleMultipleResultsFound(`Multiple sites with title '${title}' found.`, resultAsKeyValuePair); return selection.url; } return rows[0].SiteUrl; } } export default new SpoTenantSiteGetCommand(); //# sourceMappingURL=tenant-site-get.js.map