2013-05-08 41 views
6

Tôi đang sử dụng phương thức mô hình đối tượng phía khách hàng C# để truy xuất tất cả các mục danh sách từ thư viện tài liệu chứa thư mục con. Tôi đã kiểm tra các tài liệu MSDN và tôi bị mắc kẹt là tại sao tôi không thể có được tài sản trường, hoặc nếu tôi thậm chí làm điều này đúng.Truy xuất tất cả tài liệu từ tất cả thư mục con trong thư viện tài liệu - CSOM

NetworkCredential credentials = System.Net.CredentialCache.DefaultNetworkCredentials; 
      ClientContext clientcontext = new ClientContext(Resources.defaultSPSite); 
      clientcontext.Credentials = credentials; 

      //Load Libraries from SharePoint 
      //Web site = clientcontext.Web; 
      clientcontext.Load(clientcontext.Web.Lists); 
      clientcontext.ExecuteQuery(); 


      //List sharedDocumentsList = clientcontext.Web.Lists.GetByTitle("TestLDOCS"); 
      //CamlQuery camlQuery = new CamlQuery(); 
      //camlQuery.ViewXml = @"<View Scope='Recursive'><Query></Query></View>"; 

      foreach (List list in clientcontext.Web.Lists) 
      { 
       clientcontext.Load(list); 
       clientcontext.ExecuteQuery(); 
       //list.TemplateFeatureId.ToString().Equals("") && 
        string baseType = list.BaseType.ToString(); 
        string listTitle = list.Title.ToString(); 
        if (list.BaseType.ToString().Equals("DocumentLibrary", StringComparison.InvariantCultureIgnoreCase) && list.Title.ToString().Equals("TestLDOCS", StringComparison.InvariantCultureIgnoreCase)) 
        { 
         foreach (Folder subFolder in list.RootFolder.Folders) 
         { 
          foreach (File f in subFolder.Files) 
          { 
           Console.WriteLine((string) f.Title);      
          } 
         } 
        } 
      } 
     } 

Các lỗi mà tôi đang nhận được là "foreach (File f trong subFolder.Files)" bộ sưu tập có thể không được khởi tạo lỗi. Có anyway để có được các giá trị trường của tất cả các tài liệu trong mỗi thư mục con trong một thư viện tài liệu bằng cách sử dụng CSOM?

Tôi biết bạn có thể mạnh mẽ nhập giá trị trường cũng như với mục danh sách tức là (listItem ["fieldName"]). Tôi có nên đi tuyến đường này không?

Trả lời

7

Một số kiến ​​nghị:

1) thích ClientRuntimeContext.LoadQuery method để tải một danh sách cụ thể, ví dụ:

var lists = ctx.LoadQuery(ctx.Web.Lists.Where(l => l.BaseType == BaseType.DocumentLibrary)); 
ctx.ExecuteQuery(); 

2) Kể từ SharePoint SCOM hỗ trợ Request Batching nó được khuyến khích để giảm thiểu số các yêu cầu tới máy chủ. Ví dụ sau đây cho thấy làm thế nào để thực hiện một đơn yêu cầu đến máy chủ để tải tất cả các file từ thư viện tài liệu:

foreach (var list in lists) 
{ 
    var items = list.GetItems(CreateAllFilesQuery()); 
    ctx.Load(items, icol => icol.Include(i => i.File)); 
    results[list.Title] = items.Select(i=>i.File); 
} 
ctx.ExecuteQuery(); 

3) thích để tải tất cả các file thông qua truy vấn CAML như chứng minh dưới đây:

public static CamlQuery CreateAllFilesQuery() 
{ 
    var qry = new CamlQuery(); 
    qry.ViewXml ="<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Integer\">0</Value></Eq></Where></Query></View>"; 
    return qry; 
} 

sau đó, ví dụ sau sẽ trả lại tất cả các tập tin trong thư viện:

var items = list.GetItems(CreateAllFilesQuery()); 
ctx.Load(items, icol => icol.Include(i => i.File)); 
ctx.ExecuteQuery(); 
var files = items.Select(i=>i.File).ToList(); 

Nó được tối ưu hóa hơn cách lo ading danh sách cụ thể từ các góc độ hiệu suất

Hoàn dụ

Làm thế nào để tải tất cả các file từ thư viện tài liệu sử dụng SharePoint CSOM:

using (var ctx = new ClientContext(webUri)) 
{ 

    var results = new Dictionary<string, IEnumerable<File>>(); 
    var lists = ctx.LoadQuery(ctx.Web.Lists.Where(l => l.BaseType == BaseType.DocumentLibrary)); 
    ctx.ExecuteQuery(); 
    foreach (var list in lists) 
    { 
     var items = list.GetItems(CreateAllFilesQuery()); 
     ctx.Load(items, icol => icol.Include(i => i.File)); 
     results[list.Title] = items.Select(i=>i.File); 
    } 
    ctx.ExecuteQuery(); 

    //Print results 
    foreach (var result in results) 
    { 
     Console.WriteLine("List: {0}",result.Key); 
     foreach (var file in result.Value) 
     { 
      Console.WriteLine("File: {0}", file.Name); 
     } 
     }   
} 
+0

Cách tiếp cận của bạn có vẻ thực sự tốt, nhưng tôi nhận được NotSupportedException trên 'items.Select (i => i.File) ' – Santhos

1
foreach (List list in clientcontext.Web.Lists) 
     { 
      clientcontext.Load(list); 
      clientcontext.ExecuteQuery(); 
      //list.TemplateFeatureId.ToString().Equals("") && 
       string baseType = list.BaseType.ToString(); 
       string listTitle = list.Title.ToString(); 
       if (list.BaseType.ToString().Equals("DocumentLibrary", StringComparison.InvariantCultureIgnoreCase) && list.Title.ToString().Equals("TestLDOCS", StringComparison.InvariantCultureIgnoreCase)) 
       { 
        foreach (Folder subFolder in list.RootFolder.Folders) 
        { 
      clientcontext.Load(subFolder.Files); 
        clientcontext.ExecuteQuery(); 
         foreach (File f in subFolder.Files) 
         { 
          Console.WriteLine((string) f.Title);      
         } 
        } 
       } 
     } 
    } 
+0

Bạn đang thiếu tải truy vấn bổ sung cho "list.RootFolder" và sau đó "list.RootFolder.Folders". Nếu bạn không thêm những điều này, điều này không hoạt động (cung cấp cho lỗi khởi tạo.Nhưng +1 cho tôi gần hơn. :) –

Các vấn đề liên quan