private void ProcessFiles(string templateFileName, FileConnectorBase fileSystemConnector, FileConnectorBase connector) { var templateFile = ReadProvisioningTemplate.LoadProvisioningTemplateFromFile(templateFileName, null, (e) => { WriteError(new ErrorRecord(e, "TEMPLATENOTVALID", ErrorCategory.SyntaxError, null)); }); if (Template.Tenant?.AppCatalog != null) { foreach (var app in Template.Tenant.AppCatalog.Packages) { WriteObject($"Processing {app.Src}"); AddFile(app.Src, templateFile, fileSystemConnector, connector); } } if (Template.Tenant?.SiteScripts != null) { foreach (var siteScript in Template.Tenant.SiteScripts) { WriteObject($"Processing {siteScript.JsonFilePath}"); AddFile(siteScript.JsonFilePath, templateFile, fileSystemConnector, connector); } } if (Template.Localizations != null && Template.Localizations.Any()) { foreach (var location in Template.Localizations) { WriteObject($"Processing {location.ResourceFile}"); AddFile(location.ResourceFile, templateFile, fileSystemConnector, connector); } } if (Template.WebSettings != null && !String.IsNullOrEmpty(Template.WebSettings.SiteLogo)) { // is it a file? var isFile = false; using (var fileStream = fileSystemConnector.GetFileStream(Template.WebSettings.SiteLogo)) { isFile = fileStream != null; } if (isFile) { WriteObject($"Processing {Template.WebSettings.SiteLogo}"); AddFile(Template.WebSettings.SiteLogo, templateFile, fileSystemConnector, connector); } } if (Template.Files.Any()) { foreach (var file in Template.Files) { WriteObject($"Processing {file.Src}"); AddFile(file.Src, templateFile, fileSystemConnector, connector); } } if (templateFile.Connector is ICommitableFileConnector) { ((ICommitableFileConnector)templateFile.Connector).Commit(); } }
protected override void ProcessRecord() { if (!System.IO.Path.IsPathRooted(Path)) { Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path); } // Load the template ProvisioningTemplate template = ReadProvisioningTemplate .LoadProvisioningTemplateFromFile(Path, TemplateProviderExtensions); if (template == null) { throw new ApplicationException("Invalid template file!"); } var fileToRemove = template.Files.FirstOrDefault(f => f.Src == FilePath); if (fileToRemove != null) { template.Files.Remove(fileToRemove); template.Connector.DeleteFile(FilePath); if (template.Connector is ICommitableFileConnector) { ((ICommitableFileConnector)template.Connector).Commit(); } // 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") { XMLTemplateProvider provider = new XMLOpenXMLTemplateProvider(new OpenXMLConnector(outPath, fileSystemConnector)); 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); } } }
protected override void ExecuteCmdlet() { if (!System.IO.Path.IsPathRooted(Path)) { Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path); } var template = ReadProvisioningTemplate .LoadProvisioningTemplateFromFile(Path, TemplateProviderExtensions); if (template == null) { throw new ApplicationException("Invalid template file!"); } //We will remove a list if it's found so we can get the list ListInstance listInstance = template.Lists.Find(l => l.Title == List.Title); if (listInstance == null) { throw new ApplicationException("List does not exist in the template file!"); } List spList = List.GetList(SelectedWeb); ClientContext.Load(spList, l => l.RootFolder, l => l.HasUniqueRoleAssignments); ClientContext.ExecuteQueryRetry(); if (TokenizeUrls.IsPresent) { ClientContext.Load(ClientContext.Web, w => w.Url, w => w.ServerRelativeUrl, w => w.Id); ClientContext.Load(ClientContext.Site, s => s.Url, s => s.ServerRelativeUrl, s => s.Id); ClientContext.Load(ClientContext.Web.Lists, lists => lists.Include(l => l.Title, l => l.RootFolder.ServerRelativeUrl)); } CamlQuery query = new CamlQuery(); var viewFieldsStringBuilder = new StringBuilder(); if (Fields != null) { viewFieldsStringBuilder.Append("<ViewFields>"); foreach (var field in Fields) { viewFieldsStringBuilder.AppendFormat("<FieldRef Name='{0}'/>", field); } viewFieldsStringBuilder.Append("</ViewFields>"); } query.ViewXml = string.Format("<View>{0}{1}</View>", Query, viewFieldsStringBuilder); var listItems = spList.GetItems(query); ClientContext.Load(listItems, lI => lI.Include(l => l.HasUniqueRoleAssignments, l => l.ContentType.StringId)); ClientContext.ExecuteQueryRetry(); Microsoft.SharePoint.Client.FieldCollection fieldCollection = spList.Fields; ClientContext.Load(fieldCollection, fs => fs.Include(f => f.InternalName, f => f.FieldTypeKind, f => f.ReadOnlyField)); ClientContext.ExecuteQueryRetry(); var rows = new DataRowCollection(template); foreach (var listItem in listItems) { //Make sure we don't pull Folders.. Of course this won't work if (listItem.ServerObjectIsNull == false) { ClientContext.Load(listItem); ClientContext.ExecuteQueryRetry(); if (!(listItem.FileSystemObjectType == FileSystemObjectType.Folder)) { DataRow row = new DataRow(); if (IncludeSecurity && listItem.HasUniqueRoleAssignments) { row.Security.ClearSubscopes = true; row.Security.CopyRoleAssignments = false; var roleAssignments = listItem.RoleAssignments; ClientContext.Load(roleAssignments); ClientContext.ExecuteQueryRetry(); ClientContext.Load(roleAssignments, r => r.Include(a => a.Member.LoginName, a => a.Member, a => a.RoleDefinitionBindings)); ClientContext.ExecuteQueryRetry(); foreach (var roleAssignment in roleAssignments) { var principalName = roleAssignment.Member.LoginName; var roleBindings = roleAssignment.RoleDefinitionBindings; foreach (var roleBinding in roleBindings) { row.Security.RoleAssignments.Add(new OfficeDevPnP.Core.Framework.Provisioning.Model.RoleAssignment() { Principal = principalName, RoleDefinition = roleBinding.Name }); } } } if (Fields != null) { foreach (var fieldName in Fields) { Microsoft.SharePoint.Client.Field dataField = fieldCollection.FirstOrDefault(f => f.InternalName == fieldName); if (dataField != null) { var defaultFieldValue = GetFieldValueAsText(ClientContext.Web, listItem, dataField); if (TokenizeUrls.IsPresent) { defaultFieldValue = Tokenize(defaultFieldValue, ClientContext.Web, ClientContext.Site); } row.Values.Add(fieldName, defaultFieldValue); } } } else { //All fields are added except readonly fields and unsupported field type var fieldsToExport = fieldCollection.AsEnumerable() .Where(f => !f.ReadOnlyField && !_unsupportedFieldTypes.Contains(f.FieldTypeKind)); foreach (var field in fieldsToExport) { var fldKey = (from f in listItem.FieldValues.Keys where f == field.InternalName select f).FirstOrDefault(); if (!string.IsNullOrEmpty(fldKey)) { var fieldValue = GetFieldValueAsText(ClientContext.Web, listItem, field); if (TokenizeUrls.IsPresent) { fieldValue = Tokenize(fieldValue, ClientContext.Web, ClientContext.Site); } row.Values.Add(field.InternalName, fieldValue); } } } rows.Add(row); } } } template.Lists.Remove(listInstance); listInstance.DataRows.AddRange(rows); template.Lists.Add(listInstance); 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") { XMLTemplateProvider provider = new XMLOpenXMLTemplateProvider(new OpenXMLConnector(Path, fileSystemConnector)); 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); } }
protected override void ExecuteCmdlet() { 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!"); } List spList = List.GetList(SelectedWeb); ClientContext.Load(spList, l => l.RootFolder, l => l.HasUniqueRoleAssignments); ClientContext.ExecuteQueryRetry(); //We will remove a list if it's found so we can get the list ListInstance listInstance = template.Lists.Find(l => l.Title == spList.Title); if (listInstance == null) { throw new ApplicationException("List does not exist in the template file!"); } Microsoft.SharePoint.Client.Folder listFolder = spList.RootFolder; ClientContext.Load(listFolder); ClientContext.ExecuteQueryRetry(); IList <OfficeDevPnP.Core.Framework.Provisioning.Model.Folder> folders = GetChildFolders(listFolder); template.Lists.Remove(listInstance); listInstance.Folders.AddRange(folders); template.Lists.Add(listInstance); // 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") { XMLTemplateProvider provider = new XMLOpenXMLTemplateProvider(new OpenXMLConnector(outPath, fileSystemConnector)); 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); } }
protected override void ProcessRecord() { if (!System.IO.Path.IsPathRooted(Path)) { Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path); } if (!System.IO.Path.IsPathRooted(Source)) { Source = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Source); } // Load the template var template = ReadProvisioningTemplate .LoadProvisioningTemplateFromFile(Path, TemplateProviderExtensions); if (template == null) { throw new ApplicationException("Invalid template file!"); } // Load the file and add it to the .PNP file using (var fs = new FileStream(Source, FileMode.Open, FileAccess.Read, FileShare.Read)) { Folder = Folder.Replace("\\", "/"); var fileName = Source.IndexOf("\\") > 0 ? Source.Substring(Source.LastIndexOf("\\") + 1) : Source; var container = !string.IsNullOrEmpty(Container) ? Container : string.Empty; var source = !string.IsNullOrEmpty(container) ? (container + "/" + fileName) : fileName; template.Connector.SaveFileStream(fileName, container, fs); if (template.Connector is ICommitableFileConnector) { ((ICommitableFileConnector)template.Connector).Commit(); } template.Files.Add(new OfficeDevPnP.Core.Framework.Provisioning.Model.File { Src = source, Folder = Folder, Level = FileLevel, Overwrite = FileOverwrite, }); // 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); } } }