public string ApplyPnPTemplateOnSite(ApplyPnPTemplateRequest request)
        {
            try
            {
                ProvisioningTemplate template = null;
                var ptai = new ProvisioningTemplateApplyingInformation();
                ptai.ProgressDelegate = delegate(String message, Int32 progress, Int32 total)
                {
                    Console.WriteLine(@"{0:00}/{1:00} - {2}", progress, total, message);
                };
                if (string.IsNullOrEmpty(request.PnPXML)) //normal pnp file flow
                {
                    var storageFile     = fileStorageManager.GetFile($"{TemplateFolder}\\{request.PnPFileName}");
                    var pnpTemplatePath = HostingEnvironment.MapPath($"~/{TemplateFolder}");
                    var tempFile        = $"{pnpTemplatePath}\\{request.PnPFileName}";
                    System.IO.File.WriteAllBytes(tempFile, storageFile.Content);
                    using (
                        var context = TokenHelper.GetClientContextWithAccessToken(request.SiteUrl, request.AccessToken))
                    {
                        var fileSystemConnector      = new FileSystemConnector(pnpTemplatePath, "");
                        XMLTemplateProvider provider =
                            new XMLOpenXMLTemplateProvider(new OpenXMLConnector(request.PnPFileName, fileSystemConnector));

                        template           = provider.GetTemplate(request.PnPFileName.Replace(".pnp", ".xml"));
                        template.Connector = provider.Connector;

                        RefineTemplate(context, template, ptai);

                        context.Web.ApplyProvisioningTemplate(template, ptai);
                        if (System.IO.File.Exists(tempFile))
                        {
                            System.IO.File.Delete(tempFile);
                        }
                        return("{ \"IsSuccess\": true, \"Message\": \"PnP template has been applied successfully\" }");
                    }
                }
                else
                {
                    //PNP xml flow
                    using (Stream s = GenerateStreamFromString(request.PnPXML))
                    {
                        var t = new XMLPnPSchemaFormatter();
                        template = t.ToProvisioningTemplate(s);
                    }

                    using (var context = TokenHelper.GetClientContextWithAccessToken(request.SiteUrl, request.AccessToken))
                    {
                        RefineTemplate(context, template, ptai);
                        context.Web.ApplyProvisioningTemplate(template, ptai);

                        return("{ \"IsSuccess\": true, \"Message\": \"PnP template has been applied successfully\" }");
                    }
                }
            }
            catch (Exception ex)
            {
                return("{ \"IsSuccess\": false, \"Message\": \"PnP template apply failed, Error: " + ex.Message + "\" }");
            }
        }
Example #2
0
 public string ApplyPnPTemplateOnSite(ApplyPnPTemplateRequest request)
 {
     return(templateManager.ApplyPnPTemplateOnSite(request));
 }