Upload a File to SharePoint using Client Object Model

Following is the complete process of uploading/creating a file on the SharePoint server using the Client Object Model. The following process should also be implied for some of the ListItems. Traditionally, we use ListItemCreationInformation class to create a ListItem. However, if your List is a

  • Web Page Library
  • Picture Library

then also you have to use the below process to create their ListItem otherwise, you’ll get the Microsoft.SharePoint.Client.ServerException, use the SPFileCollection.Add() to create file. This is one of such example where I am creating a ListItem for the Web Page Library List

using System;
using System.Collections.Generic;
using IO = System.IO;
using System.Linq;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
string siteUrl = "http://siteUrl";
SP.ClientContext ctx = new SP.ClientContext(siteUrl);
SP.List list = ctx.Web.Lists.GetByTitle("ListTitle");
byte[] bytes = IO.File.ReadAllBytes(strFilePath); //strFilePath is the full path of the file that needs to be uploaded

IO.MemoryStream mStream = new IO.MemoryStream(bytes);

SP.FileCreationInformation fileInfo = new SP.FileCreationInformation();
fileInfo.ContentStream = mStream;
fileInfo.Overwrite = true;
fileInfo.Url = "https://siteUrl/Subsite1/Subsite2/SitePages/TestPage.aspx";
SP.File file = list.RootFolder.Files.Add(fileInfo);


SP.ListItem listItem = file.ListItemAllFields;

//If you have the MetaInfo that you want to restore
listItem["MetaInfo"] = ltItemEntity.FieldValuesAsTexts["MetaInfo"];



To update large files, up to 2GB, check out my other blog at, https://realmpksharepoint.wordpress.com/2014/04/29/upload-large-files-to-the-sharepoint-documentlibrary-using-rpc-from-a-desktop-application-c/.

Also, you can visit the following post to upload files up to 10GB to SharePoint Online,


2 thoughts on “Upload a File to SharePoint using Client Object Model

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 )

Twitter picture

You are commenting using your Twitter 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.