private SynchronizationResult SynchronizeFolderInternal(string sourcePath, Web web, string destinationPrefixedUrl, FileLevel publishingLevel) { // TODO: Consider adding recursive, but think about subfolder creation. if (sourcePath == null) { throw new ArgumentNullException("sourcePath"); } if (web == null) { throw new ArgumentNullException("web"); } if (destinationPrefixedUrl == null) { throw new ArgumentNullException("destinationPrefixedUrl"); } var processedFiles = new List <UploadInfo>(); try { //if (recursive) //{ // throw new NotSupportedException("Recursive not supported yet"); //} var assembly = Assembly.GetExecutingAssembly(); var fvi = FileVersionInfo.GetVersionInfo(assembly.Location); _configuration.Log.Information("IonFar SharePoint Synchronizer v" + fvi.FileVersion); _configuration.Log.Information("== Uploading folder '{0}' ==", destinationPrefixedUrl); var destinationServerRelativeUrl = SPUrlUtility.ResolveServerRelativeUrl(_configuration.ContextManager.CurrentContext.Site, web, destinationPrefixedUrl); var destinationFolder = web.GetFolderByServerRelativeUrl(destinationServerRelativeUrl); var filePaths = System.IO.Directory.EnumerateFiles(sourcePath); foreach (var filePath in filePaths) { var fileName = System.IO.Path.GetFileName(filePath); var fileUrl = SPUrlUtility.Combine(destinationServerRelativeUrl, fileName); using (var localStream = System.IO.File.OpenRead(filePath)) { var uploadResult = UploadFileInternal(localStream, web, destinationFolder, filePath, fileUrl, publishingLevel); processedFiles.Add(uploadResult.Item2); } } return(new SynchronizationResult(processedFiles, successful: true, error: null)); } catch (Exception ex) { _configuration.Log.Error( "Synchronization failed and the environment has been left in a partially complete state, manual intervention may be required.\nException: {0}", ex ); return(new SynchronizationResult(processedFiles, successful: false, error: ex)); } }
/// <summary> /// Uploads a file, replacing any existing file with a new version. /// </summary> /// <param name="stream">Contents of the file to upload</param> /// <param name="web">Web the destination folder exists in</param> /// <param name="destinationPrefixedUrl">Server relative URL of the destination file; may use '~sitecollection/' or '~site/' prefix; the folder must already exist</param> /// <param name="publishingLevel">Target final state of the file, e.g. Published or Draft</param> /// <returns>The updated or newly created file</returns> public File UploadFile(System.IO.Stream stream, Web web, string destinationPrefixedUrl, FileLevel publishingLevel) { var destinationFileServerRelativeUrl = SPUrlUtility.ResolveServerRelativeUrl(_configuration.ContextManager.CurrentContext.Site, web, destinationPrefixedUrl); var destinationFolderServerRelativeUrl = destinationFileServerRelativeUrl.Substring(0, destinationFileServerRelativeUrl.LastIndexOf('/')); // _configuration.Log.Information("DEBUG: file '{0}', folder '{1}'", fileName, folderServerRelativeUrl); var destinationFolder = web.GetFolderByServerRelativeUrl(destinationFolderServerRelativeUrl); var result = UploadFileInternal(stream, web, destinationFolder, null, destinationFileServerRelativeUrl, publishingLevel); return(result.Item1); }
private Folder EnsureFolderInternal(Web web, string folderPrefixedUrl) { if (web == null) { throw new ArgumentNullException("web"); } if (folderPrefixedUrl == null) { throw new ArgumentNullException("folderPrefixedUrl"); } var folderServerRelativeUrl = SPUrlUtility.ResolveServerRelativeUrl(_configuration.ContextManager.CurrentContext.Site, web, folderPrefixedUrl); _configuration.Log.Information("Ensuring folder '{0}' exists, creating if necessary", folderServerRelativeUrl); if (!_configuration.ContextManager.CurrentContext.Web.IsPropertyAvailable("ServerRelativeUrl")) { _configuration.ContextManager.CurrentContext.Load(_configuration.ContextManager.CurrentContext.Web); _configuration.ContextManager.CurrentContext.ExecuteQuery(); } if (!folderServerRelativeUrl.StartsWith(_configuration.ContextManager.CurrentContext.Web.ServerRelativeUrl)) { var msg = string.Format("You should not create a folder above the current Web root (web root: {0}, folder: {1})", _configuration.ContextManager.CurrentContext.Web.ServerRelativeUrl, folderServerRelativeUrl); throw new Exception(msg); } var folder = _configuration.ContextManager.CurrentContext.Web.GetFolderByServerRelativeUrl(folderServerRelativeUrl); _configuration.ContextManager.CurrentContext.Load(folder); try { _configuration.ContextManager.CurrentContext.ExecuteQuery(); } catch (ServerException) { var segments = folderServerRelativeUrl.Split(new[] { '/' }).ToList(); var lastSegment = segments.Last(); var parentFolderPath = string.Join("/", segments.Take(segments.Count() - 1)); // Recurse var parentFolder = EnsureFolderInternal(web, parentFolderPath); folder = parentFolder.Folders.Add(lastSegment); _configuration.ContextManager.CurrentContext.Load(folder); _configuration.Log.Information("Creating folder '{0}' under parent '{1}'", lastSegment, parentFolder); _configuration.ContextManager.CurrentContext.ExecuteQuery(); } return(folder); }