Skip to content

Adds "spo tenant site get" command. Closes #6654 #6752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
449 changes: 449 additions & 0 deletions docs/docs/cmd/spo/tenant/tenant-site-get.mdx

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions docs/src/config/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4026,6 +4026,11 @@ const sidebars: SidebarsConfig = {
label: 'tenant site archive',
id: 'cmd/spo/tenant/tenant-site-archive'
},
{
type: 'doc',
label: 'tenant site get',
id: 'cmd/spo/tenant/tenant-site-get'
},
{
type: 'doc',
label: 'tenant site list',
Expand Down
1 change: 1 addition & 0 deletions src/m365/spo/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ export default {
TENANT_SETTINGS_LIST: `${prefix} tenant settings list`,
TENANT_SETTINGS_SET: `${prefix} tenant settings set`,
TENANT_SITE_ARCHIVE: `${prefix} tenant site archive`,
TENANT_SITE_GET: `${prefix} tenant site get`,
TENANT_SITE_LIST: `${prefix} tenant site list`,
TENANT_SITE_RENAME: `${prefix} tenant site rename`,
TENANT_SITE_MEMBERSHIP_LIST: `${prefix} tenant site membership list`,
Expand Down
355 changes: 355 additions & 0 deletions src/m365/spo/commands/tenant/tenant-site-get.spec.ts

Large diffs are not rendered by default.

107 changes: 107 additions & 0 deletions src/m365/spo/commands/tenant/tenant-site-get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { z } from 'zod';
import { zod } from '../../../../utils/zod.js';
import { Logger } from '../../../../cli/Logger.js';
import { globalOptionsZod } from '../../../../Command.js';
import request, { CliRequestOptions } from '../../../../request.js';
import { validation } from '../../../../utils/validation.js';
import SpoCommand from '../../../base/SpoCommand.js';
import commands from '../../commands.js';
import { spo } from '../../../../utils/spo.js';
import { TenantSiteProperties } from './TenantSiteProperties.js';
import { formatting } from '../../../../utils/formatting.js';
import { cli } from '../../../../cli/cli.js';

const options = globalOptionsZod
.extend({
id: zod.alias('i', z.string())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like there is an issue if you use another option than url

Image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the short options are not working properly

Image

This is probably because you have to include all refiners logic into the alias function.

.refine(id => validation.isValidGuid(id), id => ({
message: `'${id}' is not a valid GUID.`
})).optional(),
title: zod.alias('t', z.string().optional()),
url: zod.alias('u', z.string().optional())
.refine(url => url !== undefined && (validation.isValidSharePointUrl(url)), {
message: `The value for 'url' must be a valid SharePoint URL or a server-relative URL starting with '/'`
})
})
.strict();

declare type Options = z.infer<typeof options>;

interface CommandArgs {
options: Options;
}

class SpoTenantSiteGetCommand extends SpoCommand {
public get name(): string {
return commands.TENANT_SITE_GET;
}

public get description(): string {
return 'Retrieves information of a specific site as admin';
}

public get schema(): z.ZodTypeAny {
return options;
}

public getRefinedSchema(schema: typeof options): z.ZodEffects<any> | undefined {
return schema
.refine(options => [options.id, options.title, options.url].filter(x => x !== undefined).length === 1, {
message: `Specify either id, title, or url, but not multiple.`
});
}

public async commandAction(logger: Logger, args: CommandArgs): Promise<void> {
if (this.verbose) {
await logger.logToStderr(`Retrieving information about site ${args.options.id || args.options.title || args.options.url}...`);
}

try {
const spoAdminUrl: string = await spo.getSpoAdminUrl(logger, this.verbose);
let tenantSiteInfo: TenantSiteProperties | undefined;

if (args.options.id) {
tenantSiteInfo = await this.getTenantSiteById(spoAdminUrl, args.options.id);
}
else if (args.options.title) {
const allSites: TenantSiteProperties[] = await spo.getAllSites(spoAdminUrl, logger, this.verbose, '', false, '');
const filterSites: TenantSiteProperties[] = allSites.filter(site => site.Title?.toLowerCase() === args.options.title?.toLowerCase());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const filterSites: TenantSiteProperties[] = allSites.filter(site => site.Title?.toLowerCase() === args.options.title?.toLowerCase());
const filterSites: TenantSiteProperties[] = allSites.filter(site => site.Title?.toLowerCase() === args.options.title!.toLowerCase());


if (filterSites.length === 0) {
throw `No site found with title '${args.options.title}'`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw `No site found with title '${args.options.title}'`;
throw `No site found with title '${args.options.title}'.`;

}
else if (filterSites.length === 1) {
tenantSiteInfo = await this.getTenantSiteById(spoAdminUrl, formatting.extractCsomGuid(filterSites[0].SiteId!));
}
else {
const resultAsKeyValuePair = formatting.convertArrayToHashTable('Url', filterSites);
const result = await cli.handleMultipleResultsFound<{ SiteId: string }>(`Multiple sites with title '${args.options.title}' found.`, resultAsKeyValuePair);

tenantSiteInfo = await this.getTenantSiteById(spoAdminUrl, formatting.extractCsomGuid(result.SiteId));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that the endpoint that retrieves the site by ID returns an empty site ID GUID as result. This is pretty annoying for the user of course. I suggest that we retrieve the site by URL since the result there seems more complete. Let's do the same for line 74.

}
}
else if (args.options.url) {
tenantSiteInfo = await spo.getSiteAdminPropertiesByUrl(args.options.url, false, logger, this.verbose);
}

await logger.log(tenantSiteInfo);
}
catch (err: any) {
this.handleRejectedODataJsonPromise(err);
}
}

private async getTenantSiteById(spoAdminUrl: string, id: string): Promise<TenantSiteProperties> {
const requestOptions: CliRequestOptions = {
url: `${spoAdminUrl}/_api/SPO.Tenant/sites('${id}')`,
headers: {
accept: 'application/json;odata=nometadata'
},
responseType: 'json'
};

return request.get<TenantSiteProperties>(requestOptions);
}
}

export default new SpoTenantSiteGetCommand();
Loading