Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit 75119cf

Browse files
Merge pull request #1630 from KoenZomers/AllowFileNameOnAddFilleAsFile
Allow -FileName with Add-PnPFile -Path
2 parents 437611c + 44a729e commit 75119cf

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

Commands/Files/AddFile.cs

+16-7
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
using SharePointPnP.PowerShell.CmdletHelpAttributes;
77
using SharePointPnP.PowerShell.Commands.Base.PipeBinds;
88
using System;
9-
using System.Linq;
10-
using System.Collections.Generic;
11-
using Microsoft.SharePoint.Client.Taxonomy;
129
using SharePointPnP.PowerShell.Commands.Utilities;
1310
using SharePointPnP.PowerShell.Commands.Enums;
1411

@@ -43,6 +40,10 @@ namespace SharePointPnP.PowerShell.Commands.Files
4340
Code = @"PS:> Add-PnPFile -FileName sample.docx -Folder ""Documents"" -Values @{Modified=""1/1/2016""; Created=""1/1/2017""; Editor=23}",
4441
Remarks = "This will add a file sample.docx to the Documents folder and will set the Modified date to 1/1/2016, Created date to 1/1/2017 and the Modified By field to the user with ID 23. To find out about the proper user ID to relate to a specific user, use Get-PnPUser.",
4542
SortOrder = 6)]
43+
[CmdletExample(
44+
Code = @"PS:> Add-PnPFile -FileName sample.docx -Folder ""Documents"" -NewFileName ""differentname.docx""",
45+
Remarks = "This will upload a local file sample.docx to the Documents folder giving it the filename differentname.docx on SharePoint",
46+
SortOrder = 7)]
4647

4748
public class AddFile : PnPWebCmdlet
4849
{
@@ -57,10 +58,13 @@ public class AddFile : PnPWebCmdlet
5758

5859
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_ASSTREAM, HelpMessage = "Name for file")]
5960
public string FileName = string.Empty;
61+
62+
[Parameter(Mandatory = false, ParameterSetName = ParameterSet_ASFILE, HelpMessage = "Filename to give the file on SharePoint")]
63+
public string NewFileName = string.Empty;
64+
6065
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_ASSTREAM, HelpMessage = "Stream with the file contents")]
6166
public Stream Stream;
6267

63-
6468
[Parameter(Mandatory = false, HelpMessage = "If versioning is enabled, this will check out the file first if it exists, upload the file, then check it in again.")]
6569
public SwitchParameter Checkout;
6670

@@ -109,14 +113,20 @@ public class AddFile : PnPWebCmdlet
109113

110114
protected override void ExecuteCmdlet()
111115
{
112-
113116
if (ParameterSetName == ParameterSet_ASFILE)
114117
{
115118
if (!System.IO.Path.IsPathRooted(Path))
116119
{
117120
Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
118121
}
119-
FileName = System.IO.Path.GetFileName(Path);
122+
if (string.IsNullOrEmpty(NewFileName))
123+
{
124+
FileName = System.IO.Path.GetFileName(Path);
125+
}
126+
else
127+
{
128+
FileName = NewFileName;
129+
}
120130
}
121131

122132
SelectedWeb.EnsureProperty(w => w.ServerRelativeUrl);
@@ -128,7 +138,6 @@ protected override void ExecuteCmdlet()
128138
//Check to see if the Content Type exists.. If it doesn't we are going to throw an exception and block this transaction right here.
129139
if (ContentType != null)
130140
{
131-
132141
try
133142
{
134143
var list = SelectedWeb.GetListByUrl(folder.ServerRelativeUrl);

0 commit comments

Comments
 (0)