Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #2949 : added cmdlets to retrieve shared links for files and folders #3181

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions documentation/Get-PnPFileSharingLink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
Module Name: PnP.PowerShell
schema: 2.0.0
applicable: SharePoint Online
online version: https://pnp.github.io/powershell/cmdlets/Get-PnPFileSharingLink.html
external help file: PnP.PowerShell.dll-Help.xml
title: Get-PnPFileSharingLink
---

# Get-PnPFileSharingLink

## SYNOPSIS
Retrieves sharing links to associated with the file.

## SYNTAX

```powershell
Get-PnPFileSharingLink -FileUrl <String> [-Connection <PnPConnection>]
```

## DESCRIPTION

Retrieves sharing links for a file.

## EXAMPLES

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

This will fetch sharing links for `Test.docx` file in the `Shared Documents` library.

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

## RELATED LINKS

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

# Get-PnPFolderSharingLink

## SYNOPSIS
Retrieves sharing links to associated with the folder.

## SYNTAX

```powershell
Get-PnPFolderSharingLink -Folder <FolderPipeBind> [-Connection <PnPConnection>]
```

## DESCRIPTION

Retrieves sharing links for a folder.

## EXAMPLES

### EXAMPLE 1
```powershell
Get-PnPFolderSharingLink -Folder "/sites/demo/Shared Documents/Test"
```

This will fetch sharing links for `Test` folder in the `Shared Documents` library.

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

## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
35 changes: 35 additions & 0 deletions src/Commands/Security/GetFileSharingLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using PnP.Framework.Utilities;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.Security
{
[Cmdlet(VerbsCommon.Get, "PnPFileSharingLink")]
public class GetFileSharingLink : PnPWebCmdlet
{
[Parameter(Mandatory = true)]
public string FileUrl;

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 sharingLinks = file.GetShareLinks();

WriteObject(sharingLinks?.RequestedItems, true);
}
}
}
27 changes: 27 additions & 0 deletions src/Commands/Security/GetFolderSharingLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using PnP.Core.Model.SharePoint;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.Security
{
[Cmdlet(VerbsCommon.Get, "PnPFolderSharingLink")]
public class GetFolderSharingLink : PnPWebCmdlet
{
[Parameter(Mandatory = true)]
public FolderPipeBind Folder;

protected override void ExecuteCmdlet()
{
var serverRelativeUrl = string.Empty;
var ctx = Connection.PnPContext;

ctx.Web.EnsureProperties(w => w.ServerRelativeUrl);

IFolder folder = Folder.GetFolder(ctx);

var sharingLinks = folder.GetShareLinks();

WriteObject(sharingLinks?.RequestedItems, true);
}
}
}