Skip to content

Commit

Permalink
Merge pull request #3198 from gautamdsheth/feature/anon-sharing
Browse files Browse the repository at this point in the history
Feature: added cmdlets for sharing files and folders anonymously
  • Loading branch information
gautamdsheth authored Jun 16, 2023
2 parents 441103e + e7627a1 commit e2c9a3b
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 4 deletions.
124 changes: 124 additions & 0 deletions documentation/Add-PnPFileAnonymousSharingLink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
Module Name: PnP.PowerShell
schema: 2.0.0
applicable: SharePoint Online
online version: https://pnp.github.io/powershell/cmdlets/Add-PnPFileAnonymousSharingLink.html
external help file: PnP.PowerShell.dll-Help.xml
title: Add-PnPFileAnonymousSharingLink
---

# Add-PnPFileAnonymousSharingLink

## SYNOPSIS
Creates an anonymous sharing link to share a file.

## SYNTAX

```powershell
Add-PnPFileAnonymousSharingLink -FileUrl <String> -Type <PnP.Core.Model.Security.ShareType> -Password <String> -ExpirationDateTime <DateTime> [-Connection <PnPConnection>]
```

## DESCRIPTION

Creates an anonymous sharing link to share a file.

## EXAMPLES

### EXAMPLE 1
```powershell
Add-PnPFileAnonymousSharingLink -FileUrl "/sites/demo/Shared Documents/Test.docx"
```

This will create an anonymous sharing link for `Test.docx` file in the `Shared Documents` library which will be viewable.

### EXAMPLE 2
```powershell
Add-PnPFileAnonymousSharingLink -FileUrl "/sites/demo/Shared Documents/Test.docx" -Type Edit -Password "PnPRocks!"
```

This will create an anonymous sharing link for `Test.docx` file in the `Shared Documents` library which will be editable by anonymous users after specifying the password.

### EXAMPLE 3
```powershell
Add-PnPFileAnonymousSharingLink -FileUrl "/sites/demo/Shared Documents/Test.docx" -Type View -ExpirationDateTime (Get-Date).AddDays(15)
```

This will create an anonymous sharing link for `Test.docx` file in the `Shared Documents` library which will be viewable by anonymous users. The link will expire after 15 days.

## 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?
`CreateOnly` value is not supported.

```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
```

### -Password
The password to be set for the file to be shared.

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

### -ExpirationDateTime
The expiration date to be after which the file link will expire.

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

## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
2 changes: 1 addition & 1 deletion documentation/Add-PnPFileOrganizationalSharingLink.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ 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`
`CreateOnly` value is not supported.

```yaml
Type: PnP.Core.Model.Security.ShareType
Expand Down
2 changes: 1 addition & 1 deletion documentation/Add-PnPFileUserSharingLink.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ 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`
`CreateOnly` value is not supported.

```yaml
Type: PnP.Core.Model.Security.ShareType
Expand Down
124 changes: 124 additions & 0 deletions documentation/Add-PnPFolderAnonymousSharingLink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
Module Name: PnP.PowerShell
schema: 2.0.0
applicable: SharePoint Online
online version: https://pnp.github.io/powershell/cmdlets/Add-PnPFolderAnonymousSharingLink.html
external help file: PnP.PowerShell.dll-Help.xml
title: Add-PnPFolderAnonymousSharingLink
---

# Add-PnPFolderAnonymousSharingLink

## SYNOPSIS
Creates an anonymous sharing link to share a folder.

## SYNTAX

```powershell
Add-PnPFolderAnonymousSharingLink -Folder <FolderPipeBind> -Type <PnP.Core.Model.Security.ShareType> -Password <String> -ExpirationDateTime <DateTime> [-Connection <PnPConnection>]
```

## DESCRIPTION

Creates a new anonymous sharing link for a folder.

## EXAMPLES

### EXAMPLE 1
```powershell
Add-PnPFolderAnonymousSharingLink -Folder "/sites/demo/Shared Documents/Test"
```

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

### EXAMPLE 2
```powershell
Add-PnPFolderAnonymousSharingLink -Folder "/sites/demo/Shared Documents/Test" -Type Edit -Password "PnPRocks!"
```

This will create an anonymous sharing link for `Test` folder in the `Shared Documents` library which will be editable by anonymous users with the specified password.

### EXAMPLE 2
```powershell
Add-PnPFolderAnonymousSharingLink -Folder "/sites/demo/Shared Documents/Test" -Type Edit -Password "PnPRocks!" -ExpirationDateTime (Get-Date).AddDays(15)
```

This will create an anonymous sharing link for `Test` folder in the `Shared Documents` library which will be editable by anonymous users with the specified password. The link will expire after 15 days.

## 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 anonymous users to view the shared content or also edit the content?
`Review` and `BlocksDownload` values are not supported.

```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
```

### -Password
The password for the folder which will be shared anonymously.

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

### -ExpirationDateTime
The expiration date for the folder after which the shared link will stop working.

```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)
2 changes: 1 addition & 1 deletion documentation/Add-PnPFolderOrganizationalSharingLink.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ 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`
`Review` and `BlocksDownload` values are not supported.

```yaml
Type: PnP.Core.Model.Security.ShareType
Expand Down
2 changes: 1 addition & 1 deletion documentation/Add-PnPFolderUserSharingLink.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ 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`
`Review` and `BlocksDownload` values are not supported.

```yaml
Type: PnP.Core.Model.Security.ShareType
Expand Down
72 changes: 72 additions & 0 deletions src/Commands/Security/AddFileAnonymousSharingLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using PnP.Core.Model.Security;
using PnP.Framework.Utilities;
using PnP.PowerShell.Commands.Model.SharePoint;
using System;
using System.Management.Automation;

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

[Parameter(Mandatory = false)]
public ShareType ShareType = ShareType.View;

[Parameter(Mandatory = false)]
public string Password;

[Parameter(Mandatory = false)]
public DateTime ExpirationDateTime;

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 AnonymousLinkOptions()
{
Type = ShareType
};

if (ParameterSpecified(nameof(Password)))
{
shareLinkRequestOptions.Password = Password;
}

if (ParameterSpecified(nameof(ExpirationDateTime)))
{
shareLinkRequestOptions.ExpirationDateTime = ExpirationDateTime;
}

var sharedAnonymousFile = file.CreateAnonymousSharingLink(shareLinkRequestOptions);

FileSharingLinkResult fileAnonymousSharingLinkResult = new()
{
Id = sharedAnonymousFile.Id,
Link = sharedAnonymousFile.Link,
Roles = sharedAnonymousFile.Roles,
WebUrl = sharedAnonymousFile.Link?.WebUrl
};

WriteObject(fileAnonymousSharingLinkResult);

}
}
}
Loading

0 comments on commit e2c9a3b

Please sign in to comment.