Guest User

Untitled

a guest
Aug 15th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. using ITR.SharePoint.Client.CSOM;
  2. using Microsoft.SharePoint.Client;
  3. using System;
  4. using System.Linq;
  5. using Microsoft.SharePoint.Client.WebParts;
  6. using OfficeDevPnP.Core.Entities;
  7.  
  8. namespace SP.DEMO.PROVISIONING
  9. {
  10. class Program
  11. {
  12. private static string username = "michaelstrange@michaelstrange.onmicrosoft.com";
  13. private static string siteURL = "https://michaelstrange.sharepoint.com/sites/StrangeTeam";
  14.  
  15. //Opret SharePoint Team Site
  16. //Opret liste med felter: Site Title, URL, IsProvisioned
  17. //Hent listen ud i koden
  18. //opret subsite på nye elementer
  19. //Ryd forsiden for webparts
  20. //Indsæt en Youtube video på forsiden (efter ejet valg)
  21.  
  22. static void Main(string[] args)
  23. {
  24.  
  25. // Starting with SPClientContext, the constructor requires a URL to the
  26. // server running SharePoint.
  27. Console.WriteLine("Please enter password:");
  28. using (SPClientContext ctx = new SPClientContext(siteURL))
  29. {
  30. ctx.Credentials = new SharePointOnlineCredentials(username, ctx.GetPasswordFromConsoleInput());
  31.  
  32. // The SharePoint web at the URL.
  33. Web web = ctx.Web;
  34.  
  35. ctx.Load(web);
  36. ctx.Load(web, x => x.Lists);
  37.  
  38. ctx.ExecuteQuery();
  39.  
  40. Console.WriteLine(string.Format("Connected to site with title of '{0}'", web.Title));
  41. Console.WriteLine();
  42.  
  43. CamlQuery query = new CamlQuery();
  44.  
  45. List myList = web.Lists.GetByTitle("TeamList");
  46.  
  47. var myListFieldCollection = myList.Fields;
  48.  
  49. ListItemCollection websites = myList.GetItems(query);
  50. ctx.Load<List>(myList);
  51. ctx.Load<ListItemCollection>(websites);
  52. ctx.Load<FieldCollection>(myListFieldCollection);
  53. ctx.ExecuteQuery();
  54.  
  55. Console.WriteLine("Getting list items...");
  56. Console.WriteLine("-------------------------------");
  57.  
  58. var siteTitleColumnInternalName =
  59. myListFieldCollection.First(t => t.Title == "Site Title").InternalName;
  60. var urlColumnInternalName = myListFieldCollection.First(t => t.Title == "URL").InternalName;
  61. var isProvisionedTitleColumnInternalName =
  62. myListFieldCollection.First(t => t.Title == "IsProvisioned").InternalName;
  63.  
  64. foreach (var website in websites)
  65. {
  66. var siteTitle = (string) website.FieldValues.First(k => k.Key == siteTitleColumnInternalName).Value;
  67. var url = (string) website.FieldValues.First(k => k.Key == urlColumnInternalName).Value;
  68. var isProvisioned = (bool) website.FieldValues
  69. .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. Console.WriteLine("Subsite created! - Title: " + "(" + siteTitle + ") " + "URL: " + "(" + url +
  85. ") " + "IsProvisioned: " + "(" + isProvisioned + ")");
  86.  
  87.  
  88. //Add webpart
  89. Console.WriteLine($"Adding webpart");
  90.  
  91. var webpartxml =
  92. "<?xml version=\"1.0\" encoding=\"utf-8\"?> <WebPart xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.microsoft.com/WebPart/v2\"> <Title>CSOM Content Editor</Title> <FrameType>None</FrameType> <Description>Allows authors to enter rich text content.</Description> <IsIncluded>true</IsIncluded> <ZoneID>Header</ZoneID> <PartOrder>0</PartOrder> <FrameState>Normal</FrameState> <Height /> <Width /> <AllowRemove>true</AllowRemove> <AllowZoneChange>true</AllowZoneChange> <AllowMinimize>true</AllowMinimize> <AllowConnect>true</AllowConnect> <AllowEdit>true</AllowEdit> <AllowHide>true</AllowHide> <IsVisible>true</IsVisible> <DetailLink /> <HelpLink /> <HelpMode>Modeless</HelpMode> <Dir>Default</Dir> <PartImageSmall /> <MissingAssembly>Cannot import this Web Part.</MissingAssembly> <PartImageLarge>/_layouts/15/images/mscontl.gif</PartImageLarge> <IsIncludedFilter /> <Assembly>Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly> <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName> <ContentLink xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\"></ContentLink>> <Content xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\"> <![CDATA[<iframe width=\"1280\" height=\"720\" src=\"https://www.youtube.com/embed/C9bC9CyuHIo\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe> ]]> </Content> <PartStorage xmlns=\"http://schemas.microsoft.com/WebPart/v2/ContentEditor\" /> </WebPart> ";
  93.  
  94. var myWebPart = new WebPartEntity { WebPartXml = webpartxml };
  95.  
  96. myWebPart.WebPartTitle = "My WebPart";
  97. myWebPart.WebPartIndex = 2;
  98. myWebPart.WebPartZone = "Header";
  99.  
  100. var serverUrl = ctx.Web.Url;
  101.  
  102. var serverRelativePageUrl = $"/sites/StrangeTeam/{url}/sitepages/home.aspx";
  103. ctx.Web.AddWebPartToWebPartPage(serverRelativePageUrl, myWebPart);
  104.  
  105. ctx.ExecuteQuery();
  106. Console.WriteLine("Webpart added!");
  107. }
  108. else
  109. {
  110. Console.WriteLine("Site Title: " + siteTitle);
  111. Console.WriteLine("URL: " + url);
  112. Console.WriteLine("isProvisioned: " + isProvisioned);
  113. Console.WriteLine("-------------------------------");
  114. }
  115.  
  116.  
  117.  
  118.  
  119. }
  120. }
  121.  
  122. Console.WriteLine("Press enter to exit application...");
  123. Console.ReadLine();
  124.  
  125.  
  126. }
  127. }
  128. }
Add Comment
Please, Sign In to add comment