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;
- namespace SP.DEMO.PROVISIONING
- {
- class Program {
- private static string username = "alnstrange@alnstrange.onmicrosoft.com";
- private static string siteURL = "https://alnstrange.sharepoint.com/sites/provisioning";
- //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("websitelist");
- 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 + ")");
- }
- else
- {
- Console.WriteLine("Site Title: " + siteTitle);
- Console.WriteLine("URL: " + url);
- Console.WriteLine("isProvisioned: " + isProvisioned);
- Console.WriteLine("-------------------------------");
- }
- }
- Console.ReadLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement