Sharepoint 2007 provides rich set of API’s that allows developers to develop custom components in Sharepoint to meet custom requirements.
SharePoint API’s can be classified into two types.
Sharepoint Object Model
Sharepoint Web Services
Sharepoint Object Model enables developers to customize almost anything that is in Sharepoint right from creating a Site Collection to connecting to external data sources. This object model is enclosed in 10 dll’s with 30 namespaces. A series of articles on consuming Sharepoint object model can be found here and here.
In on of my previous posts, I wrote down the steps to access SharePoint list data using SharePoint object model and CAML queries.
Now, lets see how to consume the Sharepoint Web Services in order to retrieve the Sharepoint List Data.
Step By Step
Create a new windows form project.
Add Web Reference to Sharepoint Lists Web Service
Right-click ‘Service Reference’ and select ‘Add Service Reference’.
In the Add Service Reference dialog box, click Advanced button.
In the ‘Service Reference settings’ dialog box, click Add Web Reference.
In the Url dropdown box, enter the url of your sharepoint site’s web service. If you are developing in the same system as the system in which MOSS is installed, then you can find the web services by clicking the link ‘Web Services in this Solution’ available in the Add Web Reference dialog box.
Note : The _vti_bin directory present under the sharepoint website directory lists down all the web services available.
Scroll through the list of Web Services. Click in the ‘Lists’ web service. Check the url in the url column and ensure that this Lists web service corresponds to your sharepoint site.
In the Web Reference name text box, give a name to the Lists web reference. Lets name it ListsWS.
Click Add Reference.
Place a button in the form. Name it btnGetListItems and the Caption can be Get List Items. Double click the button and generate the event handler.
The Lists web service contains an class called Lists. Lets create an object for this Lists class.
ListsWS.Lists list = new ListsWS.Lists();
The List object contains a method called GetListItems(). We need to invoke this method with appropriate parameters to get the items from the list
The first parameter to this method is the name of the list.
The remaining parameters are there to filter the list based on user defined criteria’s. Without any criteria, to fetch all the items from the list, the method should look like the following.
list.GetListItems(“MyListName”, “”, null, null, “”, null,null);
The signature of GetListItems in MSDN
The return type of the GetListItems() is an Xml Node.
The XmlNode’s inner xml looks like the following.
We need to process the xml node in order to access the list values.
Below, I have processed the xml to obtain the value of the column ‘Title’ that is present in the list and displayed it in a label.
The entire code is given below
ListWS.Lists lists = new ListWS.Lists();
lists.UseDefaultCredentials = true;
XmlNode nodes = lists.GetListItems(“MyList1″, “”, null, null, “”, null, null);foreach (XmlNode node in nodes)
if (node.Name == “rs:data”)
for (int i = 0; i < node.ChildNodes.Count; i++)
if (node.ChildNodes[i].Name == “z:row”)
lblResults.Text = node.ChildNodes[i].Attributes["ows_Title"].Value;