This repository was archived by the owner on Mar 26, 2026. It is now read-only.
forked from pnp/PnP-PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddFileToProvisioningTemplate.cs
More file actions
78 lines (68 loc) · 4.12 KB
/
AddFileToProvisioningTemplate.cs
File metadata and controls
78 lines (68 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using Microsoft.SharePoint.Client;
using SharePointPnP.PowerShell.CmdletHelpAttributes;
using System;
using System.Management.Automation;
namespace SharePointPnP.PowerShell.Commands.Provisioning.Site
{
[Cmdlet(VerbsCommon.Add, "PnPFileToProvisioningTemplate")]
[CmdletHelp("Adds a file to a PnP Provisioning Template",
Category = CmdletHelpCategory.Provisioning)]
[CmdletExample(
Code = @"PS:> Add-PnPFileToProvisioningTemplate -Path template.pnp -Source $sourceFilePath -Folder $targetFolder",
Remarks = "Adds a file to a PnP Site Template",
SortOrder = 1)]
[CmdletExample(
Code = @"PS:> Add-PnPFileToProvisioningTemplate -Path template.xml -Source $sourceFilePath -Folder $targetFolder",
Remarks = "Adds a file reference to a PnP Site XML Template",
SortOrder = 2)]
[CmdletExample(
Code = @"PS:> Add-PnPFileToProvisioningTemplate -Path template.pnp -Source ""./myfile.png"" -Folder ""folderinsite"" -FileLevel Published -FileOverwrite:$false",
Remarks = "Adds a file to a PnP Site Template, specifies the level as Published and defines to not overwrite the file if it exists in the site.",
SortOrder = 3)]
[CmdletExample(
Code = @"PS:> Add-PnPFileToProvisioningTemplate -Path template.pnp -Source $sourceFilePath -Folder $targetFolder -Container $container",
Remarks = "Adds a file to a PnP Site Template with a custom container for the file",
SortOrder = 4)]
[CmdletExample(
Code = @"PS:> Add-PnPFileToProvisioningTemplate -Path template.pnp -SourceUrl $urlOfFile",
Remarks = "Adds a file to a PnP Provisioning Template retrieved from the currently connected web. The url can be either full, server relative or Web relative url.",
SortOrder = 4)]
public class AddFileToProvisioningTemplate : BaseFileProvisioningCmdlet
{
/*
* Path, FileLevel, FileOverwrite and TemplateProviderExtensions fields are in the base class
* */
[Parameter(Mandatory = true, Position = 1, ParameterSetName = PSNAME_LOCAL_SOURCE, HelpMessage = "The file to add to the in-memory template, optionally including full path.")]
public string Source;
[Parameter(Mandatory = true, Position = 1, ParameterSetName = PSNAME_REMOTE_SOURCE, HelpMessage = "The file to add to the in-memory template, specifying its url in the current connected Web.")]
public string SourceUrl;
[Parameter(Mandatory = true, Position = 2, ParameterSetName = PSNAME_LOCAL_SOURCE, HelpMessage = "The target Folder for the file to add to the in-memory template.")]
public string Folder;
protected override void ProcessRecord()
{
var template = LoadTemplate();
if (this.ParameterSetName == PSNAME_REMOTE_SOURCE)
{
SelectedWeb.EnsureProperty(w => w.ServerRelativeUrl);
var sourceUri = new Uri(SourceUrl, UriKind.RelativeOrAbsolute);
// Get the server relative url of the file, whatever the input url is (absolute, server relative or web relative form)
var serverRelativeUrl =
sourceUri.IsAbsoluteUri ? sourceUri.AbsolutePath : // The url is absolute, extract the absolute path (http://server/sites/web/folder/file)
SourceUrl.StartsWith("/", StringComparison.Ordinal) ? SourceUrl : // The url is server relative. Take it as is (/sites/web/folder/file)
SelectedWeb.ServerRelativeUrl.TrimEnd('/') + "/" + SourceUrl; // The url is web relative, prepend by the web url (folder/file)
var file = SelectedWeb.GetFileByServerRelativeUrl(serverRelativeUrl);
AddSPFileToTemplate(template, file);
}
else
{
if (!System.IO.Path.IsPathRooted(Source))
{
Source = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Source);
}
// Load the file and add it to the .PNP file
Folder = Folder.Replace("\\", "/");
AddLocalFileToTemplate(template, Source, Folder);
}
}
}
}