Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using ITR.SharePoint.Client.CSOM;
- using Microsoft.SharePoint.Client;
- using System;
- using System.Linq;
- using Microsoft.SharePoint.Client.WebParts;
- namespace SP.DEMO.PROVISIONING
- {
- class Program
- {
- private static string username = "michaelstrange@michaelstrange.onmicrosoft.com";
- private static string siteURL = "https://michaelstrange.sharepoint.com/sites/StrangeTeam";
- //Opret SharePoint Team Site
- //Opret liste med felter: Site Title, URL, IsProvisioned
- //Hent listen ud i koden
- //opret subsite på nye elementer
- //Ryd forsiden for webparts
- //Indsæt en Youtube video på forsiden (efter ejet valg)
- static void Main(string[] args)
- {
- // Starting with SPClientContext, the constructor requires a URL to the
- // server running SharePoint.
- Console.WriteLine("Please enter password:");
- using (SPClientContext ctx = new SPClientContext(siteURL))
- {
- ctx.Credentials = new SharePointOnlineCredentials(username, ctx.GetPasswordFromConsoleInput());
- // The SharePoint web at the URL.
- Web web = ctx.Web;
- ctx.Load(web);
- ctx.Load(web, x => x.Lists);
- ctx.ExecuteQuery();
- Console.WriteLine(string.Format("Connected to site with title of '{0}'", web.Title));
- Console.WriteLine();
- CamlQuery query = new CamlQuery();
- List myList = web.Lists.GetByTitle("TeamList");
- var myListFieldCollection = myList.Fields;
- ListItemCollection websites = myList.GetItems(query);
- ctx.Load<List>(myList);
- ctx.Load<ListItemCollection>(websites);
- ctx.Load<FieldCollection>(myListFieldCollection);
- ctx.ExecuteQuery();
- Console.WriteLine("Getting list items...");
- Console.WriteLine("-------------------------------");
- var siteTitleColumnInternalName =
- myListFieldCollection.First(t => t.Title == "Site Title").InternalName;
- var urlColumnInternalName = myListFieldCollection.First(t => t.Title == "URL").InternalName;
- var isProvisionedTitleColumnInternalName =
- myListFieldCollection.First(t => t.Title == "IsProvisioned").InternalName;
- foreach (var website in websites)
- {
- var siteTitle = (string) website.FieldValues.First(k => k.Key == siteTitleColumnInternalName).Value;
- var url = (string) website.FieldValues.First(k => k.Key == urlColumnInternalName).Value;
- var isProvisioned = (bool) website.FieldValues
- .First(k => k.Key == isProvisionedTitleColumnInternalName).Value;
- if (isProvisioned == false)
- {
- Console.WriteLine("Generating subsite from list...");
- WebCreationInformation creation = new WebCreationInformation();
- creation.Title = siteTitle;
- creation.Url = url;
- Web newWeb = ctx.Web.Webs.Add(creation);
- ctx.Load(newWeb, w => w.Title);
- website.ParseAndSetFieldValue("IsProvisioned", "true");
- website.Update();
- ctx.ExecuteQuery();
- Console.WriteLine("Subsite created! - Title: " + "(" + siteTitle + ") " + "URL: " + "(" + url +
- ") " + "IsProvisioned: " + "(" + isProvisioned + ")");
- //Add webpart
- var page = ctx.Web.GetFileByUrl($"https://michaelstrange.sharepoint.com/sites/StrangeTeam/{url}/SitePages/Home.aspx");
- var lwp = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
- var webpartxml =
- "<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\"><div data-sp-webpart=\"\" data-sp-webpartdataversion=\"1.0\" data-sp-webpartdata=\"{&quot;id&quot;:&quot;490d7c76-1824-45b2-9de3-676421c997fa&quot;,&quot;instanceId&quot;:&quot;e108733f-1474-408d-a137-0796bf092fba&quot;,&quot;title&quot;:&quot;Embed&quot;,&quot;description&quot;:&quot;Embed content from other sites such as Sway, YouTube, Vimeo, and more&quot;,&quot;dataVersion&quot;:&quot;1.0&quot;,&quot;properties&quot;:{&quot;embedCode&quot;:&quot;<iframe width=\\&quot;1280\\&quot; height=\\&quot;720\\&quot; src=\\&quot;https://www.youtube.com/embed/C9bC9CyuHIo\\&quot; frameborder=\\&quot;0\\&quot; allow=\\&quot;autoplay; encrypted-media\\&quot; allowfullscreen=\\&quot;\\&quot;></iframe>&quot;,&quot;cachedEmbedCode&quot;:&quot;<iframe width=\\&quot;1280\\&quot; height=\\&quot;720\\&quot; src=\\&quot;https://www.youtube.com/embed/C9bC9CyuHIo\\&quot; frameborder=\\&quot;0\\&quot; allow=\\&quot;autoplay; encrypted-media\\&quot; allowfullscreen=\\&quot;\\&quot;></iframe>&quot;,&quot;shouldScaleWidth&quot;:true,&quot;tempState&quot;:{}}}\"><div data-sp-componentid=\"\">490d7c76-1824-45b2-9de3-676421c997fa</div><div data-sp-htmlproperties=\"\"></div></div></property>\r\n </properties>\r\n </data>\r\n </webPart>\r\n</webParts>";
- WebPartDefinition wpd = lwp.ImportWebPart(webpartxml);
- lwp.AddWebPart(wpd.WebPart, "Right", 1);
- ctx.ExecuteQuery();
- }
- else
- {
- Console.WriteLine("Site Title: " + siteTitle);
- Console.WriteLine("URL: " + url);
- Console.WriteLine("isProvisioned: " + isProvisioned);
- Console.WriteLine("-------------------------------");
- }
- }
- }
- Console.WriteLine("Press enter to exit application...");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement