using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
namespace ClientOMTest
{
class Program
{
static void Main(string[] args)
{
//Basic setup and test
using (ClientContext clientContext = new ClientContext("http://app1-sps2013:10080/sites/sc18/"))
{
try
{
//Get Web
Web oWebsite = clientContext.Web;
clientContext.Load(oWebsite);
clientContext.ExecuteQuery();
//Get List
List oDocLib = clientContext.Web.Lists.GetByTitle("Shared Documents");
clientContext.Load(oDocLib);
//Get ListItems
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<Query><Where><Neq><FieldRef Name='ID' /><Value Type='Counter'>null</Value></Neq></Where></Query>";
ListItemCollection listItems = oDocLib.GetItems(camlQuery);
clientContext.Load(listItems, items => items.Include(
item => item["FileRef"],
item => item["FileDirRef"],
item => item["FileLeafRef"],
item => item["File_x0020_Type"]
)
);
clientContext.ExecuteQuery();
//Get all Word document
foreach (ListItem li in listItems)
{
//Get folder of the file
string fileDirRef = li["FileDirRef"].ToString().ToLower();
string fileLeafRef = li["FileLeafRef"].ToString().ToLower();
string relativeURL = li["FileRef"].ToString().ToLower();
string fileType = li["File_x0020_Type"].ToString().ToLower();
string fullItemURL = oWebsite.Url.Substring(0, oWebsite.Url.IndexOf(oWebsite.ServerRelativeUrl)) + relativeURL;
//Get file name and folder url
switch (fileType)
{
case "docx":
ProcessDocx(relativeURL, fileLeafRef, clientContext);
break;
case "pptx":
ProcessPptx(relativeURL, clientContext);
break;
case "xlsx":
ProcessXlsx(relativeURL, clientContext);
break;
default:
break; ; //Won't change anyother file types.
}
}
}//try
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}//cache
}//using
}//main
public static void ProcessDocx(string relativeURL, string fileName, ClientContext ctx)
{
//Download the file
FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, relativeURL);
byte[] buffer = new byte[16 * 1024];
using (FileStream fs = new FileStream(@"c:\temp\" + fileName, FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
int read;
while ((read = ffl.Stream.Read(buffer, 0, buffer.Length)) > 0)
{
fs.Write(buffer, 0, read);
}
}
}
public static void ProcessPptx(string fullItemURL, ClientContext ctx)
{
}
public static void ProcessXlsx(string fullItemURL, ClientContext ctx)
{
}
}
}
参考资料
=======================
Sharepoint 2010 client object model with camlQuery - file download but no content / 0 byte
http://stackoverflow.com/questions/10024524/sharepoint-2010-client-object-model-with-camlquery-file-download-but-no-conten
Sharepoint Client Object Model: Load items from list with included File.ServerRelativeUrl
http://stackoverflow.com/questions/9059634/sharepoint-client-object-model-load-items-from-list-with-included-file-serverre
How do I return a document from a Sharepoint Document library to the user?
http://stackoverflow.com/questions/5709710/how-do-i-return-a-document-from-a-sharepoint-document-library-to-the-user
SharePoint 2010: Managed .net Client with Client Object Model (OM)
http://www.codeproject.com/Articles/60294/SharePoint-2010-Managed-net-Client-with-Client-Obj
Uploading files using Client Object Model in SharePoint 2010
http://blogs.msdn.com/b/sridhara/archive/2010/03/12/uploading-files-using-client-object-model-in-sharepoint-2010.aspx
<<Professional SharePoint 2010 Development>>