Skip to content

Commit 51be3d3

Browse files
author
Gautam Sheth
committed
Enhance URL handling in file version commands to support relative URLs and improve decoding logic
1 parent 43cf786 commit 51be3d3

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

src/Commands/Files/GetFileVersion.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
using Microsoft.SharePoint.Client;
22
using PnP.Framework.Utilities;
3+
using System;
34
using System.Management.Automation;
45
using File = Microsoft.SharePoint.Client.File;
56

67
namespace PnP.PowerShell.Commands.Files
78
{
89
[Cmdlet(VerbsCommon.Get, "PnPFileVersion", DefaultParameterSetName = "Return as file object")]
9-
10+
1011
public class GetFileVersion : PnPWebCmdlet
1112
{
1213
[Parameter(Mandatory = true)]
@@ -19,6 +20,15 @@ protected override void ExecuteCmdlet()
1920
{
2021
var serverRelativeUrl = string.Empty;
2122

23+
if (Uri.IsWellFormedUriString(Url, UriKind.Absolute))
24+
{
25+
// We can't deal with absolute URLs
26+
Url = UrlUtility.MakeRelativeUrl(Url);
27+
}
28+
29+
// 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.
30+
Url = Utilities.UrlUtilities.UrlDecode(Url.Replace("+", "%2B"));
31+
2232
var webUrl = CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);
2333

2434
if (!Url.ToLower().StartsWith(webUrl.ToLower()))
@@ -33,7 +43,7 @@ protected override void ExecuteCmdlet()
3343
File file;
3444

3545
file = CurrentWeb.GetFileByServerRelativePath(ResourcePath.FromDecodedUrl(serverRelativeUrl));
36-
46+
3747
if (UseVersionExpirationReport)
3848
{
3949
ClientContext.Load(file, f => f.Exists, f => f.VersionExpirationReport.IncludeWithDefaultProperties(i => i.CreatedBy, i => i.SnapshotDate, i => i.ExpirationDate));

src/Commands/Files/RemoveFileVersion.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.SharePoint.Client;
22
using PnP.Framework.Utilities;
33
using PnP.PowerShell.Commands.Base.PipeBinds;
4+
using System;
45
using System.Management.Automation;
56
using File = Microsoft.SharePoint.Client.File;
67
using Resources = PnP.PowerShell.Commands.Properties.Resources;
@@ -28,12 +29,19 @@ public class RemoveFileVersion : PnPWebCmdlet
2829
[Parameter(Mandatory = false, ParameterSetName = ParameterAttribute.AllParameterSets)]
2930
public SwitchParameter Force;
3031

31-
32-
3332
protected override void ExecuteCmdlet()
3433
{
3534
var serverRelativeUrl = string.Empty;
3635

36+
if (Uri.IsWellFormedUriString(Url, UriKind.Absolute))
37+
{
38+
// We can't deal with absolute URLs
39+
Url = UrlUtility.MakeRelativeUrl(Url);
40+
}
41+
42+
// 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.
43+
Url = Utilities.UrlUtilities.UrlDecode(Url.Replace("+", "%2B"));
44+
3745
var webUrl = CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);
3846

3947
if (!Url.ToLower().StartsWith(webUrl.ToLower()))
@@ -80,7 +88,7 @@ protected override void ExecuteCmdlet()
8088
else
8189
{
8290
versions.DeleteByLabel(Identity.Label);
83-
}
91+
}
8492
ClientContext.ExecuteQueryRetry();
8593
}
8694
else if (Identity.Id != -1)
@@ -92,7 +100,7 @@ protected override void ExecuteCmdlet()
92100
else
93101
{
94102
versions.DeleteByID(Identity.Id);
95-
}
103+
}
96104
ClientContext.ExecuteQueryRetry();
97105
}
98106
}

src/Commands/Files/RestoreFileVersion.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using Microsoft.SharePoint.Client;
22
using PnP.Framework.Utilities;
33
using PnP.PowerShell.Commands.Base.PipeBinds;
4-
using Resources = PnP.PowerShell.Commands.Properties.Resources;
4+
using System;
55
using System.Management.Automation;
66
using File = Microsoft.SharePoint.Client.File;
7+
using Resources = PnP.PowerShell.Commands.Properties.Resources;
78

89
namespace PnP.PowerShell.Commands.Files
910
{
@@ -23,6 +24,15 @@ protected override void ExecuteCmdlet()
2324
{
2425
var serverRelativeUrl = string.Empty;
2526

27+
if (Uri.IsWellFormedUriString(Url, UriKind.Absolute))
28+
{
29+
// We can't deal with absolute URLs
30+
Url = UrlUtility.MakeRelativeUrl(Url);
31+
}
32+
33+
// 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.
34+
Url = Utilities.UrlUtilities.UrlDecode(Url.Replace("+", "%2B"));
35+
2636
var webUrl = CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);
2737

2838
if (!Url.ToLower().StartsWith(webUrl.ToLower()))
@@ -62,7 +72,7 @@ protected override void ExecuteCmdlet()
6272
catch (ServerException e) when (e.ServerErrorTypeName.Equals("System.IO.DirectoryNotFoundException"))
6373
{
6474
throw new PSArgumentException($"Version with label '{Identity.Label}' does not exist", e);
65-
}
75+
}
6676
}
6777
else if (Identity.Id != -1)
6878
{
@@ -72,7 +82,7 @@ protected override void ExecuteCmdlet()
7282
ClientContext.Load(version);
7383
ClientContext.ExecuteQueryRetry();
7484

75-
if(version == null || !version.IsPropertyAvailable("VersionLabel"))
85+
if (version == null || !version.IsPropertyAvailable("VersionLabel"))
7686
{
7787
throw new PSArgumentException($"Version with id '{Identity.Id}' does not exist", nameof(Identity));
7888
}

0 commit comments

Comments
 (0)