public PnPFileInfo GetPnPTemplateFileFromSite(CreatePnPTemplateRequest request) { var pnpPackageInfo = new PnPFileInfo(); try { using (var context = TokenHelper.GetClientContextWithAccessToken(request.SiteUrl, request.AccessToken)) { var web = context.Web; context.Load(web, w => w.Title, w => w.ServerRelativeUrl, w => w.Url); context.ExecuteQuery(); var siteUrl = new Uri(request.SiteUrl); var pnpFileName = ""; if (string.IsNullOrEmpty(request.PnpPackageName)) { pnpFileName = BuildPnPPackageName(siteUrl); } else { pnpFileName = request.PnpPackageName; if (pnpFileName.ToLower().EndsWith(".pnp")) { pnpFileName = pnpFileName.Substring(0, pnpFileName.Length - 4); } } var pnpTemplatePath = HostingEnvironment.MapPath($"~/{TemplateFolder}"); var ptci = new ProvisioningTemplateCreationInformation(context.Web); var fileSystemConnector = new FileSystemConnector(pnpTemplatePath, ""); ptci.PersistBrandingFiles = true; ptci.PersistPublishingFiles = true; ptci.PersistMultiLanguageResources = true; ptci.FileConnector = new OpenXMLConnector($"{pnpFileName}.pnp", fileSystemConnector); ptci.ProgressDelegate = delegate(String message, Int32 progress, Int32 total) { Console.WriteLine(@"{0:00}/{1:00} - {2}", progress, total, message); }; ProvisioningTemplate template = new ProvisioningTemplate(); try { template = web.GetProvisioningTemplate(ptci); } catch (Exception ex) { Console.WriteLine("PnP engine failed to extract template. Error: {0}", ex.Message); } try { PageProvisionManager pageProvisionManager = new PageProvisionManager(); var pagesTemplate = pageProvisionManager.Extract(context, ptci); foreach (var theFile in pagesTemplate.Files) { var existingFile = template.Files.FirstOrDefault( f => f.Src.Equals(theFile.Src, StringComparison.InvariantCultureIgnoreCase)); if (existingFile == null) { template.Files.Add(theFile); } } } catch (Exception ex) { Console.WriteLine("Failed to extract Pages. Error: {0}", ex.Message); } if (web.IsSubSite()) { try { var siteColumnsTemplate = new SiteColumnProvisionManager().Extract(web, ptci); foreach (var col in siteColumnsTemplate.SiteFields) { if (!template.SiteFields.Contains(col)) { template.SiteFields.Add(col); } } } catch (Exception ex) { Console.WriteLine("Failed to extract Site Columns. Error: {0}", ex.Message); } try { var siteCTTemplate = new ContentTypeProvisionManager().Extract(web, ptci); foreach (var ct in siteCTTemplate.ContentTypes) { if (!template.ContentTypes.Contains(ct)) { template.ContentTypes.Add(ct); } } } catch (Exception ex) { Console.WriteLine("Failed to extract Site Content Types. Error: {0}", ex.Message); } } XMLTemplateProvider provider = new XMLOpenXMLTemplateProvider((OpenXMLConnector)ptci.FileConnector); provider.SaveAs(template, $"{pnpFileName}.xml"); string fileLocation = $"{pnpTemplatePath}\\{pnpFileName}.pnp"; var file = new FileInfo(fileLocation); fileStorageManager.SaveFile($"{TemplateFolder}\\{pnpFileName}.pnp", System.IO.File.ReadAllBytes(fileLocation)); pnpPackageInfo = new PnPFileInfo() { Name = $"{pnpFileName}.pnp", Size = file.Length / 1024.0m }; if (file.Exists) { file.Delete(); } } } catch (Exception ex) { throw ex; } return(pnpPackageInfo); }
private void btnExtract_Click(object sender, EventArgs e) { try { DebugMonitor.Start(); DebugMonitor.OnOutputDebugString += DebugMonitor_OnOutputDebugString; var web = Context.Web; Context.Load(web, w => w.Title, w => w.ServerRelativeUrl, w => w.Url); Context.ExecuteQuery(); var siteUrl = new Uri(SiteUrl); var pnpFileName = ""; pnpFileName = BuildPnPPackageName(siteUrl); var pnpTemplatePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\PnPTemplates"; var ptci = new ProvisioningTemplateCreationInformation(web); var fileSystemConnector = new FileSystemConnector(pnpTemplatePath, ""); ptci.PersistBrandingFiles = true; ptci.PersistPublishingFiles = true; ptci.PersistMultiLanguageResources = true; ptci.FileConnector = new OpenXMLConnector($"{pnpFileName}.pnp", fileSystemConnector); ptci.ProgressDelegate = delegate(String message, Int32 progress, Int32 total) { Console.WriteLine(@"{0:00}/{1:00} - {2}", progress, total, message); }; ProvisioningTemplate template = new ProvisioningTemplate(); try { template = web.GetProvisioningTemplate(ptci); } catch (Exception ex) { Console.WriteLine("PnP engine failed to extract template. Error: {0}", ex.Message); } try { PageProvisionManager pageProvisionManager = new PageProvisionManager(); var pagesTemplate = pageProvisionManager.Extract(Context, ptci); foreach (var theFile in pagesTemplate.Files) { var existingFile = template.Files.FirstOrDefault( f => f.Src.Equals(theFile.Src, StringComparison.InvariantCultureIgnoreCase)); if (existingFile == null) { template.Files.Add(theFile); } } } catch (Exception ex) { Console.WriteLine("Failed to extract Pages. Error: {0}", ex.Message); } if (web.IsSubSite()) { try { var siteColumnsTemplate = new SiteColumnProvisionManager().Extract(web, ptci); foreach (var col in siteColumnsTemplate.SiteFields) { if (!template.SiteFields.Contains(col)) { template.SiteFields.Add(col); } } } catch (Exception ex) { Console.WriteLine("Failed to extract Site Columns. Error: {0}", ex.Message); } try { var siteCTTemplate = new ContentTypeProvisionManager().Extract(web, ptci); foreach (var ct in siteCTTemplate.ContentTypes) { if (!template.ContentTypes.Contains(ct)) { template.ContentTypes.Add(ct); } } } catch (Exception ex) { Console.WriteLine("Failed to extract Site Content Types. Error: {0}", ex.Message); } } pgTemplate.SelectedObject = template; DebugMonitor.Stop(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }