SharePoint Tips

Thursday, November 16, 2017

CSOM Method to return list data from SharePoint - C#

        /// <summary>
        /// CSOM method to return list data from SharePoint
        /// </summary>
        /// <param name="clientContext"></param>
        /// <returns></returns>
        public ListItemCollection LoadListData(ClientContext clientContext)
        {
            Web site = clientContext.Web;
            List oList = clientContext.Web.Lists.GetByTitle("ListName");
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = @"<View Scope='Recursive'>
                                            <Query></Query>
                                        </View>";
            ListItemCollection collListItem = oList.GetItems(camlQuery);

            clientContext.Load(collListItem, listItems => listItems.Include
                                           (item => item["Column1"],
                                            item => item["Column2"]));

            clientContext.ExecuteQuery();
            return collListItem;
        }

No comments:

Post a Comment