Skip to content

Commit 21ebdff

Browse files
authored
Merge pull request #3200 from gautamdsheth/feature/remove-share-link
Feature: added cmdlets to remove sharing links from file and folder
2 parents 67f4071 + f5df79f commit 21ebdff

File tree

5 files changed

+360
-1
lines changed

5 files changed

+360
-1
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
Module Name: PnP.PowerShell
3+
schema: 2.0.0
4+
applicable: SharePoint Online
5+
online version: https://pnp.github.io/powershell/cmdlets/Remove-PnPFileSharingLink.html
6+
external help file: PnP.PowerShell.dll-Help.xml
7+
title: Remove-PnPFileSharingLink
8+
---
9+
10+
# Remove-PnPFileSharingLink
11+
12+
## SYNOPSIS
13+
Removes sharing links associated with a file.
14+
15+
## SYNTAX
16+
17+
```powershell
18+
Remove-PnPFileSharingLink -FileUrl <String> -Identity <String> -Force <SwitchParamter> [-Connection <PnPConnection>]
19+
```
20+
21+
## DESCRIPTION
22+
23+
Removes sharing links associated with a file.
24+
25+
## EXAMPLES
26+
27+
### EXAMPLE 1
28+
```powershell
29+
Remove-PnPFileSharingLink -FileUrl "/sites/demo/Shared Documents/Test.docx"
30+
```
31+
32+
This will delete all the sharing links associated with the `Test.docx` file in the `Shared Documents` document library.
33+
34+
### EXAMPLE 2
35+
```powershell
36+
Remove-PnPFileSharingLink -FileUrl "/sites/demo/Shared Documents/Test.docx" -Force
37+
```
38+
39+
This will delete all the sharing links associated with the `Test.docx` file in the `Shared Documents` document library. User will not be prompted for confirmation.
40+
41+
### EXAMPLE 3
42+
```powershell
43+
$sharingLinks = Get-PnPFileSharingLink -FileUrl "/sites/demo/Shared Documents/Test.docx"
44+
Remove-PnPFileSharingLink -FileUrl "/sites/demo/Shared Documents/Test.docx" -Identity $sharingLinks[0].Id -Force
45+
```
46+
47+
This will delete the first sharing link associated with the `Test.docx` file in the `Shared Documents` document library. User will not be prompted for confirmation.
48+
49+
## PARAMETERS
50+
51+
### -Connection
52+
Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection.
53+
54+
```yaml
55+
Type: PnPConnection
56+
Parameter Sets: (All)
57+
58+
Required: False
59+
Position: Named
60+
Default value: None
61+
Accept pipeline input: False
62+
Accept wildcard characters: False
63+
```
64+
65+
### -FileUrl
66+
The file in the site
67+
68+
```yaml
69+
Type: String
70+
Parameter Sets: (All)
71+
72+
Required: True
73+
Position: Named
74+
Default value: None
75+
Accept pipeline input: False
76+
Accept wildcard characters: False
77+
```
78+
79+
### -Identity
80+
The Id of the sharing link associated with the file.
81+
You can retrieve the identity using `Get-PnPFileSharingLink` cmdlet.
82+
83+
```yaml
84+
Type: Identity
85+
Parameter Sets: (All)
86+
87+
Required: False
88+
Position: Named
89+
Default value: None
90+
Accept pipeline input: False
91+
Accept wildcard characters: False
92+
```
93+
94+
### -Force
95+
If provided, no confirmation will be requested and the action will be performed
96+
97+
```yaml
98+
Type: String
99+
Parameter Sets: (All)
100+
101+
Required: False
102+
Position: Named
103+
Default value: None
104+
Accept pipeline input: False
105+
Accept wildcard characters: False
106+
```
107+
108+
## RELATED LINKS
109+
110+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
Module Name: PnP.PowerShell
3+
schema: 2.0.0
4+
applicable: SharePoint Online
5+
online version: https://pnp.github.io/powershell/cmdlets/Remove-PnPFolderSharingLink.html
6+
external help file: PnP.PowerShell.dll-Help.xml
7+
title: Remove-PnPFolderSharingLink
8+
---
9+
10+
# Remove-PnPFolderSharingLink
11+
12+
## SYNOPSIS
13+
Removes sharing links associated with a folder.
14+
15+
## SYNTAX
16+
17+
```powershell
18+
Remove-PnPFolderSharingLink -Folder <FolderPipeBind> -Identity <String> -Force <SwitchParamter> [-Connection <PnPConnection>]
19+
```
20+
21+
## DESCRIPTION
22+
23+
Removes sharing links associated with a folder.
24+
25+
## EXAMPLES
26+
27+
### EXAMPLE 1
28+
```powershell
29+
Remove-PnPFolderSharingLink -Folder "/sites/demo/Shared Documents/Test"
30+
```
31+
32+
This will delete all the sharing links associated with the `Test` folder in the `Shared Documents` document library.
33+
34+
### EXAMPLE 2
35+
```powershell
36+
Remove-PnPFolderSharingLink -Folder "/sites/demo/Shared Documents/Test" -Force
37+
```
38+
39+
This will delete all the sharing links associated with the `Test` folder in the `Shared Documents` document library. User will not be prompted for confirmation.
40+
41+
### EXAMPLE 3
42+
```powershell
43+
$sharingLinks = Get-PnPFolderSharingLink -Folder "/sites/demo/Shared Documents/Test"
44+
Remove-PnPFolderSharingLink -Folder "/sites/demo/Shared Documents/Test" -Identity $sharingLinks[0].Id -Force
45+
```
46+
47+
This will delete the first sharing link associated with the `Test` folder in the `Shared Documents` document library. User will not be prompted for confirmation.
48+
49+
## PARAMETERS
50+
51+
### -Connection
52+
Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection.
53+
54+
```yaml
55+
Type: PnPConnection
56+
Parameter Sets: (All)
57+
58+
Required: False
59+
Position: Named
60+
Default value: None
61+
Accept pipeline input: False
62+
Accept wildcard characters: False
63+
```
64+
65+
### -Folder
66+
The folder in the site
67+
68+
```yaml
69+
Type: FolderPipeBind
70+
Parameter Sets: (All)
71+
72+
Required: True
73+
Position: Named
74+
Default value: None
75+
Accept pipeline input: False
76+
Accept wildcard characters: False
77+
```
78+
79+
### -Identity
80+
The Id of the sharing link associated with the folder.
81+
You can retrieve the identity using `Get-PnPFolderSharingLink` cmdlet.
82+
83+
```yaml
84+
Type: Identity
85+
Parameter Sets: (All)
86+
87+
Required: False
88+
Position: Named
89+
Default value: None
90+
Accept pipeline input: False
91+
Accept wildcard characters: False
92+
```
93+
94+
### -Force
95+
If provided, no confirmation will be requested and the action will be performed
96+
97+
```yaml
98+
Type: String
99+
Parameter Sets: (All)
100+
101+
Required: False
102+
Position: Named
103+
Default value: None
104+
Accept pipeline input: False
105+
Accept wildcard characters: False
106+
```
107+
108+
## RELATED LINKS
109+
110+
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

