C#从Sharepoint上获取List数据

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();
        }


你可能感兴趣的:(C#)