SOLVED

Save as template again!!!

%3CLINGO-SUB%20id%3D%22lingo-sub-776995%22%20slang%3D%22en-US%22%3ESave%20as%20template%20again!!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-776995%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20wondering%20if%20I%20can%20save%20a%20team%20site%20on%20SharePoint%20online%20as%20a%20template%20in%20a%20simple%20way%3F%3F!!%3C%2FP%3E%3CP%3Enot%20using%20the%20PnP%20provisioning%20its%20too%20complicated...%3C%2FP%3E%3CP%3Eor%20is%20there%20a%20complete%20example%20of%20pnp%20code%20that%20works%20in%20visual%20studio%3F%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-776995%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESites%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777002%22%20slang%3D%22en-US%22%3ERe%3A%20Save%20as%20template%20again!!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777002%22%20slang%3D%22en-US%22%3E%3CP%3EDo%20you%20want%20to%20save%20a%20template%20from%20modern%20or%20classic%20team%20site%3F%3C%2FP%3E%3CP%3EWhat%20needs%20to%20be%20included%20in%20the%20template%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777017%22%20slang%3D%22en-US%22%3ERe%3A%20Save%20as%20template%20again!!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777017%22%20slang%3D%22en-US%22%3EJust%20wondering%2C%20why%20do%20you%20consider%20the%20PnP%20provisioning%20to%20be%20too%20complicated%3F%20You%20are%20looking%20for%20a%20solution%20from%20a%20Developer's%20perspective.%20With%20the%20PnP%20NuGet%20packages%20installed%20in%20your%20project%2C%20%22Save%20as%20template%22%20is%20basically%20just%201%20line%20of%20code%20(web.GetProvisioningTemplate).%20Applying%20a%20template%20to%20another%20site%20is%20also%20just%201%20line%20of%20code%20(web.ApplyProvisioningTemplate).%3CBR%20%2F%3E%3CBR%20%2F%3EA%20sample%20console%20application%20using%20the%20above%20techniques%20can%20be%20found%20here%3A%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsolution-guidance%2Fprovisioning-console-application-sample%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fsolution-guidance%2Fprovisioning-console-application-sample%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20SharePoint%20Online%20modern%20sites%20you%20can%20make%20use%20of%20Site%20Designs%20and%20Site%20Scripts.%20You%20could%20combine%20these%20with%20PnP%20provisioning%20as%20well.%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fdeclarative-customization%2Fsite-design-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fdeclarative-customization%2Fsite-design-overview%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EHope%20this%20helps!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777150%22%20slang%3D%22en-US%22%3ERe%3A%20Save%20as%20template%20again!!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777150%22%20slang%3D%22en-US%22%3Emodern%20team%20site%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777348%22%20slang%3D%22en-US%22%3ERe%3A%20Save%20as%20template%20again!!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777348%22%20slang%3D%22en-US%22%3E%3CP%3Ehi%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1803%22%20target%3D%22_blank%22%3E%40Paul%20Pascha%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethis%20is%20my%20code%3C%2FP%3E%3CP%3EIt%20works%20fine%20when%20i%20press%20start%20and%20go%20to%20the%20end%20of%20it%20but%20my%20destination%20site%20doesn't%20change...!!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eusing%20System%3B%3CBR%20%2F%3Eusing%20System.Collections.Generic%3B%3CBR%20%2F%3Eusing%20System.Linq%3B%3CBR%20%2F%3Eusing%20System.Text%3B%3CBR%20%2F%3Eusing%20System.Threading.Tasks%3B%3CBR%20%2F%3Eusing%20Microsoft.SharePoint.Client%3B%3CBR%20%2F%3Eusing%20OfficeDevPnP.Core.Framework.Provisioning.Connectors%3B%3CBR%20%2F%3Eusing%20OfficeDevPnP.Core.Framework.Provisioning.Model%3B%3CBR%20%2F%3Eusing%20OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers%3B%3CBR%20%2F%3Eusing%20OfficeDevPnP.Core.Framework.Provisioning.Providers.Xml%3B%3CBR%20%2F%3Eusing%20System.Net%3B%3CBR%20%2F%3Eusing%20System.Security%3B%3CBR%20%2F%3Eusing%20System.Threading%3B%3CBR%20%2F%3Enamespace%20Program%3CBR%20%2F%3E%7B%3CBR%20%2F%3Eclass%20Program%3CBR%20%2F%3E%7B%3CBR%20%2F%3Estatic%20void%20Main(string%5B%5D%20args)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EConsoleColor%20defaultForeground%20%3D%20Console.ForegroundColor%3B%3C%2FP%3E%3CP%3E%2F%2F%20Collect%20information%3CBR%20%2F%3Estring%20templateWebUrl%20%3D%20GetInput(%22Enter%20the%20URL%20of%20the%20template%20site%3A%20%22%2C%20false%2C%20defaultForeground)%3B%3CBR%20%2F%3Estring%20targetWebUrl%20%3D%20GetInput(%22Enter%20the%20URL%20of%20the%20target%20site%3A%20%22%2C%20false%2C%20defaultForeground)%3B%3CBR%20%2F%3Estring%20userName%20%3D%20GetInput(%22Enter%20your%20user%20name%3A%22%2C%20false%2C%20defaultForeground)%3B%3CBR%20%2F%3Estring%20pwdS%20%3D%20GetInput(%22Enter%20your%20password%3A%22%2C%20true%2C%20defaultForeground)%3B%3CBR%20%2F%3ESecureString%20pwd%20%3D%20new%20SecureString()%3B%3CBR%20%2F%3Eforeach%20(char%20c%20in%20pwdS.ToCharArray())%20pwd.AppendChar(c)%3B%3C%2FP%3E%3CP%3E%2F%2F%20GET%20the%20template%20from%20existing%20site%20and%20serialize%3CBR%20%2F%3E%2F%2F%20Serializing%20the%20template%20for%20later%20reuse%20is%20optional%3CBR%20%2F%3EProvisioningTemplate%20template%20%3D%20GetProvisioningTemplate(defaultForeground%2C%20templateWebUrl%2C%20userName%2C%20pwd)%3B%3C%2FP%3E%3CP%3E%2F%2F%20APPLY%20the%20template%20to%20new%20site%20from%3CBR%20%2F%3EApplyProvisioningTemplate(defaultForeground%2C%20targetWebUrl%2C%20userName%2C%20pwd%2C%20template)%3B%3C%2FP%3E%3CP%3E%2F%2F%20Pause%20and%20modify%20the%20UI%20to%20indicate%20that%20the%20operation%20is%20complete%3CBR%20%2F%3EConsole.ForegroundColor%20%3D%20ConsoleColor.White%3B%3CBR%20%2F%3EConsole.WriteLine(%22We're%20done.%20Press%20Enter%20to%20continue.%22)%3B%3CBR%20%2F%3EConsole.ReadLine()%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Eprivate%20static%20void%20ApplyProvisioningTemplate(ConsoleColor%20defaultForeground%2C%20string%20targetWebUrl%2C%20string%20userName%2C%20SecureString%20pwd%2C%20ProvisioningTemplate%20template)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Eprivate%20static%20string%20GetInput(string%20label%2C%20bool%20isPassword%2C%20ConsoleColor%20defaultForeground)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EConsole.ForegroundColor%20%3D%20ConsoleColor.Green%3B%3CBR%20%2F%3EConsole.WriteLine(%22%7B0%7D%20%3A%20%22%2C%20label)%3B%3CBR%20%2F%3EConsole.ForegroundColor%20%3D%20defaultForeground%3B%3C%2FP%3E%3CP%3Estring%20value%20%3D%20%22%22%3B%3C%2FP%3E%3CP%3Efor%20(ConsoleKeyInfo%20keyInfo%20%3D%20Console.ReadKey(true)%3B%20keyInfo.Key%20!%3D%20ConsoleKey.Enter%3B%20keyInfo%20%3D%20Console.ReadKey(true))%3CBR%20%2F%3E%7B%3CBR%20%2F%3Eif%20(keyInfo.Key%20%3D%3D%20ConsoleKey.Backspace)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Eif%20(value.Length%20%26gt%3B%200)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Evalue%20%3D%20value.Remove(value.Length%20-%201)%3B%3CBR%20%2F%3EConsole.SetCursorPosition(Console.CursorLeft%20-%201%2C%20Console.CursorTop)%3B%3CBR%20%2F%3EConsole.Write(%22%20%22)%3B%3CBR%20%2F%3EConsole.SetCursorPosition(Console.CursorLeft%20-%201%2C%20Console.CursorTop)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3Eelse%20if%20(keyInfo.Key%20!%3D%20ConsoleKey.Enter)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Eif%20(isPassword)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EConsole.Write(%22*%22)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3Eelse%3CBR%20%2F%3E%7B%3CBR%20%2F%3EConsole.Write(keyInfo.KeyChar)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3Evalue%20%2B%3D%20keyInfo.KeyChar%3B%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E%7D%3CBR%20%2F%3EConsole.WriteLine(%22%22)%3B%3C%2FP%3E%3CP%3Ereturn%20value%3B%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3Eprivate%20static%20ProvisioningTemplate%20GetProvisioningTemplate(ConsoleColor%20defaultForeground%2C%20string%20webUrl%2C%20string%20userName%2C%20SecureString%20pwd)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Eusing%20(var%20ctx%20%3D%20new%20ClientContext(webUrl))%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%2F%2F%20ctx.Credentials%20%3D%20new%20NetworkCredentials(userName%2C%20pwd)%3B%3CBR%20%2F%3Ectx.Credentials%20%3D%20new%20SharePointOnlineCredentials(userName%2C%20pwd)%3B%3CBR%20%2F%3Ectx.RequestTimeout%20%3D%20Timeout.Infinite%3B%3C%2FP%3E%3CP%3E%2F%2F%20Just%20to%20output%20the%20site%20details%3CBR%20%2F%3EWeb%20web%20%3D%20ctx.Web%3B%3CBR%20%2F%3Ectx.Load(web%2C%20w%20%3D%26gt%3B%20w.Title)%3B%3CBR%20%2F%3Ectx.ExecuteQueryRetry()%3B%3C%2FP%3E%3CP%3EConsole.ForegroundColor%20%3D%20ConsoleColor.White%3B%3CBR%20%2F%3EConsole.WriteLine(%22Your%20site%20title%20is%3A%22%20%2B%20ctx.Web.Title)%3B%3CBR%20%2F%3EConsole.ForegroundColor%20%3D%20defaultForeground%3B%3C%2FP%3E%3CP%3EProvisioningTemplateCreationInformation%20ptci%3CBR%20%2F%3E%3D%20new%20ProvisioningTemplateCreationInformation(ctx.Web)%3B%3C%2FP%3E%3CP%3E%2F%2F%20Create%20FileSystemConnector%20to%20store%20a%20temporary%20copy%20of%20the%20template%3CBR%20%2F%3Eptci.FileConnector%20%3D%20new%20FileSystemConnector(%40%22c%3A%5Ctemp%5Cpnpprovisioningdemo%22%2C%20%22%22)%3B%3CBR%20%2F%3Eptci.PersistComposedLookFiles%20%3D%20true%3B%3CBR%20%2F%3Eptci.ProgressDelegate%20%3D%20delegate%20(String%20message%2C%20Int32%20progress%2C%20Int32%20total)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%2F%2F%20Only%20to%20output%20progress%20for%20console%20UI%3CBR%20%2F%3EConsole.WriteLine(%22%7B0%3A00%7D%2F%7B1%3A00%7D%20-%20%7B2%7D%22%2C%20progress%2C%20total%2C%20message)%3B%3CBR%20%2F%3E%7D%3B%3C%2FP%3E%3CP%3E%2F%2F%20Execute%20actual%20extraction%20of%20the%20template%3CBR%20%2F%3EProvisioningTemplate%20template%20%3D%20ctx.Web.GetProvisioningTemplate(ptci)%3B%3C%2FP%3E%3CP%3E%2F%2F%20We%20can%20serialize%20this%20template%20to%20save%20and%20reuse%20it%3CBR%20%2F%3E%2F%2F%20Optional%20step%3CBR%20%2F%3EXMLTemplateProvider%20provider%20%3D%3CBR%20%2F%3Enew%20XMLFileSystemTemplateProvider(%40%22c%3A%5Ctemp%5Cpnpprovisioningdemo%22%2C%20%22%22)%3B%3CBR%20%2F%3Eprovider.SaveAs(template%2C%20%22PnPProvisioningDemo.xml%22)%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EProvisioningTemplate%20temp%3D%20ctx.Web.GetProvisioningTemplate(ptci)%3B%3C%2FP%3E%3CP%3E%2F%2F%20ctx.Credentials%20%3D%20new%20NetworkCredentials(userName%2C%20pwd)%3B%3CBR%20%2F%3Ectx.Credentials%20%3D%20new%20SharePointOnlineCredentials(userName%2C%20pwd)%3B%3CBR%20%2F%3Ectx.RequestTimeout%20%3D%20Timeout.Infinite%3B%3CBR%20%2F%3EProvisioningTemplateApplyingInformation%20ptai%3CBR%20%2F%3E%3D%20new%20ProvisioningTemplateApplyingInformation()%3B%3CBR%20%2F%3Eptai.ProgressDelegate%20%3D%20delegate%20(String%20message%2C%20Int32%20progress%2C%20Int32%20total)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EConsole.WriteLine(%22%7B0%3A00%7D%2F%7B1%3A00%7D%20-%20%7B2%7D%22%2C%20progress%2C%20total%2C%20message)%3B%3CBR%20%2F%3E%7D%3B%3C%2FP%3E%3CP%3E%2F%2F%20Associate%20file%20connector%20for%20assets%3CBR%20%2F%3EFileSystemConnector%20connector%20%3D%20new%20FileSystemConnector(%40%22c%3A%5Ctemp%5Cpnpprovisioningdemo%22%2C%20%22%22)%3B%3CBR%20%2F%3Etemplate.Connector%20%3D%20connector%3B%3C%2FP%3E%3CP%3E%2F%2F%20Because%20the%20template%20is%20actual%20object%2C%20we%20can%20modify%20this%20using%20code%20as%20needed%3CBR%20%2F%3Etemplate.Lists.Add(new%20ListInstance()%3CBR%20%2F%3E%7B%3CBR%20%2F%3ETitle%20%3D%20%22PnP%20Sample%20Contacts%22%2C%3CBR%20%2F%3EUrl%20%3D%20%22lists%2FPnPContacts%22%2C%3CBR%20%2F%3ETemplateType%20%3D%20(Int32)ListTemplateType.Contacts%2C%3CBR%20%2F%3EEnableAttachments%20%3D%20true%3CBR%20%2F%3E%7D)%3B%3C%2FP%3E%3CP%3Eweb.ApplyProvisioningTemplate(template%2C%20ptai)%3B%3C%2FP%3E%3CP%3Ereturn%20template%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777367%22%20slang%3D%22en-US%22%3ERe%3A%20Save%20as%20template%20again!!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777367%22%20slang%3D%22en-US%22%3EI%20can%20see%20from%20your%20code%20you%20don't%20do%20anything%20to%20your%20destination%20site.%20Your%20ApplyProvisioningTemplate%20method%20is%20empty%20and%20in%20your%20GetProvisioningTemplate%20method%20you're%20applying%20a%20modified%20template%20to%20the%20web%20you've%20specified%20in%20templateWebUrl.%20So%20it%20seems%20(haven't%20run%20your%20code)%20your%20source%20site%20is%20ultimately%20changed%20to%20include%20a%20List%20with%20Title%20%22PnP%20Sample%20Contacts%22.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777436%22%20slang%3D%22en-US%22%3ERe%3A%20Save%20as%20template%20again!!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777436%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1803%22%20target%3D%22_blank%22%3E%40Paul%20Pascha%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ewhat%20to%20write%20in%20my%26nbsp%3B%3CSPAN%3EApplyProvisioningTemplate%3F%3CBR%20%2F%3Ethe%20code%20opens%20up%20a%20console%20and%20it%20gets%20the%20template%20and%20target%20site%20and%20it%20works%20on%20them.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Ebut%20what%20happens%20at%20the%20end%20is%20exactly%20what%20u%20said.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777449%22%20slang%3D%22en-US%22%3ERe%3A%20Save%20as%20template%20again!!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777449%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F373287%22%20target%3D%22_blank%22%3E%40Soheilsd%3C%2FA%3E%26nbsp%3Bwondering%20if%20you%20have%20used%20site%20designs.%20We%20can%20configure%20new%20sites%20based%20on%20templates.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fdeclarative-customization%2Fsite-design-overview%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fdev%2Fdeclarative-customization%2Fsite-design-overview%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-777480%22%20slang%3D%22en-US%22%3ERe%3A%20Save%20as%20template%20again!!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-777480%22%20slang%3D%22en-US%22%3ESomething%20like%20this%20should%20work%3A%3CBR%20%2F%3E%3CBR%20%2F%3Eprivate%20static%20void%20ApplyProvisioningTemplate(ConsoleColor%20defaultForeground%2C%20string%20targetWebUrl%2C%20string%20userName%2C%20SecureString%20pwd%2C%20ProvisioningTemplate%20template)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Eusing%20(var%20ctx%20%3D%20new%20ClientContext(targetWebUrl))%3CBR%20%2F%3E%7B%3CBR%20%2F%3Ectx.Credentials%20%3D%20new%20SharePointOnlineCredentials(userName%2C%20pwd)%3B%3CBR%20%2F%3E%3CBR%20%2F%3Ectx.Web.ApplyProvisioningTemplate(template)%3B%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-778847%22%20slang%3D%22en-US%22%3ERe%3A%20Save%20as%20template%20again!!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-778847%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1803%22%20target%3D%22_blank%22%3E%40Paul%20Pascha%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethanks%20a%20lot%20it%20works%20perfectly.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-809482%22%20slang%3D%22en-US%22%3ERe%3A%20Save%20as%20template%20again!!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-809482%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1803%22%20target%3D%22_blank%22%3E%40Paul%20Pascha%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20my%20code%20were%20just%20doing%20fine%20until%20it%20gets%20this%20error%20at%20the%20end%20of%20it%20in%20the%26nbsp%3B%26nbsp%3B%22web.ApplyProvisioningTemplate(template%2C%20ptai)%3B%22%20section%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eerror%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%E2%80%9CThe%20formula%20refers%20to%20a%20column%20that%20does%20not%20exist.%20Check%20the%20formula%20for%20spelling%20mistakes%20or%20change%20the%20non-existing%20column%20to%20an%20existing%20column.%E2%80%9D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ewhat%20should%20I%20do%20with%20that%3F%3F!!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-824000%22%20slang%3D%22en-US%22%3ERe%3A%20Save%20as%20template%20again!!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-824000%22%20slang%3D%22en-US%22%3EThis%20is%20an%20error%20related%20to%20a%20Calculated%20Field%20in%20your%20template.%20As%20the%20error%20says%20it%20refers%20to%20a%20column%20that%20doesn't%20exist.%20You%20should%20check%20the%20name%20of%20your%20column%20and%20make%20sure%20it%20exists%20before%20your%20calculated%20field%20is%20created.%3CBR%20%2F%3E%3CBR%20%2F%3EDoes%20this%20make%20sense%3F%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

I am wondering if I can save a team site on SharePoint online as a template in a simple way??!!

not using the PnP provisioning its too complicated...

or is there a complete example of pnp code that works in visual studio??

11 Replies
Highlighted

Do you want to save a template from modern or classic team site?

What needs to be included in the template?

Highlighted
Best Response confirmed by Beau Cameron (MVP)
Solution
Just wondering, why do you consider the PnP provisioning to be too complicated? You are looking for a solution from a Developer's perspective. With the PnP NuGet packages installed in your project, "Save as template" is basically just 1 line of code (web.GetProvisioningTemplate). Applying a template to another site is also just 1 line of code (web.ApplyProvisioningTemplate).

A sample console application using the above techniques can be found here:
https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/provisioning-console-application-s...

For SharePoint Online modern sites you can make use of Site Designs and Site Scripts. You could combine these with PnP provisioning as well.
https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/site-design-overview

Hope this helps!
Highlighted
modern team site
Highlighted

hi@Paul Pascha 

this is my code

It works fine when i press start and go to the end of it but my destination site doesn't change...!!

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Framework.Provisioning.Connectors;
using OfficeDevPnP.Core.Framework.Provisioning.Model;
using OfficeDevPnP.Core.Framework.Provisioning.ObjectHandlers;
using OfficeDevPnP.Core.Framework.Provisioning.Providers.Xml;
using System.Net;
using System.Security;
using System.Threading;
namespace Program
{
class Program
{
static void Main(string[] args)
{
ConsoleColor defaultForeground = Console.ForegroundColor;

// Collect information
string templateWebUrl = GetInput("Enter the URL of the template site: ", false, defaultForeground);
string targetWebUrl = GetInput("Enter the URL of the target site: ", false, defaultForeground);
string userName = GetInput("Enter your user name:", false, defaultForeground);
string pwdS = GetInput("Enter your password:", true, defaultForeground);
SecureString pwd = new SecureString();
foreach (char c in pwdS.ToCharArray()) pwd.AppendChar(c);

// GET the template from existing site and serialize
// Serializing the template for later reuse is optional
ProvisioningTemplate template = GetProvisioningTemplate(defaultForeground, templateWebUrl, userName, pwd);

// APPLY the template to new site from
ApplyProvisioningTemplate(defaultForeground, targetWebUrl, userName, pwd, template);

// Pause and modify the UI to indicate that the operation is complete
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("We're done. Press Enter to continue.");
Console.ReadLine();
}

private static void ApplyProvisioningTemplate(ConsoleColor defaultForeground, string targetWebUrl, string userName, SecureString pwd, ProvisioningTemplate template)
{

}

private static string GetInput(string label, bool isPassword, ConsoleColor defaultForeground)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("{0} : ", label);
Console.ForegroundColor = defaultForeground;

string value = "";

for (ConsoleKeyInfo keyInfo = Console.ReadKey(true); keyInfo.Key != ConsoleKey.Enter; keyInfo = Console.ReadKey(true))
{
if (keyInfo.Key == ConsoleKey.Backspace)
{
if (value.Length > 0)
{
value = value.Remove(value.Length - 1);
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.Write(" ");
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
}
}
else if (keyInfo.Key != ConsoleKey.Enter)
{
if (isPassword)
{
Console.Write("*");
}
else
{
Console.Write(keyInfo.KeyChar);
}
value += keyInfo.KeyChar;

}

}
Console.WriteLine("");

return value;
}

private static ProvisioningTemplate GetProvisioningTemplate(ConsoleColor defaultForeground, string webUrl, string userName, SecureString pwd)
{
using (var ctx = new ClientContext(webUrl))
{
// ctx.Credentials = new NetworkCredentials(userName, pwd);
ctx.Credentials = new SharePointOnlineCredentials(userName, pwd);
ctx.RequestTimeout = Timeout.Infinite;

// Just to output the site details
Web web = ctx.Web;
ctx.Load(web, w => w.Title);
ctx.ExecuteQueryRetry();

Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Your site title is:" + ctx.Web.Title);
Console.ForegroundColor = defaultForeground;

ProvisioningTemplateCreationInformation ptci
= new ProvisioningTemplateCreationInformation(ctx.Web);

// Create FileSystemConnector to store a temporary copy of the template
ptci.FileConnector = new FileSystemConnector(@"c:\temp\pnpprovisioningdemo", "");
ptci.PersistComposedLookFiles = true;
ptci.ProgressDelegate = delegate (String message, Int32 progress, Int32 total)
{
// Only to output progress for console UI
Console.WriteLine("{0:00}/{1:00} - {2}", progress, total, message);
};

// Execute actual extraction of the template
ProvisioningTemplate template = ctx.Web.GetProvisioningTemplate(ptci);

// We can serialize this template to save and reuse it
// Optional step
XMLTemplateProvider provider =
new XMLFileSystemTemplateProvider(@"c:\temp\pnpprovisioningdemo", "");
provider.SaveAs(template, "PnPProvisioningDemo.xml");


ProvisioningTemplate temp= ctx.Web.GetProvisioningTemplate(ptci);

// ctx.Credentials = new NetworkCredentials(userName, pwd);
ctx.Credentials = new SharePointOnlineCredentials(userName, pwd);
ctx.RequestTimeout = Timeout.Infinite;
ProvisioningTemplateApplyingInformation ptai
= new ProvisioningTemplateApplyingInformation();
ptai.ProgressDelegate = delegate (String message, Int32 progress, Int32 total)
{
Console.WriteLine("{0:00}/{1:00} - {2}", progress, total, message);
};

// Associate file connector for assets
FileSystemConnector connector = new FileSystemConnector(@"c:\temp\pnpprovisioningdemo", "");
template.Connector = connector;

// Because the template is actual object, we can modify this using code as needed
template.Lists.Add(new ListInstance()
{
Title = "PnP Sample Contacts",
Url = "lists/PnPContacts",
TemplateType = (Int32)ListTemplateType.Contacts,
EnableAttachments = true
});

web.ApplyProvisioningTemplate(template, ptai);

return template;


}


}

}

}

 

Highlighted
I can see from your code you don't do anything to your destination site. Your ApplyProvisioningTemplate method is empty and in your GetProvisioningTemplate method you're applying a modified template to the web you've specified in templateWebUrl. So it seems (haven't run your code) your source site is ultimately changed to include a List with Title "PnP Sample Contacts".
Highlighted

@Paul Pascha 

what to write in my ApplyProvisioningTemplate?
the code opens up a console and it gets the template and target site and it works on them.

but what happens at the end is exactly what u said. 

Highlighted

@Soheilsd wondering if you have used site designs. We can configure new sites based on templates.

https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/site-design-overview

Highlighted
Something like this should work:

private static void ApplyProvisioningTemplate(ConsoleColor defaultForeground, string targetWebUrl, string userName, SecureString pwd, ProvisioningTemplate template)
{
using (var ctx = new ClientContext(targetWebUrl))
{
ctx.Credentials = new SharePointOnlineCredentials(userName, pwd);

ctx.Web.ApplyProvisioningTemplate(template);
}
}
Highlighted

@Paul Pascha 

thanks a lot it works perfectly.

Highlighted

@Paul Pascha 

 

Hi my code were just doing fine until it gets this error at the end of it in the  "web.ApplyProvisioningTemplate(template, ptai);" section:

 

error code:

 “The formula refers to a column that does not exist. Check the formula for spelling mistakes or change the non-existing column to an existing column.”

 

what should I do with that??!!

Highlighted
This is an error related to a Calculated Field in your template. As the error says it refers to a column that doesn't exist. You should check the name of your column and make sure it exists before your calculated field is created.

Does this make sense?