How to download/upload files from/to SharePoint using CSOM?

How to download/upload files from/to SharePoint using CSOM?

To download and upload files from/to SharePoint using CSOM (Client-side Object Model), you can use the File class and its methods. Here's how to do it:

Downloading a file

// Get the context and file object using (var ctx = new ClientContext("https://yoursharepointsite.com")) { var file = ctx.Web.GetFileByServerRelativeUrl("/Shared Documents/MyFile.docx"); // Download the file var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, file.ServerRelativeUrl); var fileName = Path.Combine(@"C:\Downloads", file.Name); using (var fileStream = System.IO.File.Create(fileName)) { fileInfo.Stream.CopyTo(fileStream); } } 

This code gets the context for your SharePoint site and the file object for the file you want to download. Then, it uses the OpenBinaryDirect method of the File class to download the file and save it to the local file system.

Uploading a file

// Get the context and file object using (var ctx = new ClientContext("https://yoursharepointsite.com")) { var fileUrl = "/Shared Documents/MyFile.docx"; var localFilePath = @"C:\MyFile.docx"; // Get the folder object to upload the file to var folder = ctx.Web.GetFolderByServerRelativeUrl("/Shared Documents"); ctx.Load(folder); ctx.ExecuteQuery(); // Upload the file using (var fileStream = new FileStream(localFilePath, FileMode.Open)) { var fileInfo = new FileCreationInformation { ContentStream = fileStream, Url = fileUrl, Overwrite = true }; var uploadFile = folder.Files.Add(fileInfo); ctx.Load(uploadFile); ctx.ExecuteQuery(); } } 

This code gets the context for your SharePoint site, the file URL and local file path for the file you want to upload, and the folder object to upload the file to. Then, it uses the Files.Add method of the folder object to upload the file and save it to the SharePoint site.

Note that in both cases, you'll need to ensure that you have the appropriate permissions to access and modify files on the SharePoint site.

