Guest User

Untitled

a guest
Jan 29th, 2018
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. ptci.HandlersToProcess = Handlers.All;
  2.  
  3. static void Main(string[] args)
  4. {
  5.  
  6. //testSPConnection();
  7. ConsoleColor defaultForeground = Console.ForegroundColor;
  8. //collect connection info
  9. string templateWebUrl = "https://sharepoint.com/templateSite";
  10. string targetWebUrl = "https://sharepoint.com/siteToApplyTemplateTo";
  11.  
  12. string userName = "user@email.com";
  13. string pwdS = GetInput("Enter your password", true, defaultForeground);
  14. SecureString pwd = new SecureString();
  15. foreach (char c in pwdS.ToCharArray()) pwd.AppendChar(c);
  16.  
  17. //get template from existing site and serialise the bugger
  18. ProvisioningTemplate template = GetProvisioningTemplate(defaultForeground, templateWebUrl, userName, pwd);
  19.  
  20. //apply it to target site
  21. ApplyProvisioningTemplate(defaultForeground, targetWebUrl, userName, pwd, template);
  22.  
  23. Console.ForegroundColor = ConsoleColor.Green;
  24. Console.WriteLine("Success!! :D Press enter to continue");
  25. Console.ReadLine();
  26.  
  27. }
  28.  
  29.  
  30.  
  31. private static SecureString GetPasswordFromString(string password)
  32. {
  33.  
  34. SecureString securePassword = new SecureString();
  35.  
  36. foreach (char c in password.ToCharArray())
  37. {
  38. securePassword.AppendChar(c);
  39. }
  40. return securePassword;
  41. }
  42.  
  43. private static string GetInput(string label, bool isPassword, ConsoleColor defaultForeground)
  44. {
  45. Console.ForegroundColor = ConsoleColor.Green;
  46. Console.WriteLine("{0} : ", label);
  47. Console.ForegroundColor = defaultForeground;
  48.  
  49. string value = "";
  50.  
  51. for (ConsoleKeyInfo keyInfo = Console.ReadKey(true); keyInfo.Key != ConsoleKey.Enter; keyInfo = Console.ReadKey(true))
  52. {
  53. if (keyInfo.Key == ConsoleKey.Backspace)
  54. {
  55. if (value.Length > 0)
  56. {
  57. value = value.Remove(value.Length - 1);
  58. Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
  59. Console.Write(" ");
  60. Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
  61. }
  62. }
  63. else if (keyInfo.Key != ConsoleKey.Enter)
  64. {
  65. if (isPassword)
  66. {
  67. Console.Write("*");
  68. }
  69. else
  70. {
  71. Console.Write(keyInfo.KeyChar);
  72. }
  73. value += keyInfo.KeyChar;
  74.  
  75. }
  76.  
  77. }
  78. Console.WriteLine("");
  79.  
  80. return value;
  81. }
  82.  
  83. private static ProvisioningTemplate GetProvisioningTemplate(ConsoleColor defaultForeground, string webUrl, string userName, SecureString pwd)
  84. {
  85. using (var ctx = new ClientContext(webUrl))
  86. {
  87. // ctx.Credentials = new NetworkCredentials(userName, pwd);
  88. ctx.Credentials = new SharePointOnlineCredentials(userName, pwd);
  89. ctx.RequestTimeout = Timeout.Infinite;
  90.  
  91. // Just to output the site details
  92. Web web = ctx.Web;
  93. ctx.Load(web, w => w.Title);
  94. ctx.ExecuteQuery();
  95.  
  96. Console.ForegroundColor = ConsoleColor.White;
  97. Console.WriteLine("Your site title is" + ctx.Web.Title);
  98. Console.ForegroundColor = defaultForeground;
  99.  
  100. ProvisioningTemplateCreationInformation ptci
  101. = new ProvisioningTemplateCreationInformation(ctx.Web);
  102.  
  103. // Create FileSystemConnector to store a temporary copy of the template
  104. ptci.FileConnector = new FileSystemConnector(@"c:pnpDemo", "");
  105. ptci.IncludeNativePublishingFiles = true;
  106. ptci.PersistBrandingFiles = true;
  107. ptci.PersistPublishingFiles = true;
  108. ptci.HandlersToProcess = Handlers.All;
  109. ptci.ProgressDelegate = delegate (String message, Int32 progress, Int32 total)
  110. {
  111. // Only to output progress for console UI
  112. Console.WriteLine("{0:00}/{1:00} - {2}", progress, total, message);
  113. };
  114.  
  115. // Execute actual extraction of the template
  116. ProvisioningTemplate template = ctx.Web.GetProvisioningTemplate(ptci);
  117.  
  118. // We can serialize this template to save and reuse it
  119. // Optional step
  120. XMLTemplateProvider provider =
  121. new XMLFileSystemTemplateProvider(@"c:pnpDemo", "");
  122. provider.SaveAs(template, "PnPProvisioningDemo.xml");
  123.  
  124. return template;
  125. }
  126. }
  127.  
  128. private static void ApplyProvisioningTemplate(ConsoleColor defaultForeground, string targetWebUrl, string userName, SecureString pwd, ProvisioningTemplate template)
  129. {
  130. using (var ctx = new ClientContext(targetWebUrl))
  131. {
  132. // ctx.Credentials = new NetworkCredentials(userName, pwd);
  133. ctx.Credentials = new SharePointOnlineCredentials(userName, pwd);
  134. ctx.RequestTimeout = Timeout.Infinite;
  135. Web web = ctx.Web;
  136.  
  137. ProvisioningTemplateApplyingInformation ptai
  138. = new ProvisioningTemplateApplyingInformation();
  139. ptai.ProgressDelegate = delegate (String message, Int32 progress, Int32 total)
  140. {
  141. Console.WriteLine("{0:00}/{1:00} - {2}", progress, total, message);
  142. };
  143.  
  144. // Associate file connector for assets
  145. FileSystemConnector connector = new FileSystemConnector(@"c:pnpDemo", "");
  146. template.Connector = connector;
  147. web.ApplyProvisioningTemplate(template, ptai);
  148.  
  149. }
  150. }
Add Comment
Please, Sign In to add comment