SharePoint Client Object Modelの小さな例

11232 ワード

筆者は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 caml Query-file download butの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 includ File.Server RelativeUrl
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 Sharpoint Dcument 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 Cient with Cient Object Model(OM)
http://www.codeproject.com/Articles/60294/SharePoint-2010-Managed-net-Client-with-Client-Obj
Uploading files using Cient 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>