public static bool ReplaceWebPartInPage(string webUrl, string targetWebPartFileName, string targetWebPartXmlFile, string sourceWebPartStorageKey, string webPartZoneIndex, string webPartZoneID, string serverRelativePageUrl, string outPutDirectory, string sourceWebPartId = "") { bool isWebPartReplaced = false; if (DeleteWebparts.DeleteWebPart(webUrl, serverRelativePageUrl, sourceWebPartStorageKey)) { System.Console.ForegroundColor = System.ConsoleColor.Green; Logger.LogSuccessMessage("[ReplaceWebPart]Successfully Deleted WebPart "); System.Console.ResetColor(); if (AddWebPart.AddWebPartToPage(webUrl, targetWebPartFileName, targetWebPartXmlFile, webPartZoneIndex, webPartZoneID, serverRelativePageUrl, outPutDirectory, sourceWebPartId)) { System.Console.ForegroundColor = System.ConsoleColor.Green; isWebPartReplaced = true; Logger.LogSuccessMessage("[ReplaceWebPart]Successfully Added WebPart "); Logger.LogSuccessMessage("[ReplaceWebPart] Successfully Replaced the newly configured WebPart and output file is present in the path: " + outPutDirectory, true); System.Console.ResetColor(); } } return(isWebPartReplaced); }
public static void GetWebPartUsage(string webPartType, string webUrl, string outPutDirectory) { string exceptionCommentsInfo1 = string.Empty; ClientContext clientContext = null; bool headerWebPart = false; Web web = null; bool isfound = false; string webPartUsageFileName = outPutDirectory + "\\" + Constants.WEBPART_USAGE_ENTITY_FILENAME + timeStamp + Constants.CSVExtension; try { Logger.LogInfoMessage("[GetWebPartUsage] Finding WebPartUsage details for Web Part: " + webPartType + " in Web: " + webUrl); using (ClientContext userContext = Helper.CreateAuthenticatedUserContext(Program.AdminDomain, Program.AdminUsername, Program.AdminPassword, webUrl)) { web = userContext.Web; userContext.Load(web); userContext.ExecuteQuery(); clientContext = userContext; WebPartUsageEntity webPartUsageEntity = null; List <WebPartUsageEntity> webPartUsage = new List <WebPartUsageEntity>(); //Prepare Exception Comments exceptionCommentsInfo1 = "Web Url: " + clientContext.Web.Url + ", Web Part Type: " + webPartType; if (clientContext != null) { List list = AddWebPart.GetPageList(ref clientContext); if (list != null) { var items = list.GetItems(CamlQuery.CreateAllItemsQuery()); //make sure to include the File on each Item fetched clientContext.Load(items, i => i.Include( item => item.File, item => item["EncodedAbsUrl"])); clientContext.ExecuteQuery(); // Iterate through all available pages in the pages list foreach (var item in items) { try { Microsoft.SharePoint.Client.File page = item.File; String pageUrl = page.ServerRelativeUrl;// item.FieldValues["EncodedAbsUrl"].ToString(); Logger.LogInfoMessage("[GetWebPartUsage] Checking for the Web Part on the Page: " + page.Name); // Requires Full Control permissions on the Web LimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared); clientContext.Load(webPartManager, wpm => wpm.WebParts, wpm => wpm.WebParts.Include( wp => wp.WebPart.Hidden, wp => wp.WebPart.IsClosed, wp => wp.WebPart.Properties, wp => wp.WebPart.Subtitle, wp => wp.WebPart.Title, wp => wp.WebPart.TitleUrl, wp => wp.WebPart.ZoneIndex)); clientContext.ExecuteQuery(); foreach (WebPartDefinition webPartDefinition in webPartManager.WebParts) { Microsoft.SharePoint.Client.WebParts.WebPart webPart = webPartDefinition.WebPart; string webPartPropertiesXml = GetWebPartPropertiesServiceCall(clientContext, webPartDefinition.Id.ToString(), pageUrl); string WebPartTypeName = string.Empty; if (webPartPropertiesXml.Contains("WebPart/v2")) { XmlDataDocument xmldoc = new XmlDataDocument(); xmldoc.LoadXml(webPartPropertiesXml); WebPartTypeName = xmldoc.DocumentElement.GetElementsByTagName("TypeName").Item(0).FirstChild.Value; xmldoc = null; } else { webParts webPartProp = null; byte[] byteArray = Encoding.UTF8.GetBytes(webPartPropertiesXml); using (MemoryStream stream = new MemoryStream(byteArray)) { StreamReader streamReader = new StreamReader(stream); System.Xml.XmlReader reader = new XmlTextReader(streamReader); XmlSerializer serializer = new XmlSerializer(typeof(webParts)); webPartProp = (webParts)serializer.Deserialize(reader); WebPartTypeName = webPartProp.webPart.metaData.type.name; stream.Flush(); } byteArray = null; } string actualWebPartType = AddWebPart.GetWebPartShortTypeName(WebPartTypeName); // only modify if we find the old web part if (actualWebPartType.Equals(webPartType, StringComparison.CurrentCultureIgnoreCase)) { Logger.LogInfoMessage("[GetWebPartUsage] Found WebPart: " + webPartType + " in Page: " + page.Name); webPartUsageEntity = new WebPartUsageEntity(); webPartUsageEntity.PageUrl = pageUrl; webPartUsageEntity.WebPartID = webPartDefinition.Id.ToString(); webPartUsageEntity.WebURL = webUrl; webPartUsageEntity.WebPartTitle = webPart.Title; webPartUsageEntity.ZoneIndex = webPart.ZoneIndex.ToString(); webPartUsageEntity.WebPartType = actualWebPartType; FileUtility.WriteCsVintoFile(webPartUsageFileName, webPartUsageEntity, ref headerWebPart); isfound = true; } } } catch (Exception ex) { ExceptionCsv.WriteException(Constants.NotApplicable, Constants.NotApplicable, webUrl, "WebPartUsage", ex.Message, ex.ToString(), "GetWebPartUsage()", ex.GetType().ToString(), exceptionCommentsInfo1); Logger.LogErrorMessage("[GetWebPartUsage] Exception Message: " + ex.Message + ", Exception Comment: " + exceptionCommentsInfo1); System.Console.ForegroundColor = System.ConsoleColor.Red; Logger.LogErrorMessage("[GetWebPartUsage] Exception Message: " + ex.Message); System.Console.ResetColor(); } } } } //Default Pages GetWebPartUsage_DefaultPages(webPartType, clientContext, outPutDirectory); //Default Pages if (isfound) { Logger.LogSuccessMessage("[GetWebPartUsage] WebPart Usage is exported to the file " + webPartUsageFileName, true); } } } catch (Exception ex) { ExceptionCsv.WriteException(Constants.NotApplicable, Constants.NotApplicable, webUrl, "WebPartUsage", ex.Message, ex.ToString(), "GetWebPartUsage()", ex.GetType().ToString(), exceptionCommentsInfo1); Logger.LogErrorMessage("[GetWebPartUsage] Exception Message: " + ex.Message + ", Exception Comment: " + exceptionCommentsInfo1); System.Console.ForegroundColor = System.ConsoleColor.Red; Logger.LogErrorMessage("[GetWebPartUsage] Exception Message: " + ex.Message); System.Console.ResetColor(); } }
public static void GetWebPartUsage_DefaultPages(string webPartType, ClientContext clientContext, string outPutDirectory) { ExceptionCsv.WebUrl = clientContext.Web.Url; string exceptionCommentsInfo1 = string.Empty; string webPartUsageFileName = outPutDirectory + "\\" + Constants.WEBPART_USAGE_ENTITY_FILENAME + timeStamp + Constants.CSVExtension; bool headerWebPart = false; bool isfound = false; try { string webUrl = clientContext.Web.Url; Logger.LogInfoMessage("[START][GetWebPartUsage_DefaultPages]"); Logger.LogInfoMessage("[GetWebPartUsage_DefaultPages] Finding WebPartUsage details for Web Part: " + webPartType + " in Web: " + webUrl); WebPartUsageEntity webPartUsageEntity = null; List <WebPartUsageEntity> webPartUsage = new List <WebPartUsageEntity>(); if (clientContext != null) { clientContext.Load(clientContext.Web.RootFolder.Files); clientContext.ExecuteQuery(); foreach (Microsoft.SharePoint.Client.File page in clientContext.Web.RootFolder.Files) { exceptionCommentsInfo1 = "Web Url: " + clientContext.Web.Url + ", Web Part Type: " + webPartType + ", PageTitle: " + page.ServerRelativeUrl; try { if (Path.GetExtension(page.ServerRelativeUrl).Equals(".aspx", StringComparison.CurrentCultureIgnoreCase)) { String pageUrl = page.ServerRelativeUrl; Logger.LogInfoMessage("[GetWebPartUsage_DefaultPages] Checking for the Web Part on the Page: " + page.Name); // Requires Full Control permissions on the Web LimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared); clientContext.Load(webPartManager, wpm => wpm.WebParts, wpm => wpm.WebParts.Include( wp => wp.WebPart.Hidden, wp => wp.WebPart.IsClosed, wp => wp.WebPart.Properties, wp => wp.WebPart.Subtitle, wp => wp.WebPart.Title, wp => wp.WebPart.TitleUrl, wp => wp.WebPart.ZoneIndex)); clientContext.ExecuteQuery(); foreach (WebPartDefinition webPartDefinition in webPartManager.WebParts) { Microsoft.SharePoint.Client.WebParts.WebPart webPart = webPartDefinition.WebPart; string webPartPropertiesXml = GetWebPartPropertiesServiceCall(clientContext, webPartDefinition.Id.ToString(), pageUrl); string WebPartTypeName = string.Empty; if (webPartPropertiesXml.Contains("WebPart/v2")) { XmlDataDocument xmldoc = new XmlDataDocument(); xmldoc.LoadXml(webPartPropertiesXml); WebPartTypeName = xmldoc.DocumentElement.GetElementsByTagName("TypeName").Item(0).FirstChild.Value; xmldoc = null; } else { webParts webPartProp = null; byte[] byteArray = Encoding.UTF8.GetBytes(webPartPropertiesXml); using (MemoryStream stream = new MemoryStream(byteArray)) { StreamReader streamReader = new StreamReader(stream); System.Xml.XmlReader reader = new XmlTextReader(streamReader); XmlSerializer serializer = new XmlSerializer(typeof(webParts)); webPartProp = (webParts)serializer.Deserialize(reader); WebPartTypeName = webPartProp.webPart.metaData.type.name; stream.Flush(); } byteArray = null; } string actualWebPartType = AddWebPart.GetWebPartShortTypeName(WebPartTypeName); // only modify if we find the old web part if (actualWebPartType.Equals(webPartType, StringComparison.CurrentCultureIgnoreCase)) { Logger.LogInfoMessage("[GetWebPartUsage_DefaultPages] Found WebPart: " + webPartType + " in Page: " + page.Name + ", " + page.ServerRelativeUrl); webPartUsageEntity = new WebPartUsageEntity(); webPartUsageEntity.PageUrl = pageUrl; webPartUsageEntity.WebPartID = webPartDefinition.Id.ToString(); webPartUsageEntity.WebURL = webUrl; webPartUsageEntity.WebPartTitle = webPart.Title; webPartUsageEntity.ZoneIndex = webPart.ZoneIndex.ToString(); webPartUsageEntity.WebPartType = actualWebPartType; FileUtility.WriteCsVintoFile(webPartUsageFileName, webPartUsageEntity, ref headerWebPart); isfound = true; } } } } catch (Exception ex) { ExceptionCsv.WriteException(Constants.NotApplicable, Constants.NotApplicable, webUrl, "WebPartUsage", ex.Message, ex.ToString(), "GetWebPartUsage_DefaultPages()", ex.GetType().ToString(), exceptionCommentsInfo1); Logger.LogErrorMessage("[GetWebPartUsage_DefaultPages] Exception Message: " + ex.Message + ", Exception Comment: " + exceptionCommentsInfo1); System.Console.ForegroundColor = System.ConsoleColor.Red; Logger.LogErrorMessage("[GetWebPartUsage_DefaultPages] Exception Message: " + ex.Message); System.Console.ResetColor(); } } } if (isfound) { Logger.LogInfoMessage("[GetWebPartUsage_DefaultPages] Default Pages WebPart Usage is exported to the file " + webPartUsageFileName); } Logger.LogInfoMessage("[END][GetWebPartUsage_DefaultPages]"); } catch (Exception ex) { System.Console.ForegroundColor = System.ConsoleColor.Red; Logger.LogErrorMessage("[GetWebPartUsage_DefaultPages] Exception Message: " + ex.Message); System.Console.ResetColor(); ExceptionCsv.WriteException(Constants.NotApplicable, Constants.NotApplicable, ExceptionCsv.WebUrl, "WebPartUsage", ex.Message, ex.ToString(), "GetWebPartUsage_DefaultPages()", ex.GetType().ToString(), exceptionCommentsInfo1); } }
/// <summary> /// Main method /// </summary> /// <param name="args">Event Arguments</param> public static void Main(string[] args) { string input = String.Empty; string input1 = string.Empty; string input2 = string.Empty; GetCredentials(); //Excception CSV Creation Command ExceptionCsv objException = ExceptionCsv.CurrentInstance; objException.CreateLogFile(Environment.CurrentDirectory); do { ShowUsage(); input = System.Console.ReadLine(); switch (input.ToUpper(System.Globalization.CultureInfo.CurrentCulture)) { case "1": do { System.Console.ForegroundColor = System.ConsoleColor.Green; System.Console.WriteLine(""); System.Console.WriteLine("Your selected Operation [1: \"Transformation\"] supports the following operations."); System.Console.WriteLine("Please type an operation number and press [Enter] to execute the specified operation:"); System.Console.WriteLine("1. Add OOTB Web Part or App Part to a page"); System.Console.WriteLine("2. Replace FTC Web Part with OOTB Web Part or App Part on a page"); System.Console.WriteLine("3. Replace MasterPage"); System.Console.WriteLine("4. Reset Device Channel Mapping Files"); System.Console.WriteLine("5. Manage [Add|Remove] Maintenance Banners"); System.Console.WriteLine("6. Exit"); System.Console.ResetColor(); input1 = System.Console.ReadLine(); switch (input1) { case "1": AddWebPart.DoWork(); break; case "2": ReplaceWebPart.DoWork(); break; case "3": ReplaceMasterPage.DoWork(); break; case "4": ResetDeviceChannelMappingFiles.DoWork(); break; case "5": ManageMaintenanceBanners.DoWork(); break; case "6": break; default: break; } } while (input1.ToUpper(System.Globalization.CultureInfo.CurrentCulture) != "6"); break; case "2": do { System.Console.ForegroundColor = System.ConsoleColor.Magenta; System.Console.WriteLine(""); System.Console.WriteLine("Your selected Operation [2: \"Clean-Up\"] supports the following operations."); System.Console.WriteLine("Please type an operation number and press [Enter] to execute the specified operation:"); System.Console.WriteLine("1. Delete Missing Setup Files"); System.Console.WriteLine("2. Delete Missing Features"); System.Console.WriteLine("3. Delete Missing Event Receivers"); System.Console.WriteLine("4. Delete Workflow Associations"); System.Console.WriteLine("5. Delete List Templates"); System.Console.WriteLine("6. Delete Missing Webparts"); System.Console.WriteLine("7. Exit"); System.Console.ResetColor(); input1 = System.Console.ReadLine(); switch (input1) { case "1": DeleteMissingSetupFiles.DoWork(); break; case "2": DeleteMissingFeatures.DoWork(); break; case "3": DeleteMissingEventReceivers.DoWork(); break; case "4": DeleteMissingWorkflowAssociations.DoWork(); break; case "5": DownloadAndModifyListTemplate.DeleteListTemplate(); break; case "6": DeleteWebparts.DoWork(); break; case "7": break; default: break; } } while (input1.ToUpper(System.Globalization.CultureInfo.CurrentCulture) != "7"); break; case "3": do { System.Console.ForegroundColor = System.ConsoleColor.DarkCyan; System.Console.WriteLine(""); System.Console.WriteLine("Your selected Operation [3: \"Self-Service Reports\"] supports the following operations."); System.Console.WriteLine("Please type an operation number and press [Enter] to execute the specified operation:"); System.Console.WriteLine("1. Generate List Template Report with FTC Analysis"); System.Console.WriteLine("2. Generate Site Template Report with FTC Analysis"); System.Console.WriteLine("3. Generate Site Column/Custom Field & Content Type Usage Report"); System.Console.WriteLine("4. Generate Non-Default Master Page Usage Report"); System.Console.WriteLine("5. Generate Site Collection Report (PPE-Only)"); System.Console.WriteLine("6. Generate Web Part Usage Report"); System.Console.WriteLine("7. Generate Web Part Properties Report"); System.Console.WriteLine("8. Generate Security Group Report"); System.Console.WriteLine("9. Generate Pivot Reports "); System.Console.WriteLine("10. Exit "); System.Console.ResetColor(); input1 = System.Console.ReadLine(); switch (input1) { case "1": DownloadAndModifyListTemplate.DoWork(); break; case "2": DownloadAndModifySiteTemplate.DoWork(); break; case "3": GenerateColumnAndTypeUsageReport.DoWork(); break; case "4": GenerateNonDefaultMasterPageUsageReport.DoWork(); break; case "5": System.Console.ForegroundColor = ConsoleColor.Yellow; System.Console.WriteLine("This operation is intended for use only in PPE; use on PROD at your own risk."); System.Console.WriteLine("For PROD, it is safer to generate the report via the o365 Self-Service Admin Portal."); System.Console.ResetColor(); System.Console.ForegroundColor = System.ConsoleColor.Cyan; System.Console.WriteLine("Press \"y\" only if you wish to continue. Press any other key to abort this operation."); System.Console.ResetColor(); input2 = System.Console.ReadLine(); if (input2.ToUpper(System.Globalization.CultureInfo.CurrentCulture) != "Y") { System.Console.WriteLine("Operation aborted by user."); break; } GenerateSiteCollectionReport.DoWork(); break; case "6": WebPartUsage.DoWork(); break; case "7": WebPartProperties.DoWork(); break; case "8": GenerateSecurityGroupReport.DoWork(); break; case "9": GeneratePivotReport.DoWork(); break; case "10": break; default: break; } } while (input1.ToUpper(System.Globalization.CultureInfo.CurrentCulture) != "10"); break; case "4": break; default: break; } }while (input.ToUpper(System.Globalization.CultureInfo.CurrentCulture) != "4"); }