public void UnGhostFile(string absoluteFilePath, string outPutFolder, string OperationType, string SharePointOnline_OR_OnPremise = Constants.OnPremise, string UserName = "******", string Password = "******", string Domain = "NA") { string fileName = string.Empty; string newFileName = string.Empty; string directoryName = string.Empty; bool headerCSVColumns = false; string exceptionCommentsInfo1 = string.Empty; GhostingAndUnGhosting_Initialization(outPutFolder, "UNGHOST"); Logger.AddMessageToTraceLogFile(Constants.Logging, "############## Un Ghosting - Trasnformation Utility Execution Started - For Web ##############"); Console.WriteLine("############## Un Ghosting - Trasnformation Utility Execution Started - For Web ##############"); Logger.AddMessageToTraceLogFile(Constants.Logging, "[DATE TIME] " + Logger.CurrentDateTime()); Console.WriteLine("[DATE TIME] " + Logger.CurrentDateTime()); Logger.AddMessageToTraceLogFile(Constants.Logging, "[START] ::: UnGhostFile"); Console.WriteLine("[START] ::: UnGhostFile"); Logger.AddMessageToTraceLogFile(Constants.Logging, "[UnGhostFile] Initiated Logger and Exception Class. Logger and Exception file will be available in path " + outPutFolder); Console.WriteLine("[UnGhostFile] Initiated Logger and Exception Class. Logger and Exception file will be available in path" + outPutFolder); try { exceptionCommentsInfo1 = "FilePath: " + absoluteFilePath; AuthenticationHelper ObjAuth = new AuthenticationHelper(); ClientContext clientContext = null; Uri fileUrl = new Uri(absoluteFilePath); clientContext = new ClientContext(fileUrl.GetComponents(UriComponents.SchemeAndServer, UriFormat.UriEscaped)); Uri siteUrl = Web.WebUrlFromPageUrlDirect(clientContext, fileUrl); clientContext = new ClientContext(siteUrl); Logger.AddMessageToTraceLogFile(Constants.Logging, "[UnGhostFile] WebUrl is " + siteUrl.ToString()); Console.WriteLine("[UnGhostFile] WebUrl is " + siteUrl.ToString()); ExceptionCsv.WebUrl = siteUrl.ToString(); //SharePoint on-premises / SharePoint Online Dedicated => OP (On-Premises) if (SharePointOnline_OR_OnPremise.ToUpper() == Constants.OnPremise) { Logger.AddMessageToTraceLogFile(Constants.Logging, "[START][UnGhostFile] GetNetworkCredentialAuthenticatedContext for WebUrl: " + siteUrl.ToString()); clientContext = ObjAuth.GetNetworkCredentialAuthenticatedContext(siteUrl.ToString(), UserName, Password, Domain); Logger.AddMessageToTraceLogFile(Constants.Logging, "[END][UnGhostFile] GetNetworkCredentialAuthenticatedContext for WebUrl: " + siteUrl.ToString()); } //SharePointOnline => OL (Online) else if (SharePointOnline_OR_OnPremise.ToUpper() == Constants.Online) { Logger.AddMessageToTraceLogFile(Constants.Logging, "[START][UnGhostFile] GetSharePointOnlineAuthenticatedContextTenant for WebUrl: " + siteUrl.ToString()); clientContext = ObjAuth.GetSharePointOnlineAuthenticatedContextTenant(siteUrl.ToString(), UserName, Password); Logger.AddMessageToTraceLogFile(Constants.Logging, "[END][UnGhostFile] GetSharePointOnlineAuthenticatedContextTenant for WebUrl: " + siteUrl.ToString()); } if (clientContext != null) { Microsoft.SharePoint.Client.File file = clientContext.Web.GetFileByServerRelativeUrl(fileUrl.AbsolutePath); clientContext.Load(file); clientContext.ExecuteQuery(); directoryName = GetLibraryName(fileUrl.ToString(), siteUrl.ToString(), fileName); Folder folder = clientContext.Web.GetFolderByServerRelativeUrl(directoryName); clientContext.Load(folder); clientContext.ExecuteQuery(); fileName = file.Name; newFileName = GetNextFileName(fileName); string path = System.IO.Directory.GetCurrentDirectory(); string downloadedFilePath = path + "\\" + newFileName; using (WebClient myWebClient = new WebClient()) { myWebClient.Credentials = CredentialCache.DefaultNetworkCredentials; myWebClient.DownloadFile(absoluteFilePath, downloadedFilePath); } Microsoft.SharePoint.Client.File uploadedFile = FileFolderExtensions.UploadFile(folder, newFileName, downloadedFilePath, true); if (uploadedFile.CheckOutType.Equals(CheckOutType.Online)) { uploadedFile.CheckIn("File is UnGhotsed and Updated", CheckinType.MinorCheckIn); } clientContext.Load(uploadedFile); clientContext.ExecuteQuery(); bool UnGhostFile_Status = false; if (OperationType.ToUpper().Trim().Equals("MOVE")) { uploadedFile.MoveTo(directoryName + fileName, MoveOperations.Overwrite); clientContext.ExecuteQuery(); Logger.AddMessageToTraceLogFile(Constants.Logging, "[UnGhostFile] Created the new version of the file " + fileName + " using MOVE operation"); Console.WriteLine("[UnGhostFile] Created the new version of the file " + fileName + " using MOVE operation"); UnGhostFile_Status = true; } else if (OperationType.ToUpper().Trim().Equals("COPY")) { uploadedFile.CopyTo(directoryName + fileName, true); clientContext.ExecuteQuery(); Logger.AddMessageToTraceLogFile(Constants.Logging, "[UnGhostFile] Created the new version of the file " + fileName + " using COPY operation"); Console.WriteLine("[UnGhostFile] Created the new version of the file " + fileName + " using COPY operation"); UnGhostFile_Status = true; } else { Logger.AddMessageToTraceLogFile(Constants.Logging, "[UnGhostFile] The Operation input in not provided to unghost the file " + fileName + ""); Console.WriteLine("[UnGhostFile] The Operation input in not provided to unghost the file " + fileName + ""); } //If Un-Ghost File Operation is Successful if (UnGhostFile_Status) { GhostingAndUnGhostingBase objUGBase = new GhostingAndUnGhostingBase(); objUGBase.FileName = fileName; objUGBase.FilePath = absoluteFilePath; objUGBase.WebUrl = siteUrl.ToString(); objUGBase.SiteCollection = Constants.NotApplicable; objUGBase.WebApplication = Constants.NotApplicable; if (objUGBase != null) { FileUtility.WriteCsVintoFile(outPutFolder + @"\" + Constants.UnGhosting_Output, objUGBase, ref headerCSVColumns); } //Deleting the files, which is downloaded to Un-Ghost the file if (System.IO.File.Exists(downloadedFilePath)) { System.IO.File.Delete(downloadedFilePath); } //Deleting the files, which is downloaded to Un-Ghost the file } } } catch (Exception ex) { Logger.AddMessageToTraceLogFile(Constants.Logging, "[Exception] UnGhostFile. Exception Message: " + ex.Message); ExceptionCsv.WriteException(ExceptionCsv.WebApplication, ExceptionCsv.SiteCollection, ExceptionCsv.WebUrl, "UnGhost", ex.Message, ex.ToString(), "UnGhostFile", ex.GetType().ToString(), exceptionCommentsInfo1); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("[Exception] UnGhostFile. Exception Message: " + ex.Message); Console.ForegroundColor = ConsoleColor.Gray; } Logger.AddMessageToTraceLogFile(Constants.Logging, "[END] ::: UnGhostFile"); Console.WriteLine("[END] ::: UnGhostFile"); Logger.AddMessageToTraceLogFile(Constants.Logging, "############## UnGhostFile - Trasnformation Utility Execution Completed for Web ##############"); Console.WriteLine("############## UnGhostFile - Trasnformation Utility Execution Completed for Web ##############"); }