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:
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)
{
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))
{
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
var fileCreationInformation = new FileCreationInformation();
fileCreationInformation.Content = documentStream;
fileCreationInformation.Overwrite = true;
fileCreationInformation.Url = siteURL + documentListURL + documentName;
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
fileCreationInformation);
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)
{
ListItemCollection listItems = GetListItemCollectionFromSP("FileLeafRef",
documentName, "Text", 1);
return (listItems != null && listItems.Count == 1) ? listItems[0] : null;
}
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