Advertisement
Guest User

Untitled

a guest
Aug 15th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. using ITR.SharePoint.Client.CSOM;
  2. using Microsoft.SharePoint.Client;
  3. using Microsoft.SharePoint.Client.WebParts;
  4. using OfficeDevPnP.Core.Entities;
  5. using System;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Web.UI.WebControls.WebParts;
  10.  
  11. namespace SP.DEMO.PROVISIONING
  12. {
  13. class Program
  14. {
  15. private static string username = "alnstrange@alnstrange.onmicrosoft.com";
  16. private static string siteURL = "https://alnstrange.sharepoint.com/sites/provisioning";
  17.  
  18. //Opret SharePoint Team Site
  19. //Opret liste med felter: Site Title, URL, IsProvisioned
  20. //Hent listen ud i koden
  21. //opret subsite på nye elementer
  22. //Ryd forsiden for webparts
  23. //Indsæt en Youtube video på forsiden (efter ejet valg)
  24.  
  25. static void Main(string[] args)
  26. {
  27.  
  28. // Starting with SPClientContext, the constructor requires a URL to the
  29. // server running SharePoint.
  30. Console.WriteLine("Please enter password:");
  31. using (SPClientContext ctx = new SPClientContext(siteURL))
  32. {
  33. ctx.Credentials = new SharePointOnlineCredentials(username, ctx.GetPasswordFromConsoleInput());
  34.  
  35. // The SharePoint web at the URL.
  36. Web web = ctx.Web;
  37.  
  38. ctx.Load(web);
  39. ctx.Load(web, x => x.Lists);
  40.  
  41. ctx.ExecuteQuery();
  42.  
  43. Console.WriteLine(string.Format("Connected to site with title of '{0}'", web.Title));
  44. Console.WriteLine();
  45.  
  46. CamlQuery query = new CamlQuery();
  47.  
  48. List myList = web.Lists.GetByTitle("websitelist");
  49.  
  50. var myListFieldCollection = myList.Fields;
  51.  
  52. ListItemCollection websites = myList.GetItems(query);
  53. ctx.Load<List>(myList);
  54. ctx.Load<ListItemCollection>(websites);
  55. ctx.Load<FieldCollection>(myListFieldCollection);
  56. ctx.ExecuteQuery();
  57.  
  58. Console.WriteLine("Getting list items...");
  59. Console.WriteLine("-------------------------------");
  60.  
  61. var siteTitleColumnInternalName = myListFieldCollection.First(t => t.Title == "Site Title").InternalName;
  62. var urlColumnInternalName = myListFieldCollection.First(t => t.Title == "URL").InternalName;
  63. var isProvisionedTitleColumnInternalName = myListFieldCollection.First(t => t.Title == "IsProvisioned").InternalName;
  64.  
  65. foreach (var website in websites)
  66. {
  67. var siteTitle = (string)website.FieldValues.First(k => k.Key == siteTitleColumnInternalName).Value;
  68. var url = (string)website.FieldValues.First(k => k.Key == urlColumnInternalName).Value;
  69. var isProvisioned = (bool)website.FieldValues.First(k => k.Key == isProvisionedTitleColumnInternalName).Value;
  70.  
  71. if (isProvisioned == false)
  72. {
  73. Console.WriteLine("Generating subsite from list...");
  74.  
  75. WebCreationInformation creation = new WebCreationInformation();
  76. creation.Title = siteTitle;
  77. creation.Url = url;
  78. Web newWeb = ctx.Web.Webs.Add(creation);
  79. ctx.Load(newWeb, w => w.Title);
  80. website.ParseAndSetFieldValue("IsProvisioned", "true");
  81. website.Update();
  82. ctx.ExecuteQuery();
  83.  
  84. // adds the webpart to the page
  85.  
  86.  
  87. Console.WriteLine("Subsite created! - Title: " + "(" + siteTitle + ") " + "URL: " + "(" + url + ") " + "IsProvisioned: " + "(" + isProvisioned + ")");
  88.  
  89. }
  90.  
  91. else
  92. {
  93. Console.WriteLine("Site Title: " + siteTitle);
  94. Console.WriteLine("URL: " + url);
  95. Console.WriteLine("isProvisioned: " + isProvisioned);
  96. Console.WriteLine("-------------------------------");
  97. }
  98.  
  99. }
  100.  
  101. Console.ReadLine();
  102. }
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement