|
| 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 | +} |
0 commit comments