-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3198 from gautamdsheth/feature/anon-sharing
Feature: added cmdlets for sharing files and folders anonymously
- Loading branch information
Showing
8 changed files
with
388 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.