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 pathBaseFileProvisioningCmdlet.cs
More file actions
304 lines (268 loc) · 14.5 KB
/
BaseFileProvisioningCmdlet.cs
File metadata and controls
304 lines (268 loc) · 14.5 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Utilities;
using OfficeDevPnP.Core.Framework.Provisioning.Connectors;
using OfficeDevPnP.Core.Framework.Provisioning.Model;
using OfficeDevPnP.Core.Framework.Provisioning.Providers;
using OfficeDevPnP.Core.Framework.Provisioning.Providers.Xml;
using SharePointPnP.PowerShell.Commands.Provisioning;
using SharePointPnP.PowerShell.Commands.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Text;
using PnPFileLevel = OfficeDevPnP.Core.Framework.Provisioning.Model.FileLevel;
using SPFile = Microsoft.SharePoint.Client.File;
namespace SharePointPnP.PowerShell.Commands
{
/// <summary>
/// Base class for commands related to adding file to template
/// </summary>
public class BaseFileProvisioningCmdlet : PnPWebCmdlet
{
protected const string PSNAME_LOCAL_SOURCE = "LocalSourceFile";
protected const string PSNAME_REMOTE_SOURCE = "RemoteSourceFile";
[Parameter(Mandatory = true, Position = 0, HelpMessage = "Filename of the .PNP Open XML provisioning template to read from, optionally including full path.")]
public string Path;
[Parameter(Mandatory = false, Position = 3, HelpMessage = "The target Container for the file to add to the in-memory template, optional argument.")]
public string Container;
[Parameter(Mandatory = false, Position = 4, HelpMessage = "The level of the files to add. Defaults to Published")]
public PnPFileLevel FileLevel = PnPFileLevel.Published;
[Parameter(Mandatory = false, Position = 5, HelpMessage = "Set to overwrite in site, Defaults to true")]
public SwitchParameter FileOverwrite = true;
[Parameter(Mandatory = false, Position = 6, ParameterSetName = PSNAME_REMOTE_SOURCE, HelpMessage = "Include webparts when the file is a page")]
public SwitchParameter ExtractWebParts = true;
[Parameter(Mandatory = false, Position = 7, ParameterSetName = PSNAME_REMOTE_SOURCE, HelpMessage = "Include webparts when the file is a page")]
public SwitchParameter ExtractFileProperties = true;
[Parameter(Mandatory = false, Position = 8, HelpMessage = "Allows you to specify ITemplateProviderExtension to execute while loading the template.")]
public ITemplateProviderExtension[] TemplateProviderExtensions;
protected readonly ProgressRecord _progressEnumeration = new ProgressRecord(0, "Activity", "Status") { Activity = "Enumerating folder" };
protected readonly ProgressRecord _progressFilesEnumeration = new ProgressRecord(1, "Activity", "Status") { Activity = "Extracting files" };
protected readonly ProgressRecord _progressFileProcessing = new ProgressRecord(2, "Activity", "Status") { Activity = "Extracting file" };
protected override void ProcessRecord()
{
base.ProcessRecord();
var ctx = (ClientContext)SelectedWeb.Context;
ctx.Load(SelectedWeb, web => web.Id, web => web.ServerRelativeUrl, web => web.Url);
if (ExtractWebParts)
{
ctx.Load(ctx.Site, site => site.Id, site => site.ServerRelativeUrl, site => site.Url);
ctx.Load(SelectedWeb.Lists, lists => lists.Include(l => l.Title, l => l.RootFolder.ServerRelativeUrl, l => l.Id));
}
ctx.ExecuteQueryRetry();
}
protected ProvisioningTemplate LoadTemplate()
{
if (!System.IO.Path.IsPathRooted(Path))
{
Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path);
}
// Load the template
var template = ReadProvisioningTemplate
.LoadProvisioningTemplateFromFile(Path,
TemplateProviderExtensions);
if (template == null)
{
throw new ApplicationException("Invalid template file!");
}
return template;
}
/// <summary>
/// Add a file to the template
/// </summary>
/// <param name="template">The provisioning template to add the file to</param>
/// <param name="fs">Stream to read the file content</param>
/// <param name="folder">target folder in the provisioning template</param>
/// <param name="fileName">Name of the file</param>
/// <param name="container">Container path within the template (pnp file) or related to the xml templage</param>
/// <param name="webParts">WebParts to include</param>
/// <param name="properties">Properties of the file</param>
protected void AddFileToTemplate(
ProvisioningTemplate template,
Stream fs,
string folder,
string fileName,
string container,
IEnumerable<WebPart> webParts = null,
IDictionary<string, string> properties = null
)
{
if (template == null) throw new ArgumentNullException(nameof(template));
if (fs == null) throw new ArgumentNullException(nameof(fs));
if (fileName == null) throw new ArgumentNullException(nameof(fileName));
var source = !string.IsNullOrEmpty(container) ? (container + "/" + fileName) : fileName;
template.Connector.SaveFileStream(fileName, container, fs);
if (template.Connector is ICommitableFileConnector)
{
((ICommitableFileConnector)template.Connector).Commit();
}
var existing = template.Files.FirstOrDefault(f => f.Src == $"{container}/{fileName}" && f.Folder == folder);
if (existing != null)
template.Files.Remove(existing);
var newFile = new OfficeDevPnP.Core.Framework.Provisioning.Model.File
{
Src = source,
Folder = folder,
Level = FileLevel,
Overwrite = FileOverwrite
};
if (webParts != null) newFile.WebParts.AddRange(webParts);
if (properties != null)
{
foreach (var property in properties)
{
newFile.Properties.Add(property.Key, property.Value);
}
}
template.Files.Add(newFile);
// Determine the output file name and path
var outFileName = System.IO.Path.GetFileName(Path);
var outPath = new FileInfo(Path).DirectoryName;
var fileSystemConnector = new FileSystemConnector(outPath, "");
var formatter = XMLPnPSchemaFormatter.LatestFormatter;
var extension = new FileInfo(Path).Extension.ToLowerInvariant();
if (extension == ".pnp")
{
var provider = new XMLOpenXMLTemplateProvider(template.Connector as OpenXMLConnector);
var templateFileName = outFileName.Substring(0, outFileName.LastIndexOf(".", StringComparison.Ordinal)) + ".xml";
provider.SaveAs(template, templateFileName, formatter, TemplateProviderExtensions);
}
else
{
XMLTemplateProvider provider = new XMLFileSystemTemplateProvider(Path, "");
provider.SaveAs(template, Path, formatter, TemplateProviderExtensions);
}
}
/// <summary>
/// Adds a remote file to a template
/// </summary>
/// <param name="template">Template to add the file to</param>
/// <param name="file">The SharePoint file to retrieve and add</param>
protected void AddSPFileToTemplate(ProvisioningTemplate template, SPFile file)
{
if (template == null) throw new ArgumentNullException(nameof(template));
if (file == null) throw new ArgumentNullException(nameof(file));
file.EnsureProperties(f => f.Name, f => f.ServerRelativeUrl);
_progressFileProcessing.StatusDescription = $"Extracting file {file.ServerRelativeUrl}";
var folderRelativeUrl = file.ServerRelativeUrl.Substring(0, file.ServerRelativeUrl.Length - file.Name.Length - 1);
var folderWebRelativeUrl = HttpUtility.UrlKeyValueDecode(folderRelativeUrl.Substring(SelectedWeb.ServerRelativeUrl.TrimEnd('/').Length + 1));
if (ClientContext.HasPendingRequest) ClientContext.ExecuteQuery();
try
{
IEnumerable<WebPart> webParts = null;
if (ExtractWebParts)
{
webParts = ExtractSPFileWebParts(file).ToArray();
_progressFileProcessing.PercentComplete = 25;
_progressFileProcessing.StatusDescription = $"Extracting webpart from {file.ServerRelativeUrl} ";
WriteProgress(_progressFileProcessing);
}
using (var fi = SPFile.OpenBinaryDirect(ClientContext, file.ServerRelativeUrl))
using (var ms = new MemoryStream())
{
_progressFileProcessing.PercentComplete = 50;
_progressFileProcessing.StatusDescription = $"Reading file {file.ServerRelativeUrl}";
WriteProgress(_progressFileProcessing);
// We are using a temporary memory stream because the file connector is seeking in the stream
// and the stream provided by OpenBinaryDirect does not allow it
fi.Stream.CopyTo(ms);
ms.Position = 0;
IDictionary<string, string> properties = null;
if (ExtractFileProperties && string.Compare(System.IO.Path.GetExtension(file.Name), ".aspx", true) == 0)
{
_progressFileProcessing.PercentComplete = 35;
_progressFileProcessing.StatusDescription = $"Extracting properties from {file.ServerRelativeUrl}";
properties = XmlPageDataHelper.ExtractProperties(
Encoding.UTF8.GetString(ms.ToArray())
).ToDictionary(p => p.Key, p => Tokenize(p.Value));
}
AddFileToTemplate(template, ms, folderWebRelativeUrl, file.Name, folderWebRelativeUrl, webParts, properties);
_progressFileProcessing.PercentComplete = 100;
_progressFileProcessing.StatusDescription = $"Adding file {file.ServerRelativeUrl} to template";
_progressFileProcessing.RecordType = ProgressRecordType.Completed;
WriteProgress(_progressFileProcessing);
}
}
catch (Exception exc)
{
WriteWarning($"Error trying to add file {file.ServerRelativeUrl} : {exc.Message}");
}
}
private IEnumerable<WebPart> ExtractSPFileWebParts(SPFile file)
{
if (file == null) throw new ArgumentNullException(nameof(file));
if (string.Compare(System.IO.Path.GetExtension(file.Name), ".aspx", true) == 0)
{
foreach (var spwp in SelectedWeb.GetWebParts(file.ServerRelativeUrl))
{
spwp.EnsureProperties(wp => wp.WebPart
#if !SP2016 // Missing ZoneId property in SP2016 version of the CSOM Library
, wp => wp.ZoneId
#endif
);
yield return new WebPart
{
Contents = Tokenize(SelectedWeb.GetWebPartXml(spwp.Id, file.ServerRelativeUrl)),
Order = (uint)spwp.WebPart.ZoneIndex,
Title = spwp.WebPart.Title,
#if !SP2016 // Missing ZoneId property in SP2016 version of the CSOM Library
Zone = spwp.ZoneId
#endif
};
}
}
}
/// <summary>
/// Adds a local file to a template
/// </summary>
/// <param name="template">Template to add the file to</param>
/// <param name="file">Full path to a local file</param>
/// <param name="folder">Destination folder of the added file</param>
protected void AddLocalFileToTemplate(ProvisioningTemplate template, string file, string folder)
{
if (template == null) throw new ArgumentNullException(nameof(template));
if (file == null) throw new ArgumentNullException(nameof(file));
if (folder == null) throw new ArgumentNullException(nameof(folder));
_progressFileProcessing.Activity = $"Extracting file {file}";
_progressFileProcessing.StatusDescription = "Adding file {file}";
_progressFileProcessing.PercentComplete = 0;
WriteProgress(_progressFileProcessing);
try
{
var fileName = System.IO.Path.GetFileName(file);
var container = !string.IsNullOrEmpty(Container) ? Container : folder.Replace("\\", "/");
using (var fs = System.IO.File.OpenRead(file))
{
AddFileToTemplate(template, fs, folder.Replace("\\", "/"), fileName, container);
}
}
catch (Exception exc)
{
WriteWarning($"Error trying to add file {file} : {exc.Message}");
}
_progressFileProcessing.RecordType = ProgressRecordType.Completed;
WriteProgress(_progressFileProcessing);
}
private string Tokenize(string input)
{
if (string.IsNullOrEmpty(input)) return input;
foreach (var list in SelectedWeb.Lists)
{
var webRelativeUrl = list.GetWebRelativeUrl();
if (!webRelativeUrl.StartsWith("_catalogs", StringComparison.Ordinal))
{
input = input
.ReplaceCaseInsensitive(list.Id.ToString("D"), "{listid:" + list.Title + "}")
.ReplaceCaseInsensitive(webRelativeUrl, "{listurl:" + list.Title + "}");
}
}
return input.ReplaceCaseInsensitive(SelectedWeb.Url, "{site}")
.ReplaceCaseInsensitive(SelectedWeb.ServerRelativeUrl, "{site}")
.ReplaceCaseInsensitive(SelectedWeb.Id.ToString(), "{siteid}")
.ReplaceCaseInsensitive(((ClientContext)SelectedWeb.Context).Site.ServerRelativeUrl, "{sitecollection}")
.ReplaceCaseInsensitive(((ClientContext)SelectedWeb.Context).Site.Id.ToString(), "{sitecollectionid}")
.ReplaceCaseInsensitive(((ClientContext)SelectedWeb.Context).Site.Url, "{sitecollection}");
}
}
}