Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Security;
- using Microsoft.SharePoint.Client;
- using System;
- using System.Configuration;
- using System.Collections.Generic;
- using System.Linq;
- namespace ContentCloudCleaner
- {
- class Program
- {
- private static string user;
- private static string password;
- private static string tenant;
- private static string siteCollectionUrl;
- public static string User
- {
- get
- {
- if (String.IsNullOrEmpty(user))
- {
- user = ConfigurationManager.AppSettings["user"];
- }
- if (String.IsNullOrEmpty(user))
- {
- user = GetInput("User", false);
- }
- return user;
- }
- }
- public static string Office365Tenant
- {
- get
- {
- if (String.IsNullOrEmpty(tenant))
- {
- tenant = ConfigurationManager.AppSettings["Office365Tenant"];
- }
- if (String.IsNullOrEmpty(tenant))
- {
- tenant = GetInput("User", false);
- }
- return tenant;
- }
- }
- public static string SiteCollectionUrl
- {
- get
- {
- if (String.IsNullOrEmpty(siteCollectionUrl))
- {
- siteCollectionUrl = ConfigurationManager.AppSettings["siteCollectionUrl"];
- }
- if (String.IsNullOrEmpty(siteCollectionUrl))
- {
- siteCollectionUrl = GetInput("siteCollectionUrl", false);
- }
- return siteCollectionUrl;
- }
- }
- public static SecureString Password
- {
- get
- {
- if (String.IsNullOrEmpty(password))
- {
- password = ConfigurationManager.AppSettings["password"];
- }
- if (String.IsNullOrEmpty(password))
- {
- password = GetInput("Password", true);
- }
- var secure = new SecureString();
- foreach (char c in password)
- {
- secure.AppendChar(c);
- }
- return secure;
- }
- }
- static void Main(string[] args)
- {
- try
- {
- var siteUrl = SiteCollectionUrl.Replace("{Office365Tenant}", Office365Tenant);
- Console.WriteLine($"Connecting to site {siteUrl}");
- using (var context = new ClientContext(siteUrl))
- {
- context.Credentials = new SharePointOnlineCredentials(User, Password);
- var web = context.Web;
- context.Load(web, w => w.ServerRelativeUrl);
- context.ExecuteQuery();
- web.RecycleBin.DeleteAll();
- DeleteSubweb(context, "Help and requests");
- ClearLists(context);
- SetMasterPage(context, $"{web.ServerRelativeUrl}/_catalogs/masterpage/seattle.master", $"{web.ServerRelativeUrl}/_catalogs/masterpage/seattle.master");
- SetWelcomePage(context, "SitePages/Home.aspx");
- DeleteFolder(context, "SiteAssets/ContentCloud");
- DeletePublishingPages(context);
- DeleteFolder(context, "_catalogs/ContentCloud");
- ClearContentTypesFromGroup(context, "Content Cloud Document Metadata Content Types");
- ClearContentTypesFromGroup(context, "Content Cloud Item Metadata Content Types");
- ClearContentTypesFromGroup(context, "Content Cloud Metadata Content Types");
- ClearContentTypesFromGroup(context, "Withdrawn Content Cloud Document Metadata Content Types");
- ClearContentTypesFromGroup(context, "Content Cloud Content Types");
- ClearContentTypesFromGroup(context, "Content Cloud Pages Content Types");
- ClearContentTypesFromGroup(context, "Admin Support Pages Content Types");
- ClearContentTypesFromGroup(context, "Admin Support Content Types");
- ClearFieldsFromGroup(context, "Admin Support Columns");
- ClearFieldsFromGroup(context, "Content Cloud Columns");
- ClearFieldsFromGroup(context, "Content Cloud Metadata Item Columns");
- web.RecycleBin.DeleteAll();
- CleanupWebPropertyBag(context, web);
- Console.WriteLine("");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error is: " + ex.Message);
- }
- }
- private static void DeletePublishingPages(ClientContext context)
- {
- var contentTypeNamePart = "Content Cloud";
- var pagesLibrary = context.Web.Lists.GetByTitle("Pages");
- context.Load(pagesLibrary,
- l => l.ContentTypes,
- l => l.ContentTypes.Include(ct => ct.Group, ct => ct.StringId),
- l => l.RootFolder,
- l => l.RootFolder.Files,
- l => l.RootFolder.Files
- .Include(f => f.ListItemAllFields.Id,
- f => f.ListItemAllFields,
- f => f.ListItemAllFields.ContentType,
- f => f.ListItemAllFields.ContentType.Group));
- context.ExecuteQuery();
- foreach (var page in pagesLibrary.RootFolder.Files)
- {
- if (page.ListItemAllFields.ContentType.Group.Contains(contentTypeNamePart))
- {
- var item = pagesLibrary.GetItemById(page.ListItemAllFields.Id);
- item.DeleteObject();
- context.ExecuteQuery();
- }
- }
- foreach (var contentType in pagesLibrary.ContentTypes)
- {
- if (contentType.Group.Contains(contentTypeNamePart))
- {
- var contentTypeToDelete = pagesLibrary.ContentTypes.GetById(contentType.StringId);
- contentTypeToDelete.DeleteObject();
- context.ExecuteQuery();
- }
- }
- }
- private static void DeleteSubweb(ClientContext context, string title)
- {
- context.Load(context.Web, w => w.Webs, w => w.Webs.Include(sw => sw.Title));
- context.ExecuteQuery();
- var web = context.Web.Webs.FirstOrDefault(w => w.Title == title);
- if (web != null)
- {
- Console.Write($"Deleting web {title}... ");
- web.DeleteObject();
- context.ExecuteQuery();
- Console.WriteLine("Deleted.");
- }
- }
- private static void SetMasterPage(ClientContext context, string masterUrl, string customMasterUrl)
- {
- Console.Write($"Changing masterpage to {masterUrl} and custom master to {customMasterUrl}... ");
- context.Web.MasterUrl = masterUrl;
- context.Web.CustomMasterUrl = customMasterUrl;
- context.Web.Update();
- context.ExecuteQuery();
- Console.WriteLine("Changed.");
- }
- private static void SetWelcomePage(ClientContext context, string webRelativeUrl)
- {
- context.Load(context.Web, w => w.RootFolder, w => w.RootFolder.WelcomePage);
- context.ExecuteQuery();
- if (!context.Web.RootFolder.WelcomePage.EndsWith(webRelativeUrl))
- {
- Console.Write($"Setting home page to {webRelativeUrl}... ");
- context.Web.RootFolder.WelcomePage = webRelativeUrl;
- context.Web.RootFolder.Update();
- context.ExecuteQuery();
- Console.WriteLine("Changed.");
- }
- }
- private static void DeleteFolder(ClientContext context, string webRelativeUrl)
- {
- try
- {
- Console.Write($"Deleting folder {webRelativeUrl}... ");
- var folder = context.Web.GetFolderByServerRelativeUrl(webRelativeUrl);
- folder.DeleteObject();
- context.ExecuteQuery();
- Console.WriteLine("Deleted.");
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Skipping folder {webRelativeUrl} because of {ex.Message}");
- }
- }
- /// <summary>
- /// Gets the password input from the console window
- /// </summary>
- /// <returns>the entered string</returns>
- private static string GetInput(string label, bool isPassword)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write("{0} : ", label);
- Console.ForegroundColor = ConsoleColor.Gray;
- string strPwd = "";
- for (ConsoleKeyInfo keyInfo = Console.ReadKey(true); keyInfo.Key != ConsoleKey.Enter; keyInfo = Console.ReadKey(true))
- {
- if (keyInfo.Key == ConsoleKey.Backspace)
- {
- if (strPwd.Length > 0)
- {
- strPwd = strPwd.Remove(strPwd.Length - 1);
- Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
- Console.Write(" ");
- Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
- }
- }
- else if (keyInfo.Key != ConsoleKey.Enter)
- {
- if (isPassword)
- {
- Console.Write("*");
- }
- else
- {
- Console.Write(keyInfo.KeyChar);
- }
- strPwd += keyInfo.KeyChar;
- }
- }
- Console.WriteLine("");
- return strPwd;
- }
- private static void ClearLists(ClientContext context)
- {
- var listNames = new List<string>();
- Web web = context.Web;
- context.Load(web.Lists,
- lists => lists.Include(list => list.Title,
- list => list.Id));
- context.ExecuteQuery();
- Console.ForegroundColor = ConsoleColor.White;
- foreach (List list in web.Lists)
- {
- if (list.Title.ToLower().StartsWith("content cloud") || list.Title.ToLower().StartsWith("withdrawn content cloud"))
- {
- listNames.Add(list.Title);
- }
- }
- listNames.ForEach(listName =>
- {
- Console.WriteLine("Deleting list : " + listName);
- List list = web.Lists.GetByTitle(listName);
- list.DeleteObject();
- context.ExecuteQuery();
- });
- web.RecycleBin.DeleteAll();
- }
- private static void ClearContentTypesFromGroup(ClientContext context, string group)
- {
- var ctNames = new List<string>();
- Console.WriteLine("Deleting content types from group : " + group);
- Web web = context.Web;
- context.Load(web.ContentTypes,
- contentTypes => contentTypes.Include(contentType => contentType.Group,
- contentType => contentType.Id, contentType => contentType.Name));
- context.ExecuteQuery();
- Console.ForegroundColor = ConsoleColor.White;
- foreach (ContentType ct in web.ContentTypes)
- {
- if (ct.Group.ToLower().Equals(group.ToLower()))
- {
- ctNames.Add(ct.Id.StringValue);
- }
- }
- ctNames.ForEach(ctName =>
- {
- try
- {
- Console.WriteLine("Deleting content type: " + ctName);
- ContentType ct = web.ContentTypes.GetById(ctName);
- ct.DeleteObject();
- context.ExecuteQuery();
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Skipping content type {ctName} because of {ex.Message}");
- }
- });
- web.RecycleBin.DeleteAll();
- }
- private static void ClearFieldsFromGroup(ClientContext context, string group)
- {
- var fieldIds = new List<string>();
- Console.WriteLine("Deleting fields from group : " + group);
- Web web = context.Web;
- context.Load(web.Fields,
- fields => fields.Include(field => field.Group,
- field => field.Id, field => field.Title));
- context.ExecuteQuery();
- Console.ForegroundColor = ConsoleColor.White;
- foreach (Field f in web.Fields)
- {
- if (f.Group.ToLower().Equals(group.ToLower()))
- {
- fieldIds.Add(f.Id.ToString());
- }
- }
- fieldIds.ForEach(field =>
- {
- try
- {
- Console.WriteLine("Deleting field type: " + field);
- Field f = web.Fields.GetById(new Guid(field));
- f.DeleteObject();
- context.ExecuteQuery();
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Skipping field {field} because of {ex.Message}");
- }
- });
- web.RecycleBin.DeleteAll();
- }
- private static void CleanupWebPropertyBag(ClientContext clientContext, Web web)
- {
- Console.WriteLine("Cleaning up web property bag from ItemChangeKey properties");
- clientContext.Load(web, w => w.AllProperties);
- clientContext.ExecuteQuery();
- var contentCloudPropertyKeys =
- web.AllProperties.FieldValues.Where(kv => kv.Key.StartsWith("ItemChangeKey") || kv.Key.StartsWith("ContentCloudLatestReferenceNumber"))
- .Select(fv => fv.Key)
- .ToList();
- foreach (var key in contentCloudPropertyKeys)
- {
- web.AllProperties[key] = null;
- Console.WriteLine("Deleting property: " + key);
- }
- web.Update();
- clientContext.ExecuteQuery();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement