public void UploadFilesInFolder(ClientContext context, Web web, ShContentFolder configFolder) { Log.Info("Uploading files from contentfolder " + configFolder.FolderName); var assetLibrary = web.Lists.GetByTitle(configFolder.ListName); context.Load(assetLibrary, l => l.RootFolder); context.ExecuteQuery(); var uploadTargetFolder = Url.Combine(assetLibrary.RootFolder.ServerRelativeUrl, configFolder.FolderUrl); var configRootFolder = Path.Combine(_contentDirectoryPath, configFolder.FolderName); if (!web.DoesFolderExists(uploadTargetFolder)) { web.Folders.Add(uploadTargetFolder); } context.ExecuteQuery(); foreach (string folder in Directory.GetDirectories(configRootFolder, "*", SearchOption.AllDirectories)) { var folderName = Url.Combine(uploadTargetFolder, folder.Replace(configRootFolder, "").Replace("\\", "/")); if (!web.DoesFolderExists(folderName)) { web.Folders.Add(folderName); } } context.ExecuteQuery(); foreach (string filePath in Directory.GetFiles(configRootFolder, "*", SearchOption.AllDirectories)) { var fileUrl = Url.Combine(uploadTargetFolder, filePath.Replace(configRootFolder, "").Replace("\\", "/")); var newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(filePath), Url = fileUrl, Overwrite = true }; Microsoft.SharePoint.Client.File uploadFile = assetLibrary.RootFolder.Files.Add(newFile); context.Load(uploadFile); context.ExecuteQuery(); } }
public void UploadFilesInFolder(ClientContext context, Web web, ShContentFolder contentFolder, bool incrementalUpload) { Log.Info("Uploading files from contentfolder " + contentFolder.FolderName); string uploadTargetFolder; Folder rootFolder; web.Lists.EnsureSiteAssetsLibrary(); context.Load(web.Lists); context.ExecuteQuery(); if (!string.IsNullOrEmpty(contentFolder.ListUrl)) { context.Load(web, w => w.ServerRelativeUrl); context.ExecuteQuery(); var listUrl = Url.Combine(web.ServerRelativeUrl, contentFolder.ListUrl); rootFolder = web.GetFolderByServerRelativeUrl(listUrl); context.Load(rootFolder); context.ExecuteQuery(); uploadTargetFolder = Url.Combine(listUrl, contentFolder.FolderUrl); } else if (!string.IsNullOrEmpty(contentFolder.ListName)) { var assetLibrary = web.Lists.GetByTitle(contentFolder.ListName); context.Load(assetLibrary, l => l.Title, l => l.RootFolder); context.ExecuteQuery(); rootFolder = assetLibrary.RootFolder; uploadTargetFolder = Url.Combine(assetLibrary.RootFolder.ServerRelativeUrl, contentFolder.FolderUrl); } else { Log.ErrorFormat("You need to specify either ListName or ListUrl for the Content Folder {0}", contentFolder.FolderName); return; } var configRootFolder = Path.Combine(_contentDirectoryPath, contentFolder.FolderName); EnsureTargetFolder(context, web, rootFolder.ServerRelativeUrl, contentFolder.FolderUrl, uploadTargetFolder); EnsureAllContentFolders(context, web, configRootFolder, uploadTargetFolder); List <ShFileProperties> filePropertiesCollection = null; if (!string.IsNullOrEmpty(contentFolder.PropertiesFile)) { var propertiesFilePath = Path.Combine(configRootFolder, contentFolder.PropertiesFile); var filePersistanceProvider = new FilePersistanceProvider <List <ShFileProperties> >(propertiesFilePath); filePropertiesCollection = filePersistanceProvider.Load(); } context.Load(context.Site, site => site.ServerRelativeUrl); context.Load(context.Web, w => w.ServerRelativeUrl, w => w.Language); context.ExecuteQuery(); String[] excludedFileExtensions = { }; if (!string.IsNullOrEmpty(contentFolder.ExcludeExtensions)) { excludedFileExtensions = contentFolder.ExcludeExtensions.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); } var files = Directory.GetFiles(configRootFolder, "*", SearchOption.AllDirectories) .Where(file => !excludedFileExtensions.Contains(Path.GetExtension(file).ToLower())).ToList(); if (incrementalUpload) { files = files.Where(f => !LastUpload.ContainsKey(contentFolder.FolderName) || new FileInfo(f).LastWriteTimeUtc > LastUpload[contentFolder.FolderName]).ToList(); } int filesUploaded = 0; foreach (string filePath in files) { UploadAndPublishSingleFile(context, web, configRootFolder, contentFolder, uploadTargetFolder, rootFolder, filePropertiesCollection, filePath); filesUploaded++; } if (filesUploaded == 0) { Log.Info("No files updated since last upload."); } else { Log.InfoFormat("{0} file(s) uploaded", filesUploaded); } if (LastUpload.ContainsKey(contentFolder.FolderName)) { LastUpload[contentFolder.FolderName] = DateTime.UtcNow; } else { LastUpload.Add(contentFolder.FolderName, DateTime.UtcNow); } }
private void UploadAndPublishSingleFile(ClientContext context, Web web, string configRootFolder, ShContentFolder contentFolder, string uploadTargetFolder, Folder rootFolder, List <ShFileProperties> filePropertiesCollection, string filePath) { var pathToFileFromRootFolder = filePath.Replace(configRootFolder.TrimEnd(new[] { '\\' }) + "\\", ""); var fileName = Path.GetFileName(pathToFileFromRootFolder); pathToFileFromRootFolder = pathToFileFromRootFolder.Replace("\\", "/"); if (!string.IsNullOrEmpty(contentFolder.PropertiesFile) && contentFolder.PropertiesFile == fileName) { Log.DebugFormat("Skipping file upload of {0} since it's used as a configuration file", fileName); return; } ShFileProperties fileProperties = null; if (filePropertiesCollection != null) { fileProperties = filePropertiesCollection.SingleOrDefault(f => f.Path == pathToFileFromRootFolder); } Log.DebugFormat("Uploading file {0} to {1}", fileName, contentFolder.ListUrl); var fileUrl = GetFileUrl(uploadTargetFolder, pathToFileFromRootFolder, fileProperties); web.CheckOutFile(fileUrl); if (fileProperties == null || fileProperties.ReplaceContent) { var newFile = GetFileCreationInformation(context, fileUrl, filePath, pathToFileFromRootFolder, fileProperties); File uploadFile = rootFolder.Files.Add(newFile); context.Load(uploadFile); context.ExecuteQuery(); } var reloadedFile = web.GetFileByServerRelativeUrl(fileUrl); context.Load(reloadedFile); context.ExecuteQuery(); ApplyFileProperties(context, fileProperties, reloadedFile); try { reloadedFile.PublishFileToLevel(FileLevel.Published); } catch { Log.Warn("Couldn't publish file " + fileUrl); } }