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;
- using OfficeDevPnP.Core.Entities;
- 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
- Console.WriteLine($"Adding webpart");
- var webpartxml =
- "<?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> ";
- var myWebPart = new WebPartEntity { WebPartXml = webpartxml };
- myWebPart.WebPartTitle = "My WebPart";
- myWebPart.WebPartIndex = 2;
- myWebPart.WebPartZone = "Header";
- var serverUrl = ctx.Web.Url;
- var serverRelativePageUrl = $"/sites/StrangeTeam/{url}/sitepages/home.aspx";
- ctx.Web.AddWebPartToWebPartPage(serverRelativePageUrl, myWebPart);
- ctx.ExecuteQuery();
- Console.WriteLine("Webpart added!");
- }
- 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();
- }
- }
- }
Add Comment
Please, Sign In to add comment