SharePoint Client Object Model的一个小例子
笔者想要写一段代码, 该代码在客户端运行, 用于测试SharePoint服务器端的性能.
客户端是无法访问到Server Side Object Model的, 所以除了调用Web Service, 我们还可以考虑使用SharePoint 2010中引入的Client Side Object Model.
需要的功能如下, 得到某个文档库下的所有的文件, 对Word, PPT, Excel文件分别使用调用不同的函数.
在这个例子的操作里, 我仅写了对Word文档的下载.
写这段代码的时候, 参考了很多人的文章, 在这里向他们的分享表示感谢.
我也把我的代码分享给大家, 希望能够帮助更多的人.
本例子在SharePoint 2013环境中测试通过.
===============================================
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
Sharepoint Client Object Model: Load items from list with included File.ServerRelativeUrl
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
<<Professional SharePoint 2010 Development>>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2012-03-14 修改SPS2010的Search Core Results webpart, 令其显示文档被索引了的所有属性
2011-03-14 "Cannot generate SSPI context"
2011-03-14 FAST for SharePoint如何重置Index
2010-03-14 Share一个整理HTML格式的在线工具
2010-03-14 WebDAV携带认证信息的问题
2010-03-14 Windows Server 2008上的SharePoint Explorer View的问题
2010-03-14 Share一个解码HTML Entity的在线工具