How to upload a document in SharePoint 2010 using Client Context Object Model ?

In order to use ClientContext I need to add reference to two dll’s to my project i.e. Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client .Runtime.dll. In this Post we will talk abou8t 1) how to Get ListItemCollection from SharePoint document list using CAML 2) Upload a document to SharePoint Document list . Get ListItemCollection from SharePoint Document list using CAML: CodeProject. I can get the ListItemCollection as displayed in code snippets below

 ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef", documentName, "Text", 1); 

The GetListItemCollectionFromSP returns the List item and the parameters to method are
name – Name of the FieldRef, value = value to match for that FieldRef, type – Type of the value and rowLimit – Maximum number of rows to fetch

private static ListItemCollection GetListItemCollectionFromSP(string name, string value, string type, int rowLimit)
{
//Replace siteURL and documentListName with your SharePoint site URL and DocumentList
ListItemCollection listItems = null; string siteURL = "site URL";string documentListName = "document list";
using (ClientContext clientContext = new ClientContext(siteURL))
{
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
CamlQuery camlQuery = new CamlQuery(); ;
camlQuery.ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='" + name + @"'/>
<Value Type='" + type + "'>" + value + @"</Value>
</Eq>
</Where>                    
<RowLimit>" + rowLimit.ToString() + @"</RowLimit>
</Query>
</View>";
listItems = documentsList.GetItems(camlQuery);
clientContext.Load(documentsList);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
}
 return listItems;
}
Upload a document to SharePoint Document List:

In this case I want to upload a document to SharePoint document list and also update the field metadata i.e. for field “DocType” to “Favourites” as in this example using ClientContext. The code snippet is displayed below

public void UploadDocument(string siteURL, string documentListName,
string documentListURL, string documentName, byte[] documentStream)
{
using (ClientContext clientContext = new ClientContext(siteURL))
{
//Get Document List
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
var fileCreationInformation = new FileCreationInformation();
//Assign to content byte[] i.e. documentStream
fileCreationInformation.Content = documentStream;
//Allow owerwrite of document
fileCreationInformation.Overwrite = true;
//Upload URL
fileCreationInformation.Url = siteURL + documentListURL + documentName;
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
//Update the metadata for a field having name “DocType”
uploadFile.ListItemAllFields[“DocType”] = “Favourites”;
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}
}

Atul Verma

I am working at Microsoft as a .NET Consultant. I have an experience of 5 years in .NET technologies and application packaging. My expertise include .NET Framework(4.0, 3.5, 3.0, 2.0), WPF, WCF, ASP.net, AJAX, Web Services, Enterprise Library, Enterprise Applications, SharePoint 2010, Web Services, SQL Server (2008, 2005), Open Xml, MS Word Automation.