diff --git a/.devproxy/api-specs/sharepoint.yaml b/.devproxy/api-specs/sharepoint.yaml index f5d060dc03f..298c95a837f 100644 --- a/.devproxy/api-specs/sharepoint.yaml +++ b/.devproxy/api-specs/sharepoint.yaml @@ -310,6 +310,30 @@ paths: responses: 200: description: OK + /_api/web/GetFolderById({folderId}): + get: + parameters: + - name: folderId + in: path + required: true + description: folder unique ID + schema: + type: string + example: "'b2307a39-e878-458b-bc90-03bc578531d6'" + security: + - delegated: + - AllSites.Read + - AllSites.Write + - AllSites.Manage + - AllSites.FullControl + - application: + - Sites.Read.All + - Sites.ReadWrite.All + - Sites.Manage.All + - Sites.FullControl.All + responses: + 200: + description: OK /_api/web/GetFolderByServerRelativePath(DecodedUrl={folderPath}): get: parameters: @@ -326,6 +350,11 @@ paths: - AllSites.Write - AllSites.Manage - AllSites.FullControl + - application: + - Sites.Read.All + - Sites.ReadWrite.All + - Sites.Manage.All + - Sites.FullControl.All responses: 200: description: OK diff --git a/docs/docs/cmd/spo/folder/folder-archive.mdx b/docs/docs/cmd/spo/folder/folder-archive.mdx new file mode 100644 index 00000000000..d2451288f04 --- /dev/null +++ b/docs/docs/cmd/spo/folder/folder-archive.mdx @@ -0,0 +1,68 @@ +import Global from '../../_global.mdx'; +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + +# spo folder archive + +Archives a folder + +## Usage + +```sh +m365 spo folder archive [options] +``` + +## Options + +```md definition-list +`-u, --webUrl ` +: The URL of the site where the folder is located. + +`--url [url]` +: The server- or site-relative decoded URL of the folder to archive. Specify either `url` or `id`, but not both. + +`-i, --id [id]` +: The UniqueId (GUID) of the folder to archive. Specify either `url` or `id`, but not both. + +`-f, --force` +: Don't prompt for confirmation. +``` + + + +## Permissions + + + + + | Resource | Permissions | + |------------|----------------| + | SharePoint | AllSites.Write | + + + + + | Resource | Permissions | + |------------|---------------------| + | SharePoint | Sites.ReadWrite.All | + + + + +## Examples + +Archive a folder by id without prompting for confirmation + +```sh +m365 spo folder archive --webUrl https://contoso.sharepoint.com/sites/Marketing --id 7a8c9207-7745-4cda-b0e2-be2618ee3030 --force +``` + +Archive a folder by URL with prompting for confirmation + +```sh +m365 spo folder archive --webUrl https://contoso.sharepoint.com/sites/Marketing --url '/sites/Marketing/shared documents/folder' +``` + +## Response + +The command won't return a response on success. diff --git a/docs/src/config/sidebars.ts b/docs/src/config/sidebars.ts index e6e537e6841..8f94f05888d 100644 --- a/docs/src/config/sidebars.ts +++ b/docs/src/config/sidebars.ts @@ -2860,6 +2860,11 @@ const sidebars: SidebarsConfig = { label: 'folder add', id: 'cmd/spo/folder/folder-add' }, + { + type: 'doc', + label: 'folder archive', + id: 'cmd/spo/folder/folder-archive' + }, { type: 'doc', label: 'folder copy', diff --git a/src/m365/spo/commands.ts b/src/m365/spo/commands.ts index f5c71f8667a..50b47ab3d60 100644 --- a/src/m365/spo/commands.ts +++ b/src/m365/spo/commands.ts @@ -93,6 +93,7 @@ export default { FILE_VERSION_REMOVE: `${prefix} file version remove`, FILE_VERSION_RESTORE: `${prefix} file version restore`, FOLDER_ADD: `${prefix} folder add`, + FOLDER_ARCHIVE: `${prefix} folder archive`, FOLDER_COPY: `${prefix} folder copy`, FOLDER_GET: `${prefix} folder get`, FOLDER_LIST: `${prefix} folder list`, diff --git a/src/m365/spo/commands/folder/folder-archive.spec.ts b/src/m365/spo/commands/folder/folder-archive.spec.ts new file mode 100644 index 00000000000..f3e68a15707 --- /dev/null +++ b/src/m365/spo/commands/folder/folder-archive.spec.ts @@ -0,0 +1,362 @@ +import assert from 'assert'; +import sinon from 'sinon'; +import auth from '../../../../Auth.js'; +import { cli } from '../../../../cli/cli.js'; +import { CommandInfo } from '../../../../cli/CommandInfo.js'; +import { Logger } from '../../../../cli/Logger.js'; +import { CommandError } from '../../../../Command.js'; +import request from '../../../../request.js'; +import { telemetry } from '../../../../telemetry.js'; +import { pid } from '../../../../utils/pid.js'; +import { session } from '../../../../utils/session.js'; +import { sinonUtil } from '../../../../utils/sinonUtil.js'; +import { formatting } from '../../../../utils/formatting.js'; +import { z } from 'zod'; +import commands from '../../commands.js'; +import command from './folder-archive.js'; + +describe(commands.FOLDER_ARCHIVE, () => { + let log: any[]; + let logger: Logger; + let commandInfo: CommandInfo; + let commandOptionsSchema: z.ZodTypeAny; + let confirmationPromptStub: sinon.SinonStub; + let loggerLogSpy: sinon.SinonSpy; + + const successResponse = { + value: '{"IsArchive":true,"TotalFileCount":1,"CreatedUtcDateTime":"2026-04-30T16:34:57.3834786Z","LastStartedUtcDateTime":"0001-01-01T00:00:00","FolderArchiveStatus":"Unknown","ProcessedFileCount":0,"SuccessCount":0,"FailureCount":0,"NotArchivableFileCount":0,"ProgressPercentage":0.0}' + }; + + before(() => { + sinon.stub(auth, 'restoreAuth').resolves(); + sinon.stub(telemetry, 'trackEvent').resolves(); + sinon.stub(pid, 'getProcessName').returns(''); + sinon.stub(session, 'getId').returns(''); + + auth.connection.active = true; + auth.connection.spoUrl = 'https://contoso.sharepoint.com'; + commandInfo = cli.getCommandInfo(command); + commandOptionsSchema = commandInfo.command.getSchemaToParse()!; + }); + + beforeEach(() => { + log = []; + logger = { + log: async (msg: string) => { + log.push(msg); + }, + logRaw: async (msg: string) => { + log.push(msg); + }, + logToStderr: async (msg: string) => { + log.push(msg); + } + }; + loggerLogSpy = sinon.spy(logger, 'log'); + confirmationPromptStub = sinon.stub(cli, 'promptForConfirmation').resolves(false); + }); + + afterEach(() => { + sinonUtil.restore([ + request.get, + request.post, + cli.promptForConfirmation + ]); + }); + + after(() => { + sinon.restore(); + auth.connection.active = false; + auth.connection.spoUrl = undefined; + }); + + it('has correct name', () => { + assert.strictEqual(command.name, commands.FOLDER_ARCHIVE); + }); + + it('has a description', () => { + assert.notStrictEqual(command.description, null); + }); + + it('excludes options from URL processing', () => { + assert.deepStrictEqual((command as any).getExcludedOptionsWithUrls(), ['url']); + }); + + it('fails validation if webUrl is not a valid SharePoint URL', async () => { + const actual = commandOptionsSchema.safeParse({ + webUrl: 'invalid-url', + id: '00000000-0000-0000-0000-000000000000', + force: true + }); + assert.strictEqual(actual.success, false); + }); + + it('fails validation if both url and id are specified', async () => { + const actual = commandOptionsSchema.safeParse({ + webUrl: 'https://contoso.sharepoint.com', + url: '/sites/test/Shared documents/folder', + id: '00000000-0000-0000-0000-000000000000', + force: true + }); + assert.strictEqual(actual.success, false); + }); + + it('fails validation if neither url nor id are specified', async () => { + const actual = commandOptionsSchema.safeParse({ + webUrl: 'https://contoso.sharepoint.com', + force: true + }); + assert.strictEqual(actual.success, false); + }); + + it('fails validation if the id option is not a valid GUID', async () => { + const actual = commandOptionsSchema.safeParse({ + webUrl: 'https://contoso.sharepoint.com', + id: 'invalid-guid', + force: true + }); + assert.strictEqual(actual.success, false); + }); + + it('passes validation with valid options (url)', async () => { + const actual = commandOptionsSchema.safeParse({ + webUrl: 'https://contoso.sharepoint.com', + url: '/sites/test/Shared documents/folder', + force: true + }); + assert.strictEqual(actual.success, true); + }); + + it('passes validation with valid options (id)', async () => { + const actual = commandOptionsSchema.safeParse({ + webUrl: 'https://contoso.sharepoint.com', + id: '00000000-0000-0000-0000-000000000000', + force: true + }); + assert.strictEqual(actual.success, true); + }); + + it('prompts before archiving folder when confirmation argument not passed', async () => { + sinon.stub(request, 'get').resolves({ Exists: true, ListItemAllFields: { Id: 1, ParentList: { Id: 'b2307a39-e878-458b-bc90-03bc578531d6' } } }); + sinon.stub(request, 'post').resolves(); + + await command.action(logger, { + options: { + webUrl: 'https://contoso.sharepoint.com', + id: '00000000-0000-0000-0000-000000000000' + } + }); + assert(confirmationPromptStub.calledOnce); + }); + + it('aborts archiving folder when prompt not confirmed', async () => { + const getStub = sinon.stub(request, 'get').resolves({ Exists: true, ListItemAllFields: { Id: 1, ParentList: { Id: 'b2307a39-e878-458b-bc90-03bc578531d6' } } }); + const postStub = sinon.stub(request, 'post').resolves(); + + await command.action(logger, { + options: { + webUrl: 'https://contoso.sharepoint.com', + url: '/sites/test/Shared documents/folder' + } + }); + + assert(getStub.notCalled); + assert(postStub.notCalled); + }); + + it('archives folder by url', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/web/GetFolderByServerRelativePath(DecodedUrl='${formatting.encodeQueryParameter('/sites/test/Shared documents/folder')}')?$select=Exists,ListItemAllFields/Id,ListItemAllFields/ParentList/Id&$expand=ListItemAllFields,ListItemAllFields/ParentList`) { + return { + Exists: true, + ListItemAllFields: { + Id: 1, + ParentList: { + Id: 'b2307a39-e878-458b-bc90-03bc578531d6' + } + } + }; + } + + throw 'Invalid request'; + }); + + const postStub = sinon.stub(request, 'post').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/Lists(guid'b2307a39-e878-458b-bc90-03bc578531d6')/items(1)/Archive`) { + return successResponse; + } + + throw 'Invalid request'; + }); + + await command.action(logger, { + options: { + webUrl: 'https://contoso.sharepoint.com/sites/test', + url: '/sites/test/Shared documents/folder', + force: true + } + }); + + assert(postStub.calledOnce); + }); + + it('archives folder by id', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/web/GetFolderById('${formatting.encodeQueryParameter('00000000-0000-0000-0000-000000000000')}')?$select=Exists,ListItemAllFields/Id,ListItemAllFields/ParentList/Id&$expand=ListItemAllFields,ListItemAllFields/ParentList`) { + return { + Exists: true, + ListItemAllFields: { + Id: 1, + ParentList: { + Id: 'b2307a39-e878-458b-bc90-03bc578531d6' + } + } + }; + } + + throw 'Invalid request'; + }); + + const postStub = sinon.stub(request, 'post').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/Lists(guid'b2307a39-e878-458b-bc90-03bc578531d6')/items(1)/Archive`) { + return successResponse; + } + + throw 'Invalid request'; + }); + + await command.action(logger, { + options: { + webUrl: 'https://contoso.sharepoint.com/sites/test', + id: '00000000-0000-0000-0000-000000000000', + verbose: true, + force: true + } + }); + + assert(postStub.calledOnce); + }); + + it('archives folder using site-relative url', async () => { + sinon.stub(request, 'get').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/web/GetFolderByServerRelativePath(DecodedUrl='${formatting.encodeQueryParameter('/sites/test/Shared Documents/folder')}')?$select=Exists,ListItemAllFields/Id,ListItemAllFields/ParentList/Id&$expand=ListItemAllFields,ListItemAllFields/ParentList`) { + return { + Exists: true, + ListItemAllFields: { + Id: 1, + ParentList: { + Id: 'b2307a39-e878-458b-bc90-03bc578531d6' + } + } + }; + } + + throw 'Invalid request'; + }); + + const postStub = sinon.stub(request, 'post').callsFake(async (opts) => { + if (opts.url === `https://contoso.sharepoint.com/sites/test/_api/Lists(guid'b2307a39-e878-458b-bc90-03bc578531d6')/items(1)/Archive`) { + return successResponse; + } + + throw 'Invalid request'; + }); + + await command.action(logger, { + options: { + webUrl: 'https://contoso.sharepoint.com/sites/test', + url: '/Shared Documents/folder', + force: true + } + }); + + assert(postStub.calledOnce); + }); + + it('outputs no result when archiving a folder', async () => { + sinon.stub(request, 'get').resolves({ Exists: true, ListItemAllFields: { Id: 1, ParentList: { Id: 'b2307a39-e878-458b-bc90-03bc578531d6' } } }); + sinon.stub(request, 'post').resolves(); + + await command.action(logger, { + options: { + webUrl: 'https://contoso.sharepoint.com/sites/test', + url: '/sites/test/Shared documents/folder', + force: true + } + }); + + assert(loggerLogSpy.notCalled); + }); + + it('throws an error when trying to archive the root folder of a document library by url', async () => { + sinon.stub(request, 'get').resolves({ Exists: true, ListItemAllFields: {} }); + + await assert.rejects(command.action(logger, { + options: { + webUrl: 'https://contoso.sharepoint.com/sites/test', + url: '/Shared Documents', + force: true + } + }), new CommandError(`The folder '/Shared Documents' is the root folder of a document library and cannot be archived. Archive a subfolder instead.`)); + }); + + it('throws an error when trying to archive the root folder of a document library by id', async () => { + sinon.stub(request, 'get').resolves({ Exists: true, ListItemAllFields: {} }); + + await assert.rejects(command.action(logger, { + options: { + webUrl: 'https://contoso.sharepoint.com/sites/test', + id: '00000000-0000-0000-0000-000000000000', + force: true + } + }), new CommandError(`The folder '00000000-0000-0000-0000-000000000000' is the root folder of a document library and cannot be archived. Archive a subfolder instead.`)); + }); + + it('throws an error when the folder does not exist by url', async () => { + sinon.stub(request, 'get').resolves({}); + + await assert.rejects(command.action(logger, { + options: { + webUrl: 'https://contoso.sharepoint.com/sites/test', + url: '/Shared Documents/temp1', + force: true + } + }), new CommandError(`The folder '/Shared Documents/temp1' does not exist.`)); + }); + + it('throws an error when the folder does not exist by id', async () => { + sinon.stub(request, 'get').resolves({}); + + await assert.rejects(command.action(logger, { + options: { + webUrl: 'https://contoso.sharepoint.com/sites/test', + id: '00000000-0000-0000-0000-000000000000', + force: true + } + }), new CommandError(`The folder '00000000-0000-0000-0000-000000000000' does not exist.`)); + }); + + it('handles error correctly', async () => { + const error = { + error: { + 'odata.error': { + code: "-2130575338, Microsoft.SharePoint.SPException", + message: { + lang: "en-US", + value: 'The folder /sites/test/Shared documents/folder does not exist.' + } + } + } + }; + + sinon.stub(request, 'get').rejects(error); + + await assert.rejects(command.action(logger, { + options: { + webUrl: 'https://contoso.sharepoint.com/sites/test', + url: '/sites/test/Shared documents/folder', + force: true + } + }), new CommandError(error.error['odata.error'].message.value)); + }); +}); diff --git a/src/m365/spo/commands/folder/folder-archive.ts b/src/m365/spo/commands/folder/folder-archive.ts new file mode 100644 index 00000000000..7dabcc64b6f --- /dev/null +++ b/src/m365/spo/commands/folder/folder-archive.ts @@ -0,0 +1,112 @@ +import commands from '../../commands.js'; +import { Logger } from '../../../../cli/Logger.js'; +import SpoCommand from '../../../base/SpoCommand.js'; +import { globalOptionsZod } from '../../../../Command.js'; +import { z } from 'zod'; +import { validation } from '../../../../utils/validation.js'; +import { cli } from '../../../../cli/cli.js'; +import { urlUtil } from '../../../../utils/urlUtil.js'; +import request, { CliRequestOptions } from '../../../../request.js'; +import { formatting } from '../../../../utils/formatting.js'; + +export const options = z.strictObject({ + ...globalOptionsZod.shape, + webUrl: z.string() + .refine(url => validation.isValidSharePointUrl(url) === true, { + error: e => `'${e.input}' is not a valid SharePoint Online site URL.` + }) + .alias('u'), + url: z.string().optional(), + id: z.uuid().optional().alias('i'), + force: z.boolean().optional().alias('f') +}); + +declare type Options = z.infer; + +interface CommandArgs { + options: Options; +} + +class SpoFolderArchiveCommand extends SpoCommand { + public get name(): string { + return commands.FOLDER_ARCHIVE; + } + + public get description(): string { + return 'Archives a folder'; + } + + public get schema(): z.ZodType | undefined { + return options; + } + + public getRefinedSchema(schema: typeof options): z.ZodObject | undefined { + return schema + .refine(options => [options.url, options.id].filter(o => o !== undefined).length === 1, { + error: `Specify 'url' or 'id', but not both.` + }); + } + + protected getExcludedOptionsWithUrls(): string[] | undefined { + return ['url']; + } + + public async commandAction(logger: Logger, args: CommandArgs): Promise { + const { webUrl, url, id, force } = args.options; + + if (!force) { + const result = await cli.promptForConfirmation({ message: `Are you sure you would like to archive this item? You will be able to reactivate it instantly for the first 7 days. After that, it will take up to 24 hours to reactivate.` }); + if (!result) { + return; + } + } + + try { + if (this.verbose) { + await logger.logToStderr(`Archiving folder ${url || id} at site ${webUrl}...`); + } + + let requestUrl: string = `${webUrl}/_api/web`; + + if (id) { + requestUrl += `/GetFolderById('${formatting.encodeQueryParameter(id)}')`; + } + else if (url) { + const serverRelativePath = urlUtil.getServerRelativePath(webUrl, url); + requestUrl += `/GetFolderByServerRelativePath(DecodedUrl='${formatting.encodeQueryParameter(serverRelativePath)}')`; + } + requestUrl += '?$select=Exists,ListItemAllFields/Id,ListItemAllFields/ParentList/Id&$expand=ListItemAllFields,ListItemAllFields/ParentList'; + + const folderInfo = await request.get<{ Exists?: boolean; ListItemAllFields?: { Id: number; ParentList: { Id: string } } }>({ + url: requestUrl, + headers: { + accept: 'application/json;odata=nometadata' + }, + responseType: 'json' + }); + + if (!folderInfo.Exists) { + throw `The folder '${url || id}' does not exist.`; + } + + if (!folderInfo.ListItemAllFields?.ParentList) { + throw `The folder '${url || id}' is the root folder of a document library and cannot be archived. Archive a subfolder instead.`; + } + + const requestOptions: CliRequestOptions = { + url: `${webUrl}/_api/Lists(guid'${folderInfo.ListItemAllFields.ParentList.Id}')/items(${folderInfo.ListItemAllFields.Id})/Archive`, + headers: { + accept: 'application/json;odata=nometadata' + }, + responseType: 'json' + }; + + await request.post(requestOptions); + } + catch (err: any) { + this.handleRejectedODataJsonPromise(err); + } + } +} + +export default new SpoFolderArchiveCommand(); \ No newline at end of file