Examples

  1. "C# CSOM SharePoint upload file"

    • Code Implementation:
      using (ClientContext context = new ClientContext("https://yoursharepointsite")) { // Authenticate if needed // Get the destination library List library = context.Web.Lists.GetByTitle("Documents"); // Prepare the file FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes("path/to/upload/file.txt"), Overwrite = true, Url = "file.txt" }; // Upload the file Microsoft.SharePoint.Client.File uploadFile = library.RootFolder.Files.Add(newFile); context.ExecuteQuery(); } 
    • Description: Uploads a file to a SharePoint document library using CSOM in C#.
  2. "C# CSOM SharePoint download file"

    • Code Implementation:
      using (ClientContext context = new ClientContext("https://yoursharepointsite")) { // Authenticate if needed // Get the file Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl("/sites/yoursite/Shared Documents/file.txt"); context.Load(file); context.ExecuteQuery(); // Download the file FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl); using (var fileStream = System.IO.File.Create("path/to/save/downloaded_file.txt")) { fileInfo.Stream.CopyTo(fileStream); } } 
    • Description: Downloads a file from a SharePoint document library using CSOM in C#.
  3. "C# CSOM SharePoint upload file with metadata"

    • Code Implementation:
      using (ClientContext context = new ClientContext("https://yoursharepointsite")) { // Authenticate if needed // Get the destination library List library = context.Web.Lists.GetByTitle("Documents"); // Prepare the file with metadata FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes("path/to/upload/file.txt"), Overwrite = true, Url = "file.txt" }; // Add metadata newFile.ListItemAllFields = new ListItemCreationInformation(); newFile.ListItemAllFields["Title"] = "File Title"; newFile.ListItemAllFields["Description"] = "File Description"; // Upload the file with metadata Microsoft.SharePoint.Client.File uploadFile = library.RootFolder.Files.Add(newFile); context.ExecuteQuery(); } 
    • Description: Uploads a file to a SharePoint document library with associated metadata using CSOM in C#.
  4. "C# CSOM SharePoint download file with specific version"

    • Code Implementation:
      using (ClientContext context = new ClientContext("https://yoursharepointsite")) { // Authenticate if needed // Get the file with specific version Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl("/sites/yoursite/Shared Documents/file.txt"); context.Load(file, f => f.Versions); context.ExecuteQuery(); // Download a specific version of the file FileVersionCollection versions = file.Versions; FileVersion version = versions.GetByLabel("1.0"); // Replace with the desired version context.Load(version); context.ExecuteQuery(); FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, version.Url); using (var fileStream = System.IO.File.Create("path/to/save/downloaded_file.txt")) { fileInfo.Stream.CopyTo(fileStream); } } 
    • Description: Downloads a specific version of a file from a SharePoint document library using CSOM in C#.
  5. "C# CSOM SharePoint upload large file"

    • Code Implementation:
      using (ClientContext context = new ClientContext("https://yoursharepointsite")) { // Authenticate if needed // Get the destination library List library = context.Web.Lists.GetByTitle("Documents"); // Prepare the large file FileCreationInformation newFile = new FileCreationInformation { ContentStream = new FileStream("path/to/large/file.zip", FileMode.Open, FileAccess.Read), Overwrite = true, Url = "large_file.zip" }; // Upload the large file Microsoft.SharePoint.Client.File uploadFile = library.RootFolder.Files.Add(newFile); context.ExecuteQuery(); } 
    • Description: Uploads a large file to a SharePoint document library using CSOM in C#.
  6. "C# CSOM SharePoint download file with CAML query"

    • Code Implementation:
      using (ClientContext context = new ClientContext("https://yoursharepointsite")) { // Authenticate if needed // Build CAML query to get the file CamlQuery query = new CamlQuery { ViewXml = @"<View> <Query> <Where> <Eq> <FieldRef Name='FileLeafRef'/> <Value Type='File'>file.txt</Value> </Eq> </Where> </Query> </View>" }; // Get the file using CAML query Microsoft.SharePoint.Client.ListItemCollection items = context.Web.Lists.GetByTitle("Documents").GetItems(query); context.Load(items); context.ExecuteQuery(); // Download the file Microsoft.SharePoint.Client.File file = items[0].File; FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl); using (var fileStream = System.IO.File.Create("path/to/save/downloaded_file.txt")) { fileInfo.Stream.CopyTo(fileStream); } } 
    • Description: Downloads a file from a SharePoint document library using CSOM and a CAML query in C#.
  7. "C# CSOM SharePoint upload file to specific folder"

    • Code Implementation:
      using (ClientContext context = new ClientContext("https://yoursharepointsite")) { // Authenticate if needed // Get the destination folder Folder folder = context.Web.GetFolderByServerRelativeUrl("/sites/yoursite/Shared Documents/Subfolder"); // Prepare the file FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes("path/to/upload/file.txt"), Overwrite = true, Url = "file.txt" }; // Upload the file to the specific folder Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(newFile); context.ExecuteQuery(); } 
    • Description: Uploads a file to a specific folder within a SharePoint document library using CSOM in C#.
  8. "C# CSOM SharePoint download multiple files"

    • Code Implementation:
      using (ClientContext context = new ClientContext("https://yoursharepointsite")) { // Authenticate if needed // Specify multiple file URLs to download string[] fileUrls = { "/sites/yoursite/Shared Documents/file1.txt", "/sites/yoursite/Shared Documents/file2.txt" }; foreach (var fileUrl in fileUrls) { Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(fileUrl); context.Load(file); context.ExecuteQuery(); // Download each file FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl); using (var fileStream = System.IO.File.Create($"path/to/save/{file.Name}")) { fileInfo.Stream.CopyTo(fileStream); } } } 
    • Description: Downloads multiple files from a SharePoint document library using CSOM in C#.
  9. "C# CSOM SharePoint upload file with content type"

    • Code Implementation:
      using (ClientContext context = new ClientContext("https://yoursharepointsite")) { // Authenticate if needed // Get the destination library List library = context.Web.Lists.GetByTitle("Documents"); // Prepare the file with content type FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes("path/to/upload/file.txt"), Overwrite = true, Url = "file.txt" }; // Set content type newFile.ListItemAllFields = new ListItemCreationInformation(); newFile.ListItemAllFields["ContentTypeId"] = "0x0101"; // Replace with the appropriate content type ID // Upload the file with content type Microsoft.SharePoint.Client.File uploadFile = library.RootFolder.Files.Add(newFile); context.ExecuteQuery(); } 
    • Description: Uploads a file to a SharePoint document library with a specified content type using CSOM in C#.
  10. "C# CSOM SharePoint download file with specific properties"

    • Code Implementation:
      using (ClientContext context = new ClientContext("https://yoursharepointsite")) { // Authenticate if needed // Get the file with specific properties Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl("/sites/yoursite/Shared Documents/file.txt"); context.Load(file, f => f.ServerRelativeUrl, f => f.Name, f => f.Length); context.ExecuteQuery(); // Download the file with specific properties FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl); using (var fileStream = System.IO.File.Create($"path/to/save/{file.Name}")) { fileInfo.Stream.CopyTo(fileStream); } // Access other properties if needed (e.g., file.Length) long fileSize = file.Length; } 
    • Description: Downloads a file from a SharePoint document library with specific properties (e.g., file length) using CSOM in C#.

More Tags

protoc autoload extrafont special-characters word-boundary format image-upload corrupt swiper.js lexer

More C# Questions

More Electrochemistry Calculators

More Chemical reactions Calculators

More Everyday Utility Calculators

More Math Calculators