Load SharePoint 2007 List into DataTable


 

I have been playing around with the Web Services provided by SharePoint and thought this code example might help someone else as it took me a bit to track down.

private ListService.Lists listService = new ListService.Lists();
private void LoadList(string listName) {

   XmlDocument doc = new XmlDocument();
   doc.LoadXml("<Document><Query/><ViewFields/><QueryOptions/></Document>");
   XmlNode listQuery = doc.SelectSingleNode("//Query");
   XmlNode listViewFields = doc.SelectSingleNode("//ViewFields");
   XmlNode listQueryOptions = doc.SelectSingleNode("//QueryOptions");
   XmlNode node;

   listService.UseDefaultCredentials = true;
   listService.Url = "http://localhost/_vti_bin/Lists.asmx";
   node = listService.GetListItems(listName, string.Empty, listQuery, listViewFields, "0", listQueryOptions, string.Empty);
   XmlTextReader reader = new XmlTextReader(node.OuterXml, XmlNodeType.Element, null);
   DataSet ds = new DataSet();
   ds.ReadXml(reader);
   DataTable table = ds.Tables["row"];
}

 

Wes MacDonald's avatar

About Wes MacDonald

Wes MacDonald is a DevOps Consultant for LIKE 10 INC., a DevOps consulting firm providing premium support, guidance and services for Azure, Microsoft 365 and Azure DevOps.

No comments yet... Be the first to leave a reply!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.