Buy Me a Coffee

Buy Me a Coffee!

PnP Core Template Capture Console Source

using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
 
namespace PnPCoreProvisioning.Part1
{
    class Program
    {
        static string userName = "username@";
        static string passwordString = @"password";
        static string sourceUrl = "http://server.farm.com/sites/blogpost";
        static string fileName = "template.xml";
        static void Main(string[] args)
        {
            using (var context = new ClientContext(sourceUrl))
            {
                SecureString securePassword = new SecureString();
                foreach (char c in passwordString)
                {
                    securePassword.AppendChar(c);
                }
                context.Credentials = new NetworkCredential(userName, securePassword);
                Web web = context.Web;
                context.Load(web, w => w.Title);
                context.ExecuteQueryRetry();
                var siteName = web.Title;
                var defaultForeground = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Source site title is: " + siteName);
                Console.ForegroundColor = defaultForeground;
                ProvisioningTemplateCreationInformation templateCreationInformation 
                    = new ProvisioningTemplateCreationInformation(web);
                templateCreationInformation.PersistBrandingFiles = false;
                templateCreationInformation.ProgressDelegate 
                    = delegate (String message, Int32 progress, Int32 total)
                {
 
                    // Use this to simply output progress to the console application UI
                    Console.WriteLine("{0:00}/{1:00} - {2}", progress, total, message);
                };
                var provisioningTemplate 
                    = web.GetProvisioningTemplate(templateCreationInformation);
                System.IO.File.WriteAllText(
                    string.Format(@".\{0}", fileName), provisioningTemplate.ToXML()
                    );
            }
        }
    }
}