笔者想要写一段代码, 该代码在客户端运行, 用于测试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>>

相关文章: