.Net Console Application读取Sharepoint Document Library方法
显示列表:
ListItemCollection listItemCollection = GetListItemCollection(); if (listItemCollection != null) { if (listItemCollection.Count > 0) { } }
得到列表:得到全部子文件夹及文件
1 public static ListItemCollection GetListItemCollection() 2 { 3 ListItemCollection ListItemCollection = null; 4 5 try 6 { 7 ClientContext clientConext = GetClientContext(); 8 if (clientConext != null) 9 { 10 CamlQuery camlQuery = new CamlQuery(); 11 camlQuery.ViewXml = "<View><Query></Query></View>"; 12 StringBuilder viewField = new StringBuilder(); 13 14 var filedArray = SPFieldNameList.Split(','); 15 if (filedArray.Count() > 0) 16 { 17 viewField.Append("<ViewFields>"); 18 foreach (var field in filedArray) 19 { 20 viewField.Append("<FieldRef Name='" + field + "'/>"); 21 } 22 23 viewField.Append("</ViewFields>"); 24 } 25 26 string viewFieldStr = viewField.ToString(); 27 camlQuery.ViewXml = "<View Scope='RecursiveAll'><Query></Query>" + viewFieldStr + "</View>"; 28 ListItemCollection = clientConext.Site.RootWeb.GetListByTitle(SPListName).GetItems(camlQuery); 29 clientConext.Load(ListItemCollection); 30 clientConext.ExecuteQuery(); 31 } 32 33 Console.WriteLine("Load Sharepoint List Data Success [" + DateTime.Now + "]"); 34 } 35 catch (Exception ex) 36 { 37 Console.WriteLine("Get List Item Collection Error:" + ex.Message); 38 } 39 40 return ListItemCollection; 41 }
得到Client Conext:
1 public static ClientContext GetClientContext() 2 { 3 ClientContext resultClientContext = null; 4 OfficeDevPnP.Core.AuthenticationManager authManager = new OfficeDevPnP.Core.AuthenticationManager(); 5 try 6 { 7 using (var clientContext = authManager.GetNetworkCredentialAuthenticatedContext(SPSiteUrl, SPUserName, SPPassword, Domain)) 8 { 9 bool listExists = clientContext.Site.RootWeb.ListExists(SPListName); 10 if (listExists == true) 11 { 12 resultClientContext = clientContext; 13 } 14 else 15 { 16 Console.WriteLine("The list[" + SPListName + "] does not exist"); 17 } 18 } 19 } 20 catch (Exception ex) 21 { 22 Console.WriteLine("Get Client Context Error:" + ex.Message); 23 } 24 25 return resultClientContext; 26 }
其中CAML Scope为:
【原文出处】