Skip to content

Potential fix for bug #5010 #5024

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

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions documentation/Get-PnPFile.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Get-PnPFile -Url <String> -AsString [-Connection <PnPConnection>]
Get-PnPFile -Url <String> -AsMemoryStream [-Connection <PnPConnection>]
```

### Skip decoding of file name, for instance %20 used in file name
```powershell
Get-PnPFile -Url <String> -NoUrlDecode [-Connection <PnPConnection>]
```


## DESCRIPTION
Allows downloading of a file from SharePoint Online. The file contents can either be read directly into memory as text, directly saved to local disk or stored in memory for further processing.

Expand Down Expand Up @@ -100,6 +106,13 @@ Get-PnPFile -Url "/sites/templates/Shared Documents/HR Site.pnp" -AsMemoryStream

Retrieves the file in memory for further processing

### EXAMPLE 9
```powershell
Get-PnPFile -Url "Shared Documents/test%20file.docx" -NoUrlDecode
```

Retrieves the file without Url decoding for further processing

## PARAMETERS

### -AsFile
Expand Down Expand Up @@ -255,6 +268,19 @@ Accept pipeline input: True (ByValue)
Accept wildcard characters: False
```

### -NoUrlDecode

```yaml
Type: SwitchParameter
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)
10 changes: 8 additions & 2 deletions src/Commands/Files/GetFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class GetFile : PnPWebCmdlet
[Parameter(Mandatory = false, ParameterSetName = URLASMEMORYSTREAM)]
public SwitchParameter AsMemoryStream;

[Parameter(Mandatory = false)]
public SwitchParameter NoUrlDecode;

protected override void ExecuteCmdlet()
{
var serverRelativeUrl = string.Empty;
Expand All @@ -72,9 +75,12 @@ protected override void ExecuteCmdlet()
// We can't deal with absolute URLs
Url = UrlUtility.MakeRelativeUrl(Url);
}

// Remove URL decoding from the Url as that will not work. We will encode the + character specifically, because if that is part of the filename, it needs to stay and not be decoded.
Url = Utilities.UrlUtilities.UrlDecode(Url.Replace("+", "%2B"));
if (!NoUrlDecode)
{
Url = Utilities.UrlUtilities.UrlDecode(Url.Replace("+", "%2B"));
}

var webUrl = CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);

Expand Down
Loading