Click here to Skip to main content
13,091,554 members (72,740 online)
Click here to Skip to main content
Add your own
alternative version

Stats

112.1K views
19 bookmarked
Posted 20 Aug 2010

How to Upload/Download a Document in SharePoint 2010 using Client Context Object Model

, 28 Aug 2010
Rate this:
Please Sign up or sign in to vote.
How to upload/download a document in SharePoint 2010 using client context object model

In order to use ClientContext, I need to add reference to two DLLs to my project, i.e., Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll.

In this blog, I’ll display how to:

  • Get ListItemCollection from SharePoint document list using CAML
  • Upload a document to SharePoint Document list
  • Download a document from SharePoint Document list

Get ListItemCollection from SharePoint Document list using CAML

I can get the ListItemCollection as displayed in the code snippets below:

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

The GetListItemCollectionFromSP returns the List item and the parameters to method are:

  • nameName of the FieldRef
  • value = value to match for that FieldRef
  • typeType of the value and
  • rowLimit – Maximum number of rows to fetch
private static ListItemCollection GetListItemCollectionFromSP(string name, 
   string value, string type, int rowLimit)
{
//Update siteURL and DocumentListName with as per your site
string siteURL = "URL of the Site";
string documentListName = "DocumentList";
ListItemCollection listItems = null;
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 overwrite 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();
}
}

Download a Document from SharePoint Document List

I can download the document using the code snippets displayed below:

public Stream DownloadDocument(string siteURL, string documentName)
{
ListItem item = GetDocumentFromSP(documentName);
if (item != null)
{
using (ClientContext clientContext = new ClientContext(siteURL))
{
FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext,
    item ["FileRef"].ToString());            

return fInfo.Stream;
}
}
return null;
}

private static ListItem GetDocumentFromSP(string documentName)
{
//This method is discussed above i.e. Get List Item Collection from SharePoint
//Document List
ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef", 
    documentName, "Text", 1);
 
return (listItems != null && listItems.Count == 1) ? listItems[0] : null;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

atverma
Software Developer (Senior)
India India
Atul works at Microsoft as a .NET consultant. As a consultant his job is to design, develop and deploy enterprise level secure and scalable solutions using Microsoft Technologies.

His technical expertise include .NET Framework(4.0, 3.5, 3.0), WPF, WCF, SharePoint 2010, ASP.net, AJAX, Web Services, Enterprise Applications, SQL Server 2008, Open Xml, MS Word Automation.

Follow him on twitter @verma_atul

He blogs at
http://www.atulverma.com
http://blogs.msdn.com/b/atverma

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionWhy no downloadable source code? Pin
Michael Kosak28-Sep-16 7:54
memberMichael Kosak28-Sep-16 7:54 
Questionus in VSTA Pin
christy000083-Feb-14 10:17
memberchristy000083-Feb-14 10:17 
QuestionVersioning Pin
Mendocina16-Dec-13 9:21
memberMendocina16-Dec-13 9:21 
QuestionFileInformation? Pin
zaitsman17-Mar-13 13:00
memberzaitsman17-Mar-13 13:00 
QuestionNeed some clearification Pin
Shubhranshu from Mumba1-Feb-13 1:20
memberShubhranshu from Mumba1-Feb-13 1:20 
GeneralMy vote of 3 Pin
maloneb27-Aug-10 12:28
membermaloneb27-Aug-10 12:28 
GeneralRe: My vote of 3 Pin
atverma28-Aug-10 18:26
memberatverma28-Aug-10 18:26 
GeneralProblems with code Pin
maloneb27-Aug-10 12:27
membermaloneb27-Aug-10 12:27 
First, let me say that this article is very helpful - it's getting me jumpstarted with the Client Object Model -

However, I found a few problems with the code:

In the DownloadDocument method, ClientOM is not a valid reference. As far as I can see, it should be Microsoft.SharePoint.Client.

In GetListItemCollectionsFromSP method, there are two variables that are not defined: siteURL and documentListName.

Thanks for the post - hope to see this refined into ready to use code. Would also be helpful if you include project files for use as example.
GeneralRe: Problems with code Pin
uskumar3328-Aug-10 1:48
memberuskumar3328-Aug-10 1:48 
GeneralRe: Problems with code Pin
atverma28-Aug-10 18:24
memberatverma28-Aug-10 18:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 29 Aug 2010
Article Copyright 2010 by atverma
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid