Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.76 KB | None | 0 0
  1. using System.Security;
  2. using Microsoft.SharePoint.Client;
  3. using System;
  4. using System.Configuration;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7.  
  8. namespace ContentCloudCleaner
  9. {
  10.     class Program
  11.     {
  12.         private static string user;
  13.         private static string password;
  14.         private static string tenant;
  15.         private static string siteCollectionUrl;
  16.  
  17.  
  18.         public static string User
  19.         {
  20.             get
  21.             {
  22.                 if (String.IsNullOrEmpty(user))
  23.                 {
  24.                     user = ConfigurationManager.AppSettings["user"];
  25.                 }
  26.                 if (String.IsNullOrEmpty(user))
  27.                 {
  28.                     user = GetInput("User", false);
  29.                 }
  30.                 return user;
  31.             }
  32.         }
  33.  
  34.         public static string Office365Tenant
  35.         {
  36.             get
  37.             {
  38.                 if (String.IsNullOrEmpty(tenant))
  39.                 {
  40.                     tenant = ConfigurationManager.AppSettings["Office365Tenant"];
  41.                 }
  42.                 if (String.IsNullOrEmpty(tenant))
  43.                 {
  44.                     tenant = GetInput("User", false);
  45.                 }
  46.                 return tenant;
  47.             }
  48.         }
  49.  
  50.         public static string SiteCollectionUrl
  51.         {
  52.             get
  53.             {
  54.                 if (String.IsNullOrEmpty(siteCollectionUrl))
  55.                 {
  56.                     siteCollectionUrl = ConfigurationManager.AppSettings["siteCollectionUrl"];
  57.                 }
  58.                 if (String.IsNullOrEmpty(siteCollectionUrl))
  59.                 {
  60.                     siteCollectionUrl = GetInput("siteCollectionUrl", false);
  61.                 }
  62.                 return siteCollectionUrl;
  63.             }
  64.         }
  65.  
  66.         public static SecureString Password
  67.         {
  68.             get
  69.             {
  70.                 if (String.IsNullOrEmpty(password))
  71.                 {
  72.                     password = ConfigurationManager.AppSettings["password"];
  73.                 }
  74.                 if (String.IsNullOrEmpty(password))
  75.                 {
  76.                     password = GetInput("Password", true);
  77.                 }
  78.  
  79.                 var secure = new SecureString();
  80.                 foreach (char c in password)
  81.                 {
  82.                     secure.AppendChar(c);
  83.                 }
  84.  
  85.                 return secure;
  86.             }
  87.         }
  88.  
  89.         static void Main(string[] args)
  90.         {
  91.             try
  92.             {
  93.                 var siteUrl = SiteCollectionUrl.Replace("{Office365Tenant}", Office365Tenant);
  94.                 Console.WriteLine($"Connecting to site {siteUrl}");
  95.                 using (var context = new ClientContext(siteUrl))
  96.                 {
  97.                     context.Credentials = new SharePointOnlineCredentials(User, Password);
  98.                     var web = context.Web;
  99.                     context.Load(web, w => w.ServerRelativeUrl);
  100.                     context.ExecuteQuery();
  101.  
  102.                     web.RecycleBin.DeleteAll();
  103.                     DeleteSubweb(context, "Help and requests");
  104.                     ClearLists(context);
  105.                     SetMasterPage(context, $"{web.ServerRelativeUrl}/_catalogs/masterpage/seattle.master", $"{web.ServerRelativeUrl}/_catalogs/masterpage/seattle.master");
  106.                     SetWelcomePage(context, "SitePages/Home.aspx");
  107.                     DeleteFolder(context, "SiteAssets/ContentCloud");
  108.                     DeletePublishingPages(context);
  109.                     DeleteFolder(context, "_catalogs/ContentCloud");
  110.                     ClearContentTypesFromGroup(context, "Content Cloud Document Metadata Content Types");
  111.                     ClearContentTypesFromGroup(context, "Content Cloud Item Metadata Content Types");
  112.                     ClearContentTypesFromGroup(context, "Content Cloud Metadata Content Types");
  113.                     ClearContentTypesFromGroup(context, "Withdrawn Content Cloud Document Metadata Content Types");
  114.                     ClearContentTypesFromGroup(context, "Content Cloud Content Types");
  115.                     ClearContentTypesFromGroup(context, "Content Cloud Pages Content Types");
  116.                     ClearContentTypesFromGroup(context, "Admin Support Pages Content Types");
  117.                     ClearContentTypesFromGroup(context, "Admin Support Content Types");
  118.                     ClearFieldsFromGroup(context, "Admin Support Columns");
  119.                     ClearFieldsFromGroup(context, "Content Cloud Columns");
  120.                     ClearFieldsFromGroup(context, "Content Cloud Metadata Item Columns");
  121.  
  122.                     web.RecycleBin.DeleteAll();
  123.  
  124.                     CleanupWebPropertyBag(context, web);
  125.  
  126.                     Console.WriteLine("");
  127.                 }
  128.             }
  129.             catch (Exception ex)
  130.             {
  131.                 Console.WriteLine("Error is: " + ex.Message);
  132.             }
  133.         }
  134.  
  135.         private static void DeletePublishingPages(ClientContext context)
  136.         {
  137.             var contentTypeNamePart = "Content Cloud";
  138.             var pagesLibrary = context.Web.Lists.GetByTitle("Pages");
  139.             context.Load(pagesLibrary,
  140.                 l => l.ContentTypes,
  141.                 l => l.ContentTypes.Include(ct => ct.Group, ct => ct.StringId),
  142.                 l => l.RootFolder,
  143.                 l => l.RootFolder.Files,
  144.                 l => l.RootFolder.Files
  145.                     .Include(f => f.ListItemAllFields.Id,
  146.                         f => f.ListItemAllFields,
  147.                         f => f.ListItemAllFields.ContentType,
  148.                         f => f.ListItemAllFields.ContentType.Group));
  149.             context.ExecuteQuery();
  150.             foreach (var page in pagesLibrary.RootFolder.Files)
  151.             {
  152.                 if (page.ListItemAllFields.ContentType.Group.Contains(contentTypeNamePart))
  153.                 {
  154.                     var item = pagesLibrary.GetItemById(page.ListItemAllFields.Id);
  155.                     item.DeleteObject();
  156.                     context.ExecuteQuery();
  157.                 }
  158.             }
  159.  
  160.             foreach (var contentType in pagesLibrary.ContentTypes)
  161.             {
  162.                 if (contentType.Group.Contains(contentTypeNamePart))
  163.                 {
  164.                     var contentTypeToDelete = pagesLibrary.ContentTypes.GetById(contentType.StringId);
  165.                     contentTypeToDelete.DeleteObject();
  166.                     context.ExecuteQuery();
  167.                 }
  168.             }
  169.         }
  170.  
  171.         private static void DeleteSubweb(ClientContext context, string title)
  172.         {
  173.             context.Load(context.Web, w => w.Webs, w => w.Webs.Include(sw => sw.Title));
  174.             context.ExecuteQuery();
  175.             var web = context.Web.Webs.FirstOrDefault(w => w.Title == title);
  176.             if (web != null)
  177.             {
  178.                 Console.Write($"Deleting web {title}... ");
  179.                 web.DeleteObject();
  180.                 context.ExecuteQuery();
  181.                 Console.WriteLine("Deleted.");
  182.             }
  183.         }
  184.  
  185.         private static void SetMasterPage(ClientContext context, string masterUrl, string customMasterUrl)
  186.         {
  187.             Console.Write($"Changing masterpage to {masterUrl} and custom master to {customMasterUrl}... ");
  188.             context.Web.MasterUrl = masterUrl;
  189.             context.Web.CustomMasterUrl = customMasterUrl;
  190.             context.Web.Update();
  191.             context.ExecuteQuery();
  192.             Console.WriteLine("Changed.");
  193.         }
  194.  
  195.         private static void SetWelcomePage(ClientContext context, string webRelativeUrl)
  196.         {
  197.             context.Load(context.Web, w => w.RootFolder, w => w.RootFolder.WelcomePage);
  198.             context.ExecuteQuery();
  199.             if (!context.Web.RootFolder.WelcomePage.EndsWith(webRelativeUrl))
  200.             {
  201.                 Console.Write($"Setting home page to {webRelativeUrl}... ");
  202.                 context.Web.RootFolder.WelcomePage = webRelativeUrl;
  203.                 context.Web.RootFolder.Update();
  204.                 context.ExecuteQuery();
  205.                 Console.WriteLine("Changed.");
  206.             }
  207.         }
  208.  
  209.         private static void DeleteFolder(ClientContext context, string webRelativeUrl)
  210.         {
  211.             try
  212.             {
  213.                 Console.Write($"Deleting folder {webRelativeUrl}... ");
  214.                 var folder = context.Web.GetFolderByServerRelativeUrl(webRelativeUrl);
  215.                 folder.DeleteObject();
  216.                 context.ExecuteQuery();
  217.                 Console.WriteLine("Deleted.");
  218.             }
  219.             catch (Exception ex)
  220.             {
  221.                 Console.WriteLine($"Skipping folder {webRelativeUrl} because of {ex.Message}");
  222.             }
  223.         }
  224.  
  225.         /// <summary>
  226.         /// Gets the password input from the console window
  227.         /// </summary>
  228.         /// <returns>the entered string</returns>
  229.         private static string GetInput(string label, bool isPassword)
  230.         {
  231.             Console.ForegroundColor = ConsoleColor.White;
  232.             Console.Write("{0} : ", label);
  233.             Console.ForegroundColor = ConsoleColor.Gray;
  234.  
  235.             string strPwd = "";
  236.  
  237.             for (ConsoleKeyInfo keyInfo = Console.ReadKey(true); keyInfo.Key != ConsoleKey.Enter; keyInfo = Console.ReadKey(true))
  238.             {
  239.                 if (keyInfo.Key == ConsoleKey.Backspace)
  240.                 {
  241.                     if (strPwd.Length > 0)
  242.                     {
  243.                         strPwd = strPwd.Remove(strPwd.Length - 1);
  244.                         Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
  245.                         Console.Write(" ");
  246.                         Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
  247.                     }
  248.                 }
  249.                 else if (keyInfo.Key != ConsoleKey.Enter)
  250.                 {
  251.                     if (isPassword)
  252.                     {
  253.                         Console.Write("*");
  254.                     }
  255.                     else
  256.                     {
  257.                         Console.Write(keyInfo.KeyChar);
  258.                     }
  259.                     strPwd += keyInfo.KeyChar;
  260.  
  261.                 }
  262.  
  263.             }
  264.             Console.WriteLine("");
  265.  
  266.             return strPwd;
  267.         }
  268.  
  269.         private static void ClearLists(ClientContext context)
  270.         {
  271.             var listNames = new List<string>();
  272.             Web web = context.Web;
  273.             context.Load(web.Lists,
  274.                 lists => lists.Include(list => list.Title,
  275.                     list => list.Id));
  276.             context.ExecuteQuery();
  277.             Console.ForegroundColor = ConsoleColor.White;
  278.             foreach (List list in web.Lists)
  279.             {
  280.                 if (list.Title.ToLower().StartsWith("content cloud") || list.Title.ToLower().StartsWith("withdrawn content cloud"))
  281.                 {
  282.                     listNames.Add(list.Title);
  283.                 }
  284.             }
  285.  
  286.             listNames.ForEach(listName =>
  287.             {
  288.                 Console.WriteLine("Deleting list : " + listName);
  289.                 List list = web.Lists.GetByTitle(listName);
  290.                 list.DeleteObject();
  291.                 context.ExecuteQuery();
  292.             });
  293.  
  294.             web.RecycleBin.DeleteAll();
  295.         }
  296.  
  297.         private static void ClearContentTypesFromGroup(ClientContext context, string group)
  298.         {
  299.             var ctNames = new List<string>();
  300.  
  301.             Console.WriteLine("Deleting content types from group : " + group);
  302.             Web web = context.Web;
  303.             context.Load(web.ContentTypes,
  304.                 contentTypes => contentTypes.Include(contentType => contentType.Group,
  305.                     contentType => contentType.Id, contentType => contentType.Name));
  306.             context.ExecuteQuery();
  307.             Console.ForegroundColor = ConsoleColor.White;
  308.             foreach (ContentType ct in web.ContentTypes)
  309.             {
  310.                 if (ct.Group.ToLower().Equals(group.ToLower()))
  311.                 {
  312.                     ctNames.Add(ct.Id.StringValue);
  313.                 }
  314.             }
  315.  
  316.             ctNames.ForEach(ctName =>
  317.             {
  318.                 try
  319.                 {
  320.                     Console.WriteLine("Deleting content type: " + ctName);
  321.                     ContentType ct = web.ContentTypes.GetById(ctName);
  322.                     ct.DeleteObject();
  323.                     context.ExecuteQuery();
  324.                 }
  325.                 catch (Exception ex)
  326.                 {
  327.                     Console.WriteLine($"Skipping content type {ctName} because of {ex.Message}");
  328.                 }
  329.             });
  330.  
  331.             web.RecycleBin.DeleteAll();
  332.         }
  333.  
  334.         private static void ClearFieldsFromGroup(ClientContext context, string group)
  335.         {
  336.             var fieldIds = new List<string>();
  337.  
  338.             Console.WriteLine("Deleting fields from group : " + group);
  339.             Web web = context.Web;
  340.             context.Load(web.Fields,
  341.                 fields => fields.Include(field => field.Group,
  342.                     field => field.Id, field => field.Title));
  343.             context.ExecuteQuery();
  344.             Console.ForegroundColor = ConsoleColor.White;
  345.             foreach (Field f in web.Fields)
  346.             {
  347.                 if (f.Group.ToLower().Equals(group.ToLower()))
  348.                 {
  349.                     fieldIds.Add(f.Id.ToString());
  350.                 }
  351.             }
  352.  
  353.             fieldIds.ForEach(field =>
  354.             {
  355.                 try
  356.                 {
  357.                     Console.WriteLine("Deleting field type: " + field);
  358.                     Field f = web.Fields.GetById(new Guid(field));
  359.                     f.DeleteObject();
  360.                     context.ExecuteQuery();
  361.                 }
  362.                 catch (Exception ex)
  363.                 {
  364.                     Console.WriteLine($"Skipping field {field} because of {ex.Message}");
  365.                 }
  366.             });
  367.  
  368.             web.RecycleBin.DeleteAll();
  369.         }
  370.  
  371.         private static void CleanupWebPropertyBag(ClientContext clientContext, Web web)
  372.         {
  373.             Console.WriteLine("Cleaning up web property bag from ItemChangeKey properties");
  374.             clientContext.Load(web, w => w.AllProperties);
  375.             clientContext.ExecuteQuery();
  376.  
  377.             var contentCloudPropertyKeys =
  378.                 web.AllProperties.FieldValues.Where(kv => kv.Key.StartsWith("ItemChangeKey") || kv.Key.StartsWith("ContentCloudLatestReferenceNumber"))
  379.                     .Select(fv => fv.Key)
  380.                     .ToList();
  381.             foreach (var key in contentCloudPropertyKeys)
  382.             {
  383.                 web.AllProperties[key] = null;
  384.                 Console.WriteLine("Deleting property: " + key);
  385.             }
  386.  
  387.             web.Update();
  388.             clientContext.ExecuteQuery();
  389.         }
  390.     }
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement