Sunday, 25 October 2015

Get all list items using CSOM in SharePoint 2013


There are several ways to get all the list items in CSOM. In the following method all the list items are retrieved using CSOM.


 private void GetAllListItems(ClientContext context, string listName)
        {
            try
            {

                Web web = context.Web;
                ListCollection lists = web.Lists;

                List selectedList = lists.GetByTitle(listName);

                context.Load<ListCollection>(lists);
                context.Load<List>(selectedList);

                context.ExecuteQuery();

                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = @"<View><Query></Query></View>";

                Microsoft.SharePoint.Client.ListItemCollection listItems = selectedList.GetItems(camlQuery);

                if (listItems.Count > 0)
                {
                    context.Load<Microsoft.SharePoint.Client.ListItemCollection>(listItems);
                    context.ExecuteQuery();
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

Happy SharePointing...................:)

No comments:

Post a Comment