src/Commands/Apps/RemoveApplicationCustomizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected override void ExecuteCmdlet()
5858

5959
if (!actions.Any())
6060
{
61-
WriteVerbose($"No application customimzers representing the client side extension registration found within the scope '{Scope}'");
61+
WriteVerbose($"No application customizers representing the client side extension registration found within the scope '{Scope}'");
6262
return;
6363
}
6464

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using Microsoft.Office.SharePoint.Tools;
2+
using PnP.Framework.Utilities;
3+
using PnP.PowerShell.Commands.Properties;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Management.Automation;
8+
9+
namespace PnP.PowerShell.Commands.Security
10+
{
11+
[Cmdlet(VerbsCommon.Remove, "PnPFileSharingLink")]
12+
[OutputType(typeof(void))]
13+
public class RemoveFileSharingLink : PnPWebCmdlet
14+
{
15+
[Parameter(Mandatory = true)]
16+
public string FileUrl;
17+
18+
[Parameter(Mandatory = false)]
19+
public string Identity;
20+
21+
[Parameter(Mandatory = false)]
22+
public SwitchParameter Force;
23+
24+
protected override void ExecuteCmdlet()
25+
{
26+
var serverRelativeUrl = string.Empty;
27+
var ctx = Connection.PnPContext;
28+
29+
ctx.Web.EnsureProperties(w => w.ServerRelativeUrl);
30+
31+
if (!FileUrl.ToLower().StartsWith(ctx.Web.ServerRelativeUrl.ToLower()))
32+
{
33+
serverRelativeUrl = UrlUtility.Combine(ctx.Web.ServerRelativeUrl, FileUrl);
34+
}
35+
else
36+
{
37+
serverRelativeUrl = FileUrl;
38+
}
39+
40+
var file = ctx.Web.GetFileByServerRelativeUrl(serverRelativeUrl);
41+
42+
var sharingLinks = file.GetShareLinks();
43+
44+
if (sharingLinks?.RequestedItems != null && sharingLinks.Length > 0)
45+
{
46+
if (ParameterSpecified(nameof(Identity)) && !string.IsNullOrEmpty(Identity))
47+
{
48+
var link = sharingLinks.Where(s => s.Id == Identity).FirstOrDefault();
49+
if (link != null)
50+
{
51+
if (Force || ShouldContinue($"Remove Sharing Link with ID {Identity} ?", Resources.Confirm))
52+
{
53+
link.DeletePermission();
54+
}
55+
}
56+
else
57+
{
58+
throw new PSArgumentException($"Sharing link with ID {Identity} not found");
59+
}
60+
}
61+
else
62+
{
63+
if (Force || ShouldContinue($"Remove all sharing links associated with the file ?", Resources.Confirm))
64+
{
65+
file.DeleteShareLinks();
66+
}
67+
}
68+
}
69+
else
70+
{
71+
throw new PSArgumentException("No sharing links were found for the specified file");
72+
}
73+
}
74+
}
75+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Microsoft.Office.SharePoint.Tools;
2+
using PnP.PowerShell.Commands.Base.PipeBinds;
3+
using PnP.PowerShell.Commands.Properties;
4+
using System.Linq;
5+
using System.Management.Automation;
6+
7+
namespace PnP.PowerShell.Commands.Security
8+
{
9+
[Cmdlet(VerbsCommon.Remove, "PnPFolderSharingLink")]
10+
[OutputType(typeof(void))]
11+
public class RemoveFolderSharingLink : PnPWebCmdlet
12+
{
13+
[Parameter(Mandatory = true)]
14+
public FolderPipeBind Folder;
15+
16+
[Parameter(Mandatory = false)]
17+
public string Identity;
18+
19+
[Parameter(Mandatory = false)]
20+
public SwitchParameter Force;
21+
22+
protected override void ExecuteCmdlet()
23+
{
24+
var serverRelativeUrl = string.Empty;
25+
var ctx = Connection.PnPContext;
26+
27+
ctx.Web.EnsureProperties(w => w.ServerRelativeUrl);
28+
29+
var folder = Folder.GetFolder(ctx);
30+
31+
var sharingLinks = folder.GetShareLinks();
32+
33+
if (sharingLinks?.RequestedItems != null && sharingLinks.Length > 0)
34+
{
35+
if (ParameterSpecified(nameof(Identity)) && !string.IsNullOrEmpty(Identity))
36+
{
37+
var link = sharingLinks.Where(s => s.Id == Identity).FirstOrDefault();
38+
if (link != null)
39+
{
40+
if (Force || ShouldContinue($"Remove Sharing Link with ID {Identity} ?", Resources.Confirm))
41+
{
42+
link.DeletePermission();
43+
}
44+
}
45+
else
46+
{
47+
throw new PSArgumentException($"Sharing link with ID {Identity} not found");
48+
}
49+
}
50+
else
51+
{
52+
if (Force || ShouldContinue($"Remove all sharing links associated with the folder ?", Resources.Confirm))
53+
{
54+
folder.DeleteShareLinks();
55+
}
56+
}
57+
}
58+
else
59+
{
60+
throw new PSArgumentException("No sharing links were found for the specified folder");
61+
}
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)