public Stream GetFileStream(object id, int offset = 0) { var file = GetFileById(id); if (file is SharePointFileErrorEntry) { return(null); } var fileInfo = File.OpenBinaryDirect(clientContext, (string)id); clientContext.ExecuteQuery(); var tempBuffer = TempStream.Create(); using (var str = fileInfo.Stream) { if (str != null) { str.CopyTo(tempBuffer); tempBuffer.Flush(); tempBuffer.Seek(offset, SeekOrigin.Begin); } } return(tempBuffer); }
public Stream GetFileStream(object id, int offset = 0) { var file = GetFileById(id); if (file is SharePointFileErrorEntry) { return(null); } var fileInfo = File.OpenBinaryDirect(clientContext, (string)id); clientContext.ExecuteQuery(); var tempBuffer = new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read, 8096, FileOptions.DeleteOnClose); using (var str = fileInfo.Stream) { if (str != null) { str.CopyTo(tempBuffer); tempBuffer.Flush(); tempBuffer.Seek(offset, SeekOrigin.Begin); } } return(tempBuffer); }
public void InsertFile(string serverRelativeUrl) { if (string.IsNullOrEmpty(serverRelativeUrl)) { return; } //enhancement - we could not delete the file at the end of this method //enhancement - if we did not delete this file we could compqare its created date agasint the modified date in sharepoint and only take a copy if updates have occured. using (var clientContext = new ClientContext(Settings.Default.SharePointContextUrl)) { var temp = Path.GetTempFileName(); var fileInformation = File.OpenBinaryDirect(clientContext, serverRelativeUrl); var stream = fileInformation.Stream; using (var output = System.IO.File.OpenWrite(temp)) { stream.CopyTo(output); } _document.Application.Selection.InsertFile(temp, "", false, false); System.IO.File.Delete(temp); } }
public IDocument OpenFile(string cacheName, string serverRelativeUrl) { if (serverRelativeUrl == null) { WordOM.Document nDoc = _document.Application.Documents.Add(); var document = new OfficeDocument(nDoc); return(document); } using (var clientContext = new ClientContext(Settings.Default.SharePointContextUrl)) { string temp = Path.GetTempFileName(); FileInformation fileInformation = File.OpenBinaryDirect(clientContext, serverRelativeUrl); Stream stream = fileInformation.Stream; using (FileStream output = System.IO.File.OpenWrite(temp)) { stream.CopyTo(output); } ObjectCache cache = MemoryCache.Default; cache.Add(cacheName, string.Empty, new CacheItemPolicy()); WordOM.Document d = _document.Application.Documents.Add(temp); d.UpdateOrCreatePropertyValue(Constants.WordDocumentProperties.CacheName, cacheName); System.IO.File.Delete(temp); var document = new OfficeDocument(d); return(document); } }
static void Main(string[] args) { if (args.Length < 2 && args.Length != 4) { Console.WriteLine("SPTransferSpeed.Console.exe documentItemUrl sizeInMB [username] [password]"); Console.WriteLine("SPTransferSpeed.Console.exe https://bugfree.sharepoint.com/sites/testweb/testdoclib/testfile 1024 [email protected] password"); return; } var destination = new Uri(args[0]); var sizeInMegabytes = int.Parse(args[1]); string username = null; string password = null; if (args.Length == 4) { username = args[2]; password = args[3]; } var contentLength = sizeInMegabytes * Megabyte; var randomizedContent = new byte[contentLength]; new Random().NextBytes(randomizedContent); using (var ctx = SetupContext(destination, username, password)) { // internally the context uses HttpWebRequest to upload and download files. // Thus, when uploading or downloading large files, we may experience // Unhandled Exception: System.Net.WebException: The operation has timed out // unless we change the default timeout period of 180 seconds. ctx.RequestTimeout = Timeout.Infinite; var uploadTime = Time(() => { using (var ms = new MemoryStream(randomizedContent)) { SPFile.SaveBinaryDirect(ctx, destination.LocalPath, ms, true); } }); var downloadTime = Time(() => { var fileInformation = SPFile.OpenBinaryDirect(ctx, destination.LocalPath); using (var sr = new StreamReader(fileInformation.Stream)) { sr.ReadToEnd(); } }); Console.WriteLine( "{0} MB uploaded in {1:0.0} seconds at {2:0.0} Mbit/s\r\n" + "{0} MB downloaded in {3:0.0} seconds at {4:0.0} Mbit/s", sizeInMegabytes, uploadTime, contentLength / uploadTime * 8 / Megabit, downloadTime, contentLength / downloadTime * 8 / Megabit); } }
private void GetFile(ClientContext ctx, string fileUrl, out byte[] fileArray, out string fileString) { OnVerboseNotify("Downloading " + fileUrl); var fileInfo = File.OpenBinaryDirect(ctx, fileUrl); ctx.ExecuteQueryRetry(); var dowloadFileStream = fileInfo.Stream; fileArray = GetFileAsByteArray(dowloadFileStream); fileString = GetFileString(fileArray); }
static long CSOMRead(Uri destination, string username, string password) { using (var ctx = SetupContext(destination, username, password)) { var fileInformation = SPFile.OpenBinaryDirect(ctx, destination.LocalPath); using (var sr = new StreamReader(fileInformation.Stream)) { var restored = sr.ReadToEnd(); return(long.Parse(restored)); } } }
public Stream GetFileStream(object id) { var file = GetFileById(id); if (file is SharePointFileErrorEntry) { return(null); } var fileInfo = File.OpenBinaryDirect(clientContext, (string)id); clientContext.ExecuteQuery(); return(fileInfo.Stream); }
public string DownLoadFile(string serverPath, string localPath, bool overwrite = false) { var fileName = Path.GetFileName(localPath); if (string.IsNullOrEmpty(GetFileExtention(localPath))) { fileName = GetFileName(serverPath); } else { localPath = Path.GetDirectoryName(localPath); } if (!overwrite && !string.IsNullOrEmpty(localPath) && !string.IsNullOrEmpty(fileName)) { if (CheckIfFileExist(Path.Combine(localPath, fileName))) { return("Success"); } } CreateFolderIfNotExist(localPath); using (var ctx = GetContext()) { var fullPath = GetSharePointRootFolder(serverPath, ctx); var file = ctx.Web.GetFileByServerRelativeUrl(fullPath); ctx.Load(file); ctx.ExecuteQuery(); var fileRef = file.ServerRelativeUrl; var fileInfo = File.OpenBinaryDirect(ctx, fileRef); if (fileName == null || localPath == null) { return("Failed"); } var newPath = Path.Combine(localPath, fileName); using (var fileStream = System.IO.File.Create(newPath)) { fileInfo.Stream.CopyTo(fileStream); } } return("Success"); }
public Stream GetFileStream(string serverRelativeUrl) { var context = List.Context as ClientContext; if (context == null) { throw new NotImplementedException("Bin mir noch nicht sicher, wie ich das mache..."); } var file = File.OpenBinaryDirect(context, serverRelativeUrl); context.ExecuteQuery(); return(file.Stream); }
public bool FileExistsInSharePoint(string serverRelativeUrl) { try { using (var clientContext = new ClientContext(Settings.Default.SharePointContextUrl)) { File.OpenBinaryDirect(clientContext, serverRelativeUrl); return(true); } } catch (Exception) { return(false); } }
private string DownloadLocalImage(string serverRelativeUrl) { using (var clientContext = new ClientContext(Settings.Default.SharePointContextUrl)) { string temp = Path.GetTempFileName(); var fileInformation = File.OpenBinaryDirect(clientContext, ("/" + serverRelativeUrl.ToLower().Replace(clientContext.Url.ToLower(), string.Empty)).Replace("//", "/")); // :( yep var stream = fileInformation.Stream; using (var output = System.IO.File.OpenWrite(temp)) { stream.CopyTo(output); } return(temp); } }
/// <summary> /// Returns a file as string /// </summary> /// <param name="web">The Web to process</param> /// <param name="serverRelativeUrl">The server relative URL to the file</param> /// <returns>The file contents as a string</returns> /// <remarks># /// /// Based on https://github.com/SharePoint/PnP-Sites-Core/blob/master/Core/OfficeDevPnP.Core/Extensions/FileFolderExtensions.cs /// Modified to force onpremises support /// /// </remarks> public static string GetFileByServerRelativeUrlAsString(this Web web, string serverRelativeUrl) { var file = web.GetFileByServerRelativeUrl(serverRelativeUrl); var context = web.Context; context.Load(file); context.ExecuteQueryRetry(); var spVersion = BaseTransform.GetVersion(context); Stream sourceStream; if (spVersion == SPVersion.SP2010) { sourceStream = new MemoryStream(); if (context.HasPendingRequest) { context.ExecuteQueryRetry(); } var fileBinary = File.OpenBinaryDirect((ClientContext)context, serverRelativeUrl); context.ExecuteQueryRetry(); Stream tempSourceStream = fileBinary.Stream; CopyStream(tempSourceStream, sourceStream); sourceStream.Seek(0, SeekOrigin.Begin); } else { ClientResult <Stream> stream = file.OpenBinaryStream(); web.Context.ExecuteQueryRetry(); sourceStream = stream.Value; } string returnString = string.Empty; using (Stream memStream = new MemoryStream()) { CopyStream(sourceStream, memStream); memStream.Position = 0; StreamReader reader = new StreamReader(memStream); returnString = reader.ReadToEnd(); } return(returnString); }
public void DownloadFile <TType>(TType aggregateRoot, Stream download) where TType : SharePointDocumentDomainModel { var fileInfo = File.OpenBinaryDirect(_ctx, aggregateRoot.FileRef); using (var memory = new MemoryStream()) { var buffer = new byte[1024 * 64]; int nread; while ((nread = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0) { memory.Write(buffer, 0, nread); } memory.Seek(0, SeekOrigin.Begin); memory.CopyTo(download); } }
public IDocument OpenFile(string serverRelativeUrl) { using (var clientContext = new ClientContext(Settings.Default.SharePointContextUrl)) { string temp = Path.GetTempFileName(); FileInformation fileInformation = File.OpenBinaryDirect(clientContext, serverRelativeUrl); Stream stream = fileInformation.Stream; using (FileStream output = System.IO.File.OpenWrite(temp)) { stream.CopyTo(output); } WordOM.Document d = _document.Application.Documents.Add(temp); var doc = new OfficeDocument(d); System.IO.File.Delete(temp); return(doc); } }
public string GenerateTempFile(string siteUrl, string documentUrl) { string tf = Path.GetTempFileName(); using (var clientContext = new ClientContext(siteUrl)) { documentUrl = !documentUrl.StartsWith("/") ? "/" + documentUrl : documentUrl; FileInformation fInfo = File.OpenBinaryDirect(clientContext, documentUrl); var fs = new FileStream(tf, FileMode.Create); var read = new byte[256]; int count = fInfo.Stream.Read(read, 0, read.Length); while (count > 0) { fs.Write(read, 0, count); count = fInfo.Stream.Read(read, 0, read.Length); } fs.Close(); fInfo.Stream.Close(); } return(tf); }
/// <summary> /// Copy the file from the source to the target location /// </summary> /// <param name="sourceFileUrl"></param> /// <param name="targetLocationUrl"></param> /// <remarks> /// Based on the documentation: https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/upload-large-files-sample-app-for-sharepoint /// </remarks> public string CopyAssetToTargetLocation(string sourceFileUrl, string targetLocationUrl, int fileChunkSizeInMB = 3) { // This copies the latest version of the asset to the target site collection // Going to need to add a bunch of checks to ensure the target file exists // Each sliced upload requires a unique ID. Guid uploadId = Guid.NewGuid(); // Calculate block size in bytes. int blockSize = fileChunkSizeInMB * 1024 * 1024; bool fileOverwrite = true; Stream sourceStream = null; var sourceAssetFile = _sourceClientContext.Web.GetFileByServerRelativeUrl(sourceFileUrl); _sourceClientContext.Load(sourceAssetFile, s => s.Exists); _sourceClientContext.ExecuteQueryRetry(); if (sourceAssetFile.Exists) { // Test ByPass //if 2010 then if (_sourceContextSPVersion == SPVersion.SP2010) { sourceStream = new MemoryStream(); if (_sourceClientContext.HasPendingRequest) { _sourceClientContext.ExecuteQueryRetry(); } var fileBinary = File.OpenBinaryDirect(_sourceClientContext, sourceFileUrl); _sourceClientContext.ExecuteQueryRetry(); Stream tempSourceStream = fileBinary.Stream; CopyStream(tempSourceStream, sourceStream); //Fix: https://stackoverflow.com/questions/47510815/sharepoint-uploadfile-specified-argument-was-out-of-range-of-valid-values sourceStream.Seek(0, SeekOrigin.Begin); } else { // Get the file from SharePoint ClientResult <System.IO.Stream> sourceAssetFileData = sourceAssetFile.OpenBinaryStream(); _sourceClientContext.Load(sourceAssetFile); _sourceClientContext.ExecuteQueryRetry(); sourceStream = sourceAssetFileData.Value; } using (Stream sourceFileStream = sourceStream) { string fileName = sourceAssetFile.EnsureProperty(p => p.Name); LogInfo(string.Format(LogStrings.AssetTransferUploading, fileName), LogStrings.Heading_AssetTransfer); // New File object. Microsoft.SharePoint.Client.File uploadFile; // Get the information about the folder that will hold the file. // Add the file to the target site Folder targetFolder = _targetClientContext.Web.GetFolderByServerRelativeUrl(targetLocationUrl); _targetClientContext.Load(targetFolder); _targetClientContext.ExecuteQueryRetry(); // Get the file size long fileSize = sourceFileStream.Length; // Process with two approaches if (fileSize <= blockSize) { // Use regular approach. FileCreationInformation fileInfo = new FileCreationInformation(); fileInfo.ContentStream = sourceFileStream; fileInfo.Url = fileName; fileInfo.Overwrite = fileOverwrite; uploadFile = targetFolder.Files.Add(fileInfo); _targetClientContext.Load(uploadFile); _targetClientContext.ExecuteQueryRetry(); LogInfo(string.Format(LogStrings.AssetTransferUploadComplete, fileName), LogStrings.Heading_AssetTransfer); // Return the file object for the uploaded file. return(uploadFile.EnsureProperty(o => o.ServerRelativeUrl)); } else { // Use large file upload approach. ClientResult <long> bytesUploaded = null; using (BinaryReader br = new BinaryReader(sourceFileStream)) { byte[] buffer = new byte[blockSize]; Byte[] lastBuffer = null; long fileoffset = 0; long totalBytesRead = 0; int bytesRead; bool first = true; bool last = false; // Read data from file system in blocks. while ((bytesRead = br.Read(buffer, 0, buffer.Length)) > 0) { totalBytesRead = totalBytesRead + bytesRead; // You've reached the end of the file. if (totalBytesRead == fileSize) { last = true; // Copy to a new buffer that has the correct size. lastBuffer = new byte[bytesRead]; Array.Copy(buffer, 0, lastBuffer, 0, bytesRead); } if (first) { using (MemoryStream contentStream = new MemoryStream()) { // Add an empty file. FileCreationInformation fileInfo = new FileCreationInformation(); fileInfo.ContentStream = contentStream; fileInfo.Url = fileName; fileInfo.Overwrite = fileOverwrite; uploadFile = targetFolder.Files.Add(fileInfo); // Start upload by uploading the first slice. using (MemoryStream s = new MemoryStream(buffer)) { // Call the start upload method on the first slice. bytesUploaded = uploadFile.StartUpload(uploadId, s); _targetClientContext.ExecuteQueryRetry(); // fileoffset is the pointer where the next slice will be added. fileoffset = bytesUploaded.Value; } // You can only start the upload once. first = false; } } else { // Get a reference to your file. var fileUrl = targetFolder.ServerRelativeUrl + System.IO.Path.AltDirectorySeparatorChar + fileName; uploadFile = _targetClientContext.Web.GetFileByServerRelativeUrl(fileUrl); if (last) { // Is this the last slice of data? using (MemoryStream s = new MemoryStream(lastBuffer)) { // End sliced upload by calling FinishUpload. uploadFile = uploadFile.FinishUpload(uploadId, fileoffset, s); _targetClientContext.ExecuteQueryRetry(); LogInfo(string.Format(LogStrings.AssetTransferUploadComplete, fileName), LogStrings.Heading_AssetTransfer); // Return the file object for the uploaded file. return(fileUrl); } } else { using (MemoryStream s = new MemoryStream(buffer)) { // Continue sliced upload. bytesUploaded = uploadFile.ContinueUpload(uploadId, fileoffset, s); _targetClientContext.ExecuteQueryRetry(); // Update fileoffset for the next slice. fileoffset = bytesUploaded.Value; } } } } } } } } LogWarning("Asset was not transferred as it was not found in the source web. Asset: " + sourceFileUrl, LogStrings.Heading_AssetTransfer); return(null); }