public string GetWebPartProperties(string pageUrl, string StorageKey, string webUrl, string outPutDirectory, string SharePointOnline_OR_OnPremise = "N/A", string UserName = "******", string Password = "******", string Domain = "N/A", string ActionType = "web") { string webPartPropertiesFileName = string.Empty; AuthenticationHelper ObjAuth = new AuthenticationHelper(); ClientContext clientContext = new ClientContext(webUrl); string webPartXml = string.Empty; ExceptionCsv.WebUrl = webUrl; string exceptionCommentsInfo1 = string.Empty; try { if (ActionType.ToLower().Trim() == Constants.ActionType_Web.ToLower()) { //Initialized Exception and Logger. WebPart_Initialization(outPutDirectory); Logger.AddMessageToTraceLogFile(Constants.Logging, "############## Web Part Trasnformation Utility Execution Started - GetWebPartProperties ##############"); Console.WriteLine("############## Web Part Trasnformation Utility Execution Started - GetWebPartProperties ##############"); Logger.AddMessageToTraceLogFile(Constants.Logging, "[DATE TIME] " + Logger.CurrentDateTime()); Console.WriteLine("[DATE TIME] " + Logger.CurrentDateTime()); Logger.AddMessageToTraceLogFile(Constants.Logging, "[START][GetWebPartProperties] "); Console.WriteLine("[START][GetWebPartProperties] "); Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartProperties] Initiated Logger and Exception Class. Logger and Exception file will be available at path: " + outPutDirectory); Console.WriteLine("[GetWebPartProperties] Initiated Logger and Exception Class. Logger and Exception file will be available at path: " + outPutDirectory); } string sourceWebPartXmlFilesDir = outPutDirectory + @"\" + Constants.SOURCE_WEBPART_XML_DIR; if (!System.IO.Directory.Exists(sourceWebPartXmlFilesDir)) { System.IO.Directory.CreateDirectory(sourceWebPartXmlFilesDir); } //Deleted the Web Part Usage File DeleteUsageFiles_WebPartHelper(sourceWebPartXmlFilesDir, StorageKey + "_" + Constants.WEBPART_PROPERTIES_FILENAME); //Prepare Exception Comments exceptionCommentsInfo1 = "Web Url: " + webUrl + ", Page Url: " + pageUrl + ", StorageKey" + StorageKey; //SharePoint on-premises / SharePoint Online Dedicated => OP (On-Premises) if (SharePointOnline_OR_OnPremise.ToUpper().Equals(Constants.OnPremise.ToUpper())) { Logger.AddMessageToTraceLogFile(Constants.Logging, "[START][GetWebPartProperties] GetNetworkCredentialAuthenticatedContext for WebUrl: " + webUrl); clientContext = ObjAuth.GetNetworkCredentialAuthenticatedContext(webUrl, UserName, Password, Domain); Logger.AddMessageToTraceLogFile(Constants.Logging, "[END][GetWebPartProperties] GetNetworkCredentialAuthenticatedContext for WebUrl: " + webUrl); } //SharePointOnline => OL (Online) else if (SharePointOnline_OR_OnPremise.ToUpper().Equals(Constants.Online.ToUpper())) { Logger.AddMessageToTraceLogFile(Constants.Logging, "[START][GetWebPartProperties] GetSharePointOnlineAuthenticatedContextTenant for WebUrl: " + webUrl); clientContext = ObjAuth.GetSharePointOnlineAuthenticatedContextTenant(webUrl, UserName, Password); Logger.AddMessageToTraceLogFile(Constants.Logging, "[END][GetWebPartProperties] GetSharePointOnlineAuthenticatedContextTenant for WebUrl: " + webUrl); } if (clientContext != null) { Web web = clientContext.Web; clientContext.Load(web,w => w.Url); clientContext.ExecuteQueryRetry(); Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartProperties] Retrieving WebPart Properties for StorageKey: " + StorageKey.ToString() + " in the Page" + pageUrl); Console.WriteLine("[GetWebPartProperties] Retrieving WebPart Properties for StorageKey: " + StorageKey.ToString() + " in the Page" + pageUrl); var service = new Transformation.PowerShell.WebPartPagesService.WebPartPagesWebService(); service.Url = clientContext.Web.Url + Constants.WEBPART_SERVICE; Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartProperties] Service Url used to retrieve WebPart Properties : " + service.Url); Console.WriteLine("[GetWebPartProperties] Service Url used to retrieve WebPart Properties : " + service.Url); service.PreAuthenticate = true; service.Credentials = clientContext.Credentials; //For Publishing Pages, Pass - WebPartID //For SitePage or Team Site, Pass - StorageKey.ToGuid() webPartXml = service.GetWebPart2(pageUrl, StorageKey.ToGuid(), Storage.Shared, SPWebServiceBehavior.Version3); Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartProperties] Successfully retreived Web Part Properties"); Console.WriteLine("[GetWebPartProperties] Successfully retreived Web Part Properties"); webPartPropertiesFileName = sourceWebPartXmlFilesDir + "\\" + StorageKey + "_" + Constants.WEBPART_PROPERTIES_FILENAME; using (StreamWriter fsWebPartProperties = new StreamWriter(webPartPropertiesFileName)) { fsWebPartProperties.WriteLine(webPartXml); fsWebPartProperties.Flush(); } Logger.AddMessageToTraceLogFile(Constants.Logging, "[GetWebPartProperties] WebPart Properties in xml format is exported to the file " + webPartPropertiesFileName); Console.WriteLine("[GetWebPartProperties] WebPart Properties in xml format is exported to the file " + webPartPropertiesFileName); } Logger.AddMessageToTraceLogFile(Constants.Logging, "[END][GetWebPartProperties]"); Console.WriteLine("[END][GetWebPartProperties]"); if (ActionType.ToLower().Trim() == Constants.ActionType_Web.ToLower()) { Logger.AddMessageToTraceLogFile(Constants.Logging, "############## Web Part Trasnformation Utility Execution Completed for Web - GetWebPartProperties ##############"); Console.WriteLine("############## Web Part Trasnformation Utility Execution Completed for Web - GetWebPartProperties ##############"); } } catch (Exception ex) { ExceptionCsv.WriteException(ExceptionCsv.WebApplication, ExceptionCsv.SiteCollection, ExceptionCsv.WebUrl, "Web Part", ex.Message, ex.ToString(), "GetWebPartProperties", ex.GetType().ToString(), exceptionCommentsInfo1); Logger.AddMessageToTraceLogFile(Constants.Logging, "[EXCEPTION][GetWebPartProperties] Exception Message: " + ex.Message + ", Exception Comment: " + exceptionCommentsInfo1); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[EXCEPTION][GetWebPartProperties] Exception Message: " + ex.Message); Console.ForegroundColor = ConsoleColor.Gray; } return webPartPropertiesFileName; }
private string GetWebPartPropertiesServiceCall(ClientContext clientContext, string storageKey, string pageUrl) { string webPartXml = string.Empty; var service = new Transformation.PowerShell.WebPartPagesService.WebPartPagesWebService(); service.Url = clientContext.Web.Url + Constants.WEBPART_SERVICE; service.PreAuthenticate = true; service.Credentials = clientContext.Credentials; // Actual web service call which returns the information in string format webPartXml = service.GetWebPart2(pageUrl, storageKey.ToGuid(), Storage.Shared, SPWebServiceBehavior.Version3); return webPartXml; }