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

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>>

posted on   中道学友  阅读(2230)  评论(0编辑  收藏  举报

编辑推荐:
· 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的在线工具

导航

< 2013年3月 >
24 25 26 27 28 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6

技术追求准确,态度积极向上

点击右上角即可分享
微信分享提示