From d6c563dceca933c8b79b3295c5492c2063a0650e Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Sun, 11 Jun 2023 20:53:47 +0300 Subject: [PATCH] Feature: add organizational sharing link cmdlets for files and folder --- .../Add-PnPFileOrganizationalSharingLink.md | 89 +++++++++++++++++++ .../Add-PnPFolderOrganizationalSharingLink.md | 89 +++++++++++++++++++ .../FileOrganizationalSharingLinkResult.cs | 14 +++ .../FolderOrganizationalSharingLinkResult.cs | 6 ++ .../AddFileOrganizationalSharingLink.cs | 52 +++++++++++ .../AddFolderOrganizationalSharingLink.cs | 44 +++++++++ 6 files changed, 294 insertions(+) create mode 100644 documentation/Add-PnPFileOrganizationalSharingLink.md create mode 100644 documentation/Add-PnPFolderOrganizationalSharingLink.md create mode 100644 src/Commands/Model/SharePoint/FileOrganizationalSharingLinkResult.cs create mode 100644 src/Commands/Model/SharePoint/FolderOrganizationalSharingLinkResult.cs create mode 100644 src/Commands/Security/AddFileOrganizationalSharingLink.cs create mode 100644 src/Commands/Security/AddFolderOrganizationalSharingLink.cs diff --git a/documentation/Add-PnPFileOrganizationalSharingLink.md b/documentation/Add-PnPFileOrganizationalSharingLink.md new file mode 100644 index 000000000..097f28d95 --- /dev/null +++ b/documentation/Add-PnPFileOrganizationalSharingLink.md @@ -0,0 +1,89 @@ +--- +Module Name: PnP.PowerShell +schema: 2.0.0 +applicable: SharePoint Online +online version: https://pnp.github.io/powershell/cmdlets/Add-PnPFileOrganizationalSharingLink.html +external help file: PnP.PowerShell.dll-Help.xml +title: Add-PnPFileOrganizationalSharingLink +--- + +# Add-PnPFileOrganizationalSharingLink + +## SYNOPSIS +Creates an organizational sharing link for a file. + +## SYNTAX + +```powershell +Add-PnPFileOrganizationalSharingLink -FileUrl -Type [-Connection ] +``` + +## DESCRIPTION + +Creates a new organization sharing link for a file. + +## EXAMPLES + +### EXAMPLE 1 +```powershell +Add-PnPFileOrganizationalSharingLink -FileUrl "/sites/demo/Shared Documents/Test.docx" +``` + +This will create an organization sharing link for `Test.docx` file in the `Shared Documents` library which will be viewable by users in the organization. + +### EXAMPLE 2 +```powershell +Add-PnPFileOrganizationalSharingLink -FileUrl "/sites/demo/Shared Documents/Test.docx" -Type Edit +``` + +This will create an organization sharing link for `Test.docx` file in the `Shared Documents` library which will be editable by users in the organization. + +## PARAMETERS + +### -Connection +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. + +```yaml +Type: PnPConnection +Parameter Sets: (All) + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -FileUrl +The file in the site + +```yaml +Type: String +Parameter Sets: (All) + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -ShareType +The type of sharing that you want to, i.e do you want to enable people in your organization to view the shared content or also edit the content? + +Supported values are `View` and `Edit` + +```yaml +Type: PnP.Core.Model.Security.ShareType +Parameter Sets: (All) + +Required: False +Position: Named +Default value: View +Accept pipeline input: False +Accept wildcard characters: False +``` + +## RELATED LINKS + +[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) diff --git a/documentation/Add-PnPFolderOrganizationalSharingLink.md b/documentation/Add-PnPFolderOrganizationalSharingLink.md new file mode 100644 index 000000000..80cd3b18f --- /dev/null +++ b/documentation/Add-PnPFolderOrganizationalSharingLink.md @@ -0,0 +1,89 @@ +--- +Module Name: PnP.PowerShell +schema: 2.0.0 +applicable: SharePoint Online +online version: https://pnp.github.io/powershell/cmdlets/Add-PnPFolderOrganizationalSharingLink.html +external help file: PnP.PowerShell.dll-Help.xml +title: Add-PnPFolderOrganizationalSharingLink +--- + +# Add-PnPFolderOrganizationalSharingLink + +## SYNOPSIS +Creates an organizational sharing link for a folder. + +## SYNTAX + +```powershell +Add-PnPFolderOrganizationalSharingLink -Folder -Type [-Connection ] +``` + +## DESCRIPTION + +Creates a new organization sharing link for a folder. + +## EXAMPLES + +### EXAMPLE 1 +```powershell +Add-PnPFolderOrganizationalSharingLink -Folder "/sites/demo/Shared Documents/Test" +``` + +This will create an organization sharing link for `Test` folder in the `Shared Documents` library which will be viewable by users in the organization. + +### EXAMPLE 2 +```powershell +Add-PnPFolderOrganizationalSharingLink -Folder "/sites/demo/Shared Documents/Test" -Type Edit +``` + +This will create an organization sharing link for `Test` folder in the `Shared Documents` library which will be editable by users in the organization. + +## PARAMETERS + +### -Connection +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. + +```yaml +Type: PnPConnection +Parameter Sets: (All) + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -Folder +The folder in the site + +```yaml +Type: FolderPipeBind +Parameter Sets: (All) + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -ShareType +The type of sharing that you want to, i.e do you want to enable people in your organization to view the shared content or also edit the content? + +Supported values are `View` and `Edit` + +```yaml +Type: PnP.Core.Model.Security.ShareType +Parameter Sets: (All) + +Required: False +Position: Named +Default value: View +Accept pipeline input: False +Accept wildcard characters: False +``` + +## RELATED LINKS + +[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp) diff --git a/src/Commands/Model/SharePoint/FileOrganizationalSharingLinkResult.cs b/src/Commands/Model/SharePoint/FileOrganizationalSharingLinkResult.cs new file mode 100644 index 000000000..43a810208 --- /dev/null +++ b/src/Commands/Model/SharePoint/FileOrganizationalSharingLinkResult.cs @@ -0,0 +1,14 @@ +using PnP.Core.Model.Security; +using System.Collections.Generic; + +namespace PnP.PowerShell.Commands.Model.SharePoint +{ + public class FileOrganizationalSharingLinkResult + { + public string Id; + + public ISharingLink Link; + + public List Roles; + } +} diff --git a/src/Commands/Model/SharePoint/FolderOrganizationalSharingLinkResult.cs b/src/Commands/Model/SharePoint/FolderOrganizationalSharingLinkResult.cs new file mode 100644 index 000000000..21e3b315c --- /dev/null +++ b/src/Commands/Model/SharePoint/FolderOrganizationalSharingLinkResult.cs @@ -0,0 +1,6 @@ +namespace PnP.PowerShell.Commands.Model.SharePoint +{ + public class FolderOrganizationalSharingLinkResult : FileOrganizationalSharingLinkResult + { + } +} diff --git a/src/Commands/Security/AddFileOrganizationalSharingLink.cs b/src/Commands/Security/AddFileOrganizationalSharingLink.cs new file mode 100644 index 000000000..c969331fd --- /dev/null +++ b/src/Commands/Security/AddFileOrganizationalSharingLink.cs @@ -0,0 +1,52 @@ +using PnP.Core.Model.Security; +using PnP.Framework.Utilities; +using PnP.PowerShell.Commands.Model.SharePoint; +using System.Management.Automation; + +namespace PnP.PowerShell.Commands.Security +{ + [Cmdlet(VerbsCommon.Add, "PnPFileOrganizationalSharingLink")] + public class AddFileOrganizationalSharingLink : PnPWebCmdlet + { + [Parameter(Mandatory = true)] + public string FileUrl; + + [Parameter(Mandatory = false)] + public ShareType ShareType = ShareType.View; + + protected override void ExecuteCmdlet() + { + var serverRelativeUrl = string.Empty; + var ctx = Connection.PnPContext; + + ctx.Web.EnsureProperties(w => w.ServerRelativeUrl); + + if (!FileUrl.ToLower().StartsWith(ctx.Web.ServerRelativeUrl.ToLower())) + { + serverRelativeUrl = UrlUtility.Combine(ctx.Web.ServerRelativeUrl, FileUrl); + } + else + { + serverRelativeUrl = FileUrl; + } + + var file = ctx.Web.GetFileByServerRelativeUrl(serverRelativeUrl); + + var shareLinkRequestOptions = new OrganizationalLinkOptions() + { + Type = ShareType + }; + + var share = file.CreateOrganizationalSharingLink(shareLinkRequestOptions); + + FileOrganizationalSharingLinkResult fileOrganizationalSharingLinkResult = new() + { + Id = share.Id, + Link = share.Link, + Roles = share.Roles, + }; + + WriteObject(fileOrganizationalSharingLinkResult); + } + } +} diff --git a/src/Commands/Security/AddFolderOrganizationalSharingLink.cs b/src/Commands/Security/AddFolderOrganizationalSharingLink.cs new file mode 100644 index 000000000..1969aba7c --- /dev/null +++ b/src/Commands/Security/AddFolderOrganizationalSharingLink.cs @@ -0,0 +1,44 @@ +using PnP.Core.Model.Security; +using PnP.Core.Model.SharePoint; +using PnP.PowerShell.Commands.Base.PipeBinds; +using PnP.PowerShell.Commands.Model.SharePoint; +using System.Management.Automation; + +namespace PnP.PowerShell.Commands.Security +{ + [Cmdlet(VerbsCommon.Add, "PnPFolderOrganizationalSharingLink")] + public class AddFolderOrganizationalSharingLink : PnPWebCmdlet + { + [Parameter(Mandatory = true)] + public FolderPipeBind Folder; + + [Parameter(Mandatory = false)] + public ShareType ShareType = ShareType.View; + + protected override void ExecuteCmdlet() + { + var serverRelativeUrl = string.Empty; + var ctx = Connection.PnPContext; + + ctx.Web.EnsureProperties(w => w.ServerRelativeUrl); + + IFolder folder = Folder.GetFolder(ctx); + + var shareLinkRequestOptions = new OrganizationalLinkOptions() + { + Type = ShareType + }; + + var share = folder.CreateOrganizationalSharingLink(shareLinkRequestOptions); + + FolderOrganizationalSharingLinkResult folderOrganizationalSharingLinkResult = new() + { + Id = share.Id, + Link = share.Link, + Roles = share.Roles, + }; + + WriteObject(folderOrganizationalSharingLinkResult); + } + } +}