Upload a File with CSOM

This is an example of how to upload a file with the C# Client Side Object Model (CSOM):

internal static File UploadFile(ClientContext clientContext, Web web, string filePath, Folder folder, string fileName, string title)
 string target = folder.ServerRelativeUrl + "/" + fileName;
FileCreationInformation fci = new FileCreationInformation();
 fci.Overwrite = true;
 fci.Url = target;
 fci.Content = System.IO.File.ReadAllBytes(filePath);
File uploadedFile = folder.Files.Add(fci);
 uploadedFile.ListItemAllFields["Title"] = title;
if (uploadedFile.CheckOutType != CheckOutType.None)
 uploadedFile.CheckIn("Initial Upload", CheckinType.MajorCheckIn);
 return uploadedFile;

Note that this method also lets you set a title for the document, as well as the file name, and it checks the document in for you if required.

Upload a File with CSOM

2 thoughts on “Upload a File with CSOM

  1. Dilip Nikam says:

    Very useful code you shared. Thanks for that. Its save my lots of time.

    In CSOM can we get the uploaded document full URL?
    If yes then request you to pls share the code.

    Thanks in Advance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.