Wednesday, June 3, 2009

Part 2: Get all the Lists Items of a List using Lists.asmx webservice

This series is in continuation of demonstrating how to access SharePoint data “behind the scenes” without having to run the program on the SharePoint server. In Part 1 of this series, I have demonstrated how to access the SharePoint service using a web reference as well as how to access all the lists of a particular site. In this session, I will demonstrate how to get all items of a particular sharepoint list.

Step 1: First you need to create a Visual Studio Project. You can create a Webpart or Web Control or Console Application as per your requirement.

Step 2: Add the following code at the beginning:
using System.Xml;
using System.Data;
using System.Net;
using Microsoft.SharePoint;
/* Optional */

Step 3: Add a Web reference with the following URL:
http://[your site url]/_vti_bin/Lists.asmx and name it as "WS" (or anything else, you just need to update that accordingly in the code block shown below).

Step 4: Add the following code:

WS.Lists listService = new WS.Lists();

/* This line is optional for root level site. If the site is a sub-site or under /sites/, then you need to write this, otherwise the code will always try to find a list in the root. */
listService.Url = "http://[your site url]/_vti_bin/Lists.asmx";

listService.Credentials = System.Net.CredentialCache.DefaultCredentials;

XmlDocument xmlDoc = new System.Xml.XmlDocument();

XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");

/* The commented statements below is optional. If applicable if you want to pass any queries, or need some specific columns to return, or need to pass any query options. */
/*ndQuery.InnerXml = "<where><and><gt><fieldref name='Field1'>" +
"<value type='Number'>5000</value></gt><gt><fieldref name='Field2'>" +
"<value type='DateTime'>2009-05-01T00:00:00</value></gt></and></where>";
ndViewFields.InnerXml = "<fieldref name='Field1'><fieldref name='Field2'>";
ndQueryOptions.InnerXml =
"<includemandatorycolumns>FALSE</includemandatorycolumns>" +

/* "My List" below is the name of the sharepoint list. */
XmlNode ndListItems = listService.GetListItems("My List", null, ndQuery, ndViewFields, null, ndQueryOptions, null);

/* Here I have used DataSet and Datatable, but you can also parse the returned XML directly */
XmlReader reader = new XmlNodeReader(ndListItems);
DataSet ds = new DataSet();

/* You can find two tables, one is "data" and the other one is "row". We will get all the items in the later one */

foreach (DataRow dr in ds.Tables[1].Rows)
/* You can replace Console.WriteLine accordingly if you are not using Console Application */

The code above will return all the lists item's Title column value of the particular List. If you need other column's value, you can use the corresponding column name. You can find out the other column names using the following code:

foreach (DataColumn dc in ds.Tables[1].Columns)