Skip to content

Commit

Permalink
Add ability to bypass shared locks for spo file remove. Closes #6313
Browse files Browse the repository at this point in the history
  • Loading branch information
Saurabh7019 committed Oct 14, 2024
1 parent d92b1c2 commit 931484d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
17 changes: 13 additions & 4 deletions docs/docs/cmd/spo/file/file-remove.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ m365 spo page template remove
`--recycle`
: Recycle the file instead of actually deleting it.

`--bypassSharedLock`
: Remove the file even if it is locked for shared use.

`-f, --force`
: Don't prompt for confirming removing the file.
```
Expand All @@ -39,22 +42,28 @@ m365 spo page template remove

## Examples

Remove file by ID.
Remove a file by ID.

```sh
m365 spo file remove --webUrl https://contoso.sharepoint.com/sites/project-x --id 0cd891ef-afce-4e55-b836-fce03286cccf
```

Remove the file by site-relative URL.
Remove a file by site-relative URL.

```sh
m365 spo file remove --webUrl https://contoso.sharepoint.com/sites/project-x --url "/Shared Documents/Test.docx"
```

Move the file by server-relative URL to the recycle bin.
Remove a file by server-relative URL to the recycle bin.

```sh
m365 spo file remove --webUrl https://contoso.sharepoint.com/sites/project-x --url "/sites/project-x/Shared Documents/Test.docx" --recycle
```

Remove a file that is locked

```sh
m365 spo file remove --webUrl https://contoso.sharepoint.com/sites/project-x --url "/sites/project-x/SharedDocuments/Test.docx" --recycle
m365 spo file remove --webUrl https://contoso.sharepoint.com/sites/project-x --url "/Shared Documents/Test.docx" --bypassSharedLock
```

Remove a page template by site-relative URL.
Expand Down
17 changes: 17 additions & 0 deletions src/m365/spo/commands/file/file-remove.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,23 @@ describe(commands.FILE_REMOVE, () => {
assert(correctRequestIssued);
});

it('correctly bypasses a shared lock', async () => {
const postStub = sinon.stub(request, 'post').callsFake(async (opts) => {
if (opts.url === `https://contoso.sharepoint.com/_api/web/GetFileByServerRelativePath(DecodedUrl='%2F0cd891ef-afce-4e55-b836-fce03286cccf')`) {
if (opts.headers &&
opts.headers.accept &&
opts.headers.accept === 'application/json;odata=nometadata') {
return;
}
}

throw 'Invalid request';
});

await command.action(logger, { options: { webUrl: 'https://contoso.sharepoint.com', url: '0cd891ef-afce-4e55-b836-fce03286cccf', force: true, bypassSharedLock: true } });
assert.deepStrictEqual(postStub.firstCall.args[0].headers?.Prefer, 'bypass-shared-lock');
});

it('command correctly handles file remove reject request', async () => {
const err = 'An error has occurred';
const error = {
Expand Down
11 changes: 10 additions & 1 deletion src/m365/spo/commands/file/file-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Options extends GlobalOptions {
id?: string;
url?: string;
recycle?: boolean;
bypassSharedLock?: boolean;
force?: boolean;
}

Expand Down Expand Up @@ -49,6 +50,7 @@ class SpoFileRemoveCommand extends SpoCommand {
id: typeof args.options.id !== 'undefined',
url: typeof args.options.url !== 'undefined',
recycle: !!args.options.recycle,
bypassSharedLock: !!args.options.bypassSharedLock,
force: !!args.options.force
});
});
Expand All @@ -68,6 +70,9 @@ class SpoFileRemoveCommand extends SpoCommand {
{
option: '--recycle'
},
{
option: '--bypassSharedLock'
},
{
option: '-f, --force'
}
Expand Down Expand Up @@ -98,7 +103,7 @@ class SpoFileRemoveCommand extends SpoCommand {

#initTypes(): void {
this.types.string.push('webUrl', 'id', 'url');
this.types.boolean.push('recycle', 'force');
this.types.boolean.push('recycle', 'bypassSharedLock', 'force');
}

protected getExcludedOptionsWithUrls(): string[] | undefined {
Expand Down Expand Up @@ -136,6 +141,10 @@ class SpoFileRemoveCommand extends SpoCommand {
responseType: 'json'
};

if (args.options.bypassSharedLock) {
requestOptions.headers!.Prefer = 'bypass-shared-lock';
}

try {
await request.post(requestOptions);
}
Expand Down

0 comments on commit 931484d

Please sign in to comment.