public void FolderCopyMoveTests() { var azureDirectory = azureBlobAdapter.Directory; var newFolderName = azureBlobAdapter.Path.Combine(blobRootFolderName, @"new folder\subfolder\leaf"); //azureDirectory.CreateDirectory(newFolderName); //this tests both azureDirectory.GetFiles(blobFolderName) azureDirectory.EnumerateFileSystemEntries(blobFolderName) FileFolderExtensions.CopyDirectory(azureBlobAdapter, blobFolderName, newFolderName); var olditems = azureDirectory.EnumerateFileSystemEntries(blobFolderName, null, System.IO.SearchOption.AllDirectories); var newitems = azureDirectory.EnumerateFileSystemEntries(newFolderName, null, System.IO.SearchOption.AllDirectories); foreach (var fileName in newitems) { Debug.WriteLine("File Name \t" + fileName); } Assert.Equal(newitems.Count(), olditems.Count()); var sourceRootFolderName = azureBlobAdapter.Path.Combine(blobRootFolderName, @"new folder"); // The parent directory of the destination path must exist. var movedRootFolderName = @"\\hostname\shared\movedtop\movedsub\moved"; azureDirectory.CreateDirectory(movedRootFolderName); azureDirectory.Move(sourceRootFolderName, movedRootFolderName); var ExistsResult = azureDirectory.Exists(sourceRootFolderName); Assert.False(ExistsResult); var ExistsResultTrue = azureDirectory.Exists(@"\\hostname\shared\movedtop\movedsub\moved\subfolder\leaf"); Assert.True(ExistsResultTrue); //clean up so next time unit test will run azureDirectory.Delete(@"\\hostname\shared\movedtop", true); }
protected override void ExecuteCmdlet() { if (!System.IO.Path.IsPathRooted(Path)) { Path = System.IO.Path.Combine(SessionState.Path.CurrentFileSystemLocation.Path, Path); } ClientObjectExtensions.EnsureProperty <Microsoft.SharePoint.Client.Web, string>(SelectedWeb, w => w.ServerRelativeUrl); var folder = FileFolderExtensions.EnsureFolder(SelectedWeb, SelectedWeb.RootFolder, Folder); var fileUrl = UrlUtility.Combine(folder.ServerRelativeUrl, System.IO.Path.GetFileName(Path)); // Check if the file exists if (Checkout) { try { var existingFile = SelectedWeb.GetFileByServerRelativeUrl(fileUrl); ClientObjectExtensions.EnsureProperty <File, bool>(existingFile, f => f.Exists); if (existingFile.Exists) { FileFolderExtensions.CheckOutFile(SelectedWeb, fileUrl); } } catch { // Swallow exception, file does not exist } } var file = folder.UploadFile(new FileInfo(Path).Name, Path, true); if (Values != null) { var item = file.ListItemAllFields; foreach (var key in Values.Keys) { item[key as string] = Values[key]; } item.Update(); ClientContext.ExecuteQueryRetry(); } if (Checkout) { FileFolderExtensions.CheckInFile(SelectedWeb, fileUrl, CheckinType.MajorCheckIn, CheckInComment); } if (Publish) { FileFolderExtensions.PublishFile(SelectedWeb, fileUrl, PublishComment); } if (Approve) { FileFolderExtensions.ApproveFile(SelectedWeb, fileUrl, ApproveComment); } WriteObject(file); }
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 ##############"); }