public 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;

            try
            {
                string webUrl = clientContext.Web.Url;

                Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartUsage_DefaultPages] Finding WebPartUsage details for Web Part: " + webPartType + " in Web: " + webUrl);
                Console.WriteLine("[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;

                        if (Path.GetExtension(page.ServerRelativeUrl).Equals(".aspx", StringComparison.CurrentCultureIgnoreCase))
                        {
                            String pageUrl = page.ServerRelativeUrl;

                            Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartUsage_DefaultPages] Checking for the Web Part on the Page: " + page.Name);
                            Console.WriteLine("[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 = GetWebPartShortTypeName(WebPartTypeName);

                                // only modify if we find the old web part
                                if (actualWebPartType.Equals(webPartType))
                                {
                                    Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartUsage_DefaultPages] Found WebPart: " + webPartType + " in Page: " + page.Name + ", " + page.ServerRelativeUrl);
                                    Console.WriteLine("[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);
                                }
                            }
                        }
                    }
                }

                Console.WriteLine("[GetWebPartUsage_DefaultPages] Default Pages WebPart Usage is exported to the file " + webPartUsageFileName);
                Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartUsage_DefaultPages] Default Pages WebPart Usage is exported to the file " + webPartUsageFileName);

                Logger.AddMessageToTraceLogFile(Constants.Logging, "[END][GetWebPartUsage_DefaultPages]");
                Console.WriteLine("[END][GetWebPartUsage_DefaultPages]");
            }
            catch (Exception ex)
            {
                ExceptionCsv.WriteException(ExceptionCsv.WebApplication, ExceptionCsv.SiteCollection, ExceptionCsv.WebUrl, "Web Part", ex.Message, ex.ToString(), "GetWebPartUsage_DefaultPages", ex.GetType().ToString(), exceptionCommentsInfo1);
                Logger.AddMessageToTraceLogFile(Constants.Logging, "[EXCEPTION][GetWebPartUsage_DefaultPages] Exception Message: " + ex.Message + ", Exception Comment: " + exceptionCommentsInfo1);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("[EXCEPTION][GetWebPartUsage_DefaultPages] Exception Message: " + ex.Message);
                Console.ForegroundColor = ConsoleColor.Gray;
            }
        }
        public void GetWebPartUsage(string webPartType, ClientContext clientContext,string outPutDirectory)
        {
            ExceptionCsv.WebUrl = clientContext.Web.Url;
            string exceptionCommentsInfo1 = string.Empty;
            string webPartUsageFileName = outPutDirectory + "\\" + Constants.WEBPART_USAGE_ENTITY_FILENAME;
            try
            {
                //Initialized Exception and Logger.
                WebPart_Initialization(outPutDirectory);

                string webUrl = clientContext.Web.Url;
                Logger.AddMessageToTraceLogFile(Constants.Logging, "############## Web Part  Trasnformation Utility Execution Started ##############");
                Console.WriteLine("############## Web Part  Trasnformation Utility Execution Started ##############");

                Logger.AddMessageToTraceLogFile(Constants.Logging, "[DATE TIME] " + Logger.CurrentDateTime());
                Console.WriteLine("[DATE TIME] " + Logger.CurrentDateTime());

                Logger.AddMessageToTraceLogFile(Constants.Logging, "[START][GetWebPartUsage]");
                Console.WriteLine("[START][GetWebPartUsage]");

                Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartUsage] Initiated Logger and Exception Class. Logger and Exception file will be available at path: " + outPutDirectory);
                Console.WriteLine("[GetWebPartUsage] Initiated Logger and Exception Class. Logger and Exception file will be available at path: " + outPutDirectory);

                Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartUsage] Finding WebPartUsage details for Web Part: " + webPartType + " in Web: " + webUrl);
                Console.WriteLine("[GetWebPartUsage] Finding WebPartUsage details for Web Part: " + webPartType + " in Web: " + webUrl);

                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 = 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)
                        {
                            Microsoft.SharePoint.Client.File page = item.File;

                            String pageUrl = page.ServerRelativeUrl;// item.FieldValues["EncodedAbsUrl"].ToString();

                            Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartUsage] Checking for the Web Part on the Page: " + page.Name);
                            Console.WriteLine("[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 = GetWebPartShortTypeName(WebPartTypeName);

                                // only modify if we find the old web part
                                if (actualWebPartType.Equals(webPartType))
                                {
                                    Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartUsage] Found WebPart: " + webPartType + " in Page: " + page.Name);
                                    Console.WriteLine("[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);
                                }
                            }
                        }
                    }
                }
                //Default Pages
                GetWebPartUsage_DefaultPages(webPartType, clientContext, outPutDirectory);
                //Default Pages

                Console.WriteLine("[GetWebPartUsage] WebPart Usage is exported to the file " + webPartUsageFileName);
                Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartUsage] WebPart Usage is exported to the file " + webPartUsageFileName);

                Logger.AddMessageToTraceLogFile(Constants.Logging, "[END][GetWebPartUsage]");
                Console.WriteLine("[END][GetWebPartUsage]");

                Logger.AddMessageToTraceLogFile(Constants.Logging, "############## Web Part Trasnformation Utility Execution Completed for Web ##############");
                Console.WriteLine("############## Web Part Trasnformation Utility Execution Completed  for Web ##############");
            }
            catch (Exception ex)
            {
                ExceptionCsv.WriteException(ExceptionCsv.WebApplication, ExceptionCsv.SiteCollection, ExceptionCsv.WebUrl, "Web Part", ex.Message, ex.ToString(), "GetWebPartUsage", ex.GetType().ToString(), exceptionCommentsInfo1);
                Logger.AddMessageToTraceLogFile(Constants.Logging, "[EXCEPTION][GetWebPartUsage] Exception Message: " + ex.Message + ", Exception Comment: " + exceptionCommentsInfo1);
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("[EXCEPTION][GetWebPartUsage] Exception Message: " + ex.Message);
                Console.ForegroundColor = ConsoleColor.Gray;
            }
        }