Advertisement
Guest User

Untitled

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