Advertisement
Guest User

Untitled

a guest
Aug 15th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.13 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.  
  7. namespace SP.DEMO.PROVISIONING
  8. {
  9.     class Program
  10.     {
  11.         private static string username = "michaelstrange@michaelstrange.onmicrosoft.com";
  12.         private static string siteURL = "https://michaelstrange.sharepoint.com/sites/StrangeTeam";
  13.  
  14.         //Opret SharePoint Team Site
  15.         //Opret liste med felter: Site Title, URL, IsProvisioned
  16.         //Hent listen ud i koden
  17.         //opret subsite på nye elementer
  18.         //Ryd forsiden for webparts
  19.         //Indsæt en Youtube video på forsiden (efter ejet valg)
  20.  
  21.         static void Main(string[] args)
  22.         {
  23.  
  24.             // Starting with SPClientContext, the constructor requires a URL to the
  25.             // server running SharePoint.
  26.             Console.WriteLine("Please enter password:");
  27.             using (SPClientContext ctx = new SPClientContext(siteURL))
  28.             {
  29.                 ctx.Credentials = new SharePointOnlineCredentials(username, ctx.GetPasswordFromConsoleInput());
  30.  
  31.                 // The SharePoint web at the URL.
  32.                 Web web = ctx.Web;
  33.  
  34.                 ctx.Load(web);
  35.                 ctx.Load(web, x => x.Lists);
  36.  
  37.                 ctx.ExecuteQuery();
  38.  
  39.                 Console.WriteLine(string.Format("Connected to site with title of '{0}'", web.Title));
  40.                 Console.WriteLine();
  41.  
  42.                 CamlQuery query = new CamlQuery();
  43.  
  44.                 List myList = web.Lists.GetByTitle("TeamList");
  45.  
  46.                 var myListFieldCollection = myList.Fields;
  47.  
  48.                 ListItemCollection websites = myList.GetItems(query);
  49.                 ctx.Load<List>(myList);
  50.                 ctx.Load<ListItemCollection>(websites);
  51.                 ctx.Load<FieldCollection>(myListFieldCollection);
  52.                 ctx.ExecuteQuery();
  53.  
  54.                 Console.WriteLine("Getting list items...");
  55.                 Console.WriteLine("-------------------------------");
  56.  
  57.                 var siteTitleColumnInternalName =
  58.                     myListFieldCollection.First(t => t.Title == "Site Title").InternalName;
  59.                 var urlColumnInternalName = myListFieldCollection.First(t => t.Title == "URL").InternalName;
  60.                 var isProvisionedTitleColumnInternalName =
  61.                     myListFieldCollection.First(t => t.Title == "IsProvisioned").InternalName;
  62.  
  63.                 foreach (var website in websites)
  64.                 {
  65.                     var siteTitle = (string) website.FieldValues.First(k => k.Key == siteTitleColumnInternalName).Value;
  66.                     var url = (string) website.FieldValues.First(k => k.Key == urlColumnInternalName).Value;
  67.                     var isProvisioned = (bool) website.FieldValues
  68.                         .First(k => k.Key == isProvisionedTitleColumnInternalName).Value;
  69.  
  70.                     if (isProvisioned == false)
  71.                     {
  72.                         Console.WriteLine("Generating subsite from list...");
  73.  
  74.                         WebCreationInformation creation = new WebCreationInformation();
  75.                         creation.Title = siteTitle;
  76.                         creation.Url = url;
  77.                         Web newWeb = ctx.Web.Webs.Add(creation);
  78.                         ctx.Load(newWeb, w => w.Title);
  79.                         website.ParseAndSetFieldValue("IsProvisioned", "true");
  80.                         website.Update();
  81.                         ctx.ExecuteQuery();
  82.  
  83.                         Console.WriteLine("Subsite created! - Title: " + "(" + siteTitle + ") " + "URL: " + "(" + url +
  84.                                           ") " + "IsProvisioned: " + "(" + isProvisioned + ")");
  85.  
  86.  
  87.                         //Add webpart
  88.                         var page = ctx.Web.GetFileByUrl($"https://michaelstrange.sharepoint.com/sites/StrangeTeam/{url}/SitePages/Home.aspx");
  89.                         var lwp = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
  90.  
  91.  
  92.                         var webpartxml =
  93.                             "<webParts>\r\n  <webPart xmlns=\"http://schemas.microsoft.com/WebPart/v3\">\r\n    <metaData>\r\n      <type name=\"Microsoft.SharePoint.WebPartPages.ClientSideWebPart,\r\n        Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c\" />\r\n      <importErrorMessage>Cannot Import WebPart</importErrorMessage>\r\n    </metaData>\r\n    <data>\r\n      <properties>\r\n        <property name=\"Title\" type=\"string\">Embed</property>\r\n        <property name=\"Description\" type=\"string\">Embed content from other sites such as Sway, YouTube, Vimeo, and more</property>\r\n        <property name=\"IconUrl\" type=\"string\"></property>\r\n        <property name=\"ClientSideWebPartId\">490d7c76-1824-45b2-9de3-676421c997fa</property>\r\n        <property name=\"ClientSideWebPartData\" type=\"string\">&lt;div data-sp-webpart=\"\" data-sp-webpartdataversion=\"1.0\" data-sp-webpartdata=\"{&amp;quot;id&amp;quot;:&amp;quot;490d7c76-1824-45b2-9de3-676421c997fa&amp;quot;,&amp;quot;instanceId&amp;quot;:&amp;quot;e108733f-1474-408d-a137-0796bf092fba&amp;quot;,&amp;quot;title&amp;quot;:&amp;quot;Embed&amp;quot;,&amp;quot;description&amp;quot;:&amp;quot;Embed content from other sites such as Sway, YouTube, Vimeo, and more&amp;quot;,&amp;quot;dataVersion&amp;quot;:&amp;quot;1.0&amp;quot;,&amp;quot;properties&amp;quot;:{&amp;quot;embedCode&amp;quot;:&amp;quot;&lt;iframe width=\\&amp;quot;1280\\&amp;quot; height=\\&amp;quot;720\\&amp;quot; src=\\&amp;quot;https://www.youtube.com/embed/C9bC9CyuHIo\\&amp;quot; frameborder=\\&amp;quot;0\\&amp;quot; allow=\\&amp;quot;autoplay; encrypted-media\\&amp;quot; allowfullscreen=\\&amp;quot;\\&amp;quot;&gt;&lt;/iframe&gt;&amp;quot;,&amp;quot;cachedEmbedCode&amp;quot;:&amp;quot;&lt;iframe width=\\&amp;quot;1280\\&amp;quot; height=\\&amp;quot;720\\&amp;quot; src=\\&amp;quot;https://www.youtube.com/embed/C9bC9CyuHIo\\&amp;quot; frameborder=\\&amp;quot;0\\&amp;quot; allow=\\&amp;quot;autoplay; encrypted-media\\&amp;quot; allowfullscreen=\\&amp;quot;\\&amp;quot;&gt;&lt;/iframe&gt;&amp;quot;,&amp;quot;shouldScaleWidth&amp;quot;:true,&amp;quot;tempState&amp;quot;:{}}}\"&gt;&lt;div data-sp-componentid=\"\"&gt;490d7c76-1824-45b2-9de3-676421c997fa&lt;/div&gt;&lt;div data-sp-htmlproperties=\"\"&gt;&lt;/div&gt;&lt;/div&gt;</property>\r\n      </properties>\r\n    </data>\r\n  </webPart>\r\n</webParts>";
  94.  
  95.  
  96.                         WebPartDefinition wpd = lwp.ImportWebPart(webpartxml);
  97.                         lwp.AddWebPart(wpd.WebPart, "Right", 1);
  98.                         ctx.ExecuteQuery();
  99.  
  100.                     }
  101.                     else
  102.                     {
  103.                         Console.WriteLine("Site Title: " + siteTitle);
  104.                         Console.WriteLine("URL: " + url);
  105.                         Console.WriteLine("isProvisioned: " + isProvisioned);
  106.                         Console.WriteLine("-------------------------------");
  107.                     }
  108.  
  109.  
  110.  
  111.  
  112.                 }
  113.             }
  114.  
  115.             Console.WriteLine("Press enter to exit application...");
  116.             Console.ReadLine();
  117.  
  118.  
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement