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"];
}

 

About wesmacdonald

Wes MacDonald is a Visual Studio ALM MVP, PSD, MCT and a Principal Consultant for LIKE 10 INC., a SharePoint Server, Visual Studio and Team Foundation Server Consulting Firm providing premium support and guidance services.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: