Skip to content

Commit

Permalink
Merge pull request #3178 from gautamdsheth/feature/create-user-sharin…
Browse files Browse the repository at this point in the history
…g-link

Feature: added cmdlets to share files/folders with specified users
  • Loading branch information
gautamdsheth authored Jun 11, 2023
2 parents 267f017 + d23ef6d commit 9ba9048
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 4 deletions.
103 changes: 103 additions & 0 deletions documentation/Add-PnPFileUserSharingLink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
Module Name: PnP.PowerShell
schema: 2.0.0
applicable: SharePoint Online
online version: https://pnp.github.io/powershell/cmdlets/Add-PnPFileUserSharingLink.html
external help file: PnP.PowerShell.dll-Help.xml
title: Add-PnPFileUserSharingLink
---

# Add-PnPFileUserSharingLink

## SYNOPSIS
Creates a sharing link to share a file with a list of specified users.

## SYNTAX

```powershell
Add-PnPFileUserSharingLink -FileUrl <String> -Type <PnP.Core.Model.Security.ShareType> -Users <String[]> [-Connection <PnPConnection>]
```

## DESCRIPTION

Creates a new user sharing link for a file.

## EXAMPLES

### EXAMPLE 1
```powershell
Add-PnPFileUserSharingLink -FileUrl "/sites/demo/Shared Documents/Test.docx" -Users "john@contoso.onmicrosoft.com","jane@contoso.onmicrosoft.com"
```

This will create an user sharing link for `Test.docx` file in the `Shared Documents` library which will be viewable to specified users in the organization.

### EXAMPLE 2
```powershell
Add-PnPFileUserSharingLink -FileUrl "/sites/demo/Shared Documents/Test.docx" -Type Edit -Users "john@contoso.onmicrosoft.com","jane@contoso.onmicrosoft.com"
```

This will create an user sharing link for `Test.docx` file in the `Shared Documents` library which will be editable by specified 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
```

### -Users
The UPN(s) of the user(s) to with whom you would like to share the file.

```yaml
Type: String[]
Parameter Sets: (All)
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
103 changes: 103 additions & 0 deletions documentation/Add-PnPFolderUserSharingLink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
Module Name: PnP.PowerShell
schema: 2.0.0
applicable: SharePoint Online
online version: https://pnp.github.io/powershell/cmdlets/Add-PnPFolderUserSharingLink.html
external help file: PnP.PowerShell.dll-Help.xml
title: Add-PnPFolderUserSharingLink
---

# Add-PnPFolderUserSharingLink

## SYNOPSIS
Creates a sharing link to share a folder with a list of specified users.

## SYNTAX

```powershell
Add-PnPFolderUserSharingLink -Folder <FolderPipeBind> -Type <PnP.Core.Model.Security.ShareType> -Users <String[]> [-Connection <PnPConnection>]
```

## DESCRIPTION

Creates a new user sharing link for a folder.

## EXAMPLES

### EXAMPLE 1
```powershell
Add-PnPFolderUserSharingLink -Folder "/sites/demo/Shared Documents/Test" -Users "john@contoso.onmicrosoft.com","jane@contoso.onmicrosoft.com"
```

This will create an user sharing link for `Test` folder in the `Shared Documents` library which will be viewable to specified users in the organization.

### EXAMPLE 2
```powershell
Add-PnPFolderUserSharingLink -Folder "/sites/demo/Shared Documents/Test" -Type Edit -Users "john@contoso.onmicrosoft.com","jane@contoso.onmicrosoft.com"
```

This will create an user sharing link for `Test` folder in the `Shared Documents` library which will be editable by specified 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
```

### -Users
The UPN(s) of the user(s) to with whom you would like to share the folder.

```yaml
Type: String[]
Parameter Sets: (All)
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

namespace PnP.PowerShell.Commands.Model.SharePoint
{
public class FileOrganizationalSharingLinkResult
public class FileSharingLinkResult
{
public string Id;

public ISharingLink Link;

public List<PermissionRole> Roles;

public string WebUrl;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace PnP.PowerShell.Commands.Model.SharePoint
{
public class FolderOrganizationalSharingLinkResult : FileOrganizationalSharingLinkResult
public class FolderSharingLinkResult : FileSharingLinkResult
{
}
}
4 changes: 3 additions & 1 deletion src/Commands/Security/AddFileOrganizationalSharingLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace PnP.PowerShell.Commands.Security
{
[Cmdlet(VerbsCommon.Add, "PnPFileOrganizationalSharingLink")]
[OutputType(typeof(FileSharingLinkResult))]
public class AddFileOrganizationalSharingLink : PnPWebCmdlet
{
[Parameter(Mandatory = true)]
Expand Down Expand Up @@ -39,11 +40,12 @@ protected override void ExecuteCmdlet()

var share = file.CreateOrganizationalSharingLink(shareLinkRequestOptions);

FileOrganizationalSharingLinkResult fileOrganizationalSharingLinkResult = new()
FileSharingLinkResult fileOrganizationalSharingLinkResult = new()
{
Id = share.Id,
Link = share.Link,
Roles = share.Roles,
WebUrl = share.Link?.WebUrl
};

WriteObject(fileOrganizationalSharingLinkResult);
Expand Down
68 changes: 68 additions & 0 deletions src/Commands/Security/AddFileUserSharingLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using PnP.Core.Model.Security;
using PnP.Framework.Utilities;
using PnP.PowerShell.Commands.Model.SharePoint;
using System.Collections.Generic;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.Security
{
[Cmdlet(VerbsCommon.Add, "PnPFileUserSharingLink")]
[OutputType(typeof(FileSharingLinkResult))]
public class AddPnPFileUserSharingLink : PnPWebCmdlet
{
[Parameter(Mandatory = true)]
public string FileUrl;

[Parameter(Mandatory = true)]
public string[] Users;

[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);

// List of users to share the file/folder with
var driveRecipients = new List<IDriveRecipient>();
foreach(var user in Users)
{
var driveRecipient = UserLinkOptions.CreateDriveRecipient(user);
driveRecipients.Add(driveRecipient);
}

var shareLinkRequestOptions = new UserLinkOptions()
{
// Users can see and edit the file online, but not download it
Type = ShareType,
Recipients = driveRecipients
};

var share = file.CreateUserSharingLink(shareLinkRequestOptions);

FileSharingLinkResult fileUserSharingLinkResult = new()
{
Id = share.Id,
Link = share.Link,
Roles = share.Roles,
WebUrl = share.Link?.WebUrl
};

WriteObject(fileUserSharingLinkResult);
}
}
}
4 changes: 3 additions & 1 deletion src/Commands/Security/AddFolderOrganizationalSharingLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace PnP.PowerShell.Commands.Security
{
[Cmdlet(VerbsCommon.Add, "PnPFolderOrganizationalSharingLink")]
[OutputType(typeof(FolderSharingLinkResult))]
public class AddFolderOrganizationalSharingLink : PnPWebCmdlet
{
[Parameter(Mandatory = true)]
Expand All @@ -31,11 +32,12 @@ protected override void ExecuteCmdlet()

var share = folder.CreateOrganizationalSharingLink(shareLinkRequestOptions);

FolderOrganizationalSharingLinkResult folderOrganizationalSharingLinkResult = new()
FolderSharingLinkResult folderOrganizationalSharingLinkResult = new()
{
Id = share.Id,
Link = share.Link,
Roles = share.Roles,
WebUrl = share.Link?.WebUrl
};

WriteObject(folderOrganizationalSharingLinkResult);
Expand Down
Loading

0 comments on commit 9ba9048

Please sign in to comment.