Advertisement
Guest User

Untitled

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