C#從Sharepoint上獲取List數據:
添加引用:
using Microsoft.SharePoint.Client; using System.Data;
public static void getDataFromSPList(string siteURL, string listName) { ClientContext clientContext = new ClientContext(siteURL); Web web = clientContext.Web; ListCollection collList = web.Lists; clientContext.Load(collList); clientContext.ExecuteQuery(); //Sharepoint的遠程交互只有在ExecuteQuery之後才被執行的。 foreach (Microsoft.SharePoint.Client.List list in collList) { Console.WriteLine("Title:{0}",list.Title); } //CamlQuery CamlQuery camlQuery = new CamlQuery(); camlQuery.ViewXml = ""; Microsoft.SharePoint.Client.List planlist = collList.GetByTitle(listName); ListItemCollection collListItem = planlist.GetItems(camlQuery); clientContext.Load(collListItem, items=>items.Include( item=>item.Id, item=>item.DisplayName, item=>item.HasUniqueRoleAssignments) ); clientContext.ExecuteQuery(); foreach (ListItem olistItem in collListItem) { Console.WriteLine("ID: {0} \nDisplay name: {1} \nUnique role assignments: {2}", olistItem.Id, olistItem.DisplayName, olistItem.HasUniqueRoleAssignments); } clientContext.Load(planlist); clientContext.ExecuteQuery(); Console.WriteLine(planlist.Title.ToString()); Console.WriteLine(planlist.ItemCount.ToString()); Console.ReadKey(); }