Dotnetdreamer's Weblog

SharePoint, Silverlight and Azure

Archive for June, 2009

MOSS Web Services – Accessing Sharepoint List data

Posted by Ramprasad Navaneethakrishnan on June 4, 2009

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.

  1. Sharepoint Object Model
  2. 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

  1. Create a new windows form project.
  2. Add Web Reference to Sharepoint Lists Web Service
    1. Right-click ‘Service Reference’ and select ‘Add Service Reference’.
    2. In the Add Service Reference dialog box, click Advanced button.
    3. In the ‘Service Reference settings’ dialog box, click Add Web Reference.
    4. 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.
      1. Note : The _vti_bin directory present under the sharepoint website directory lists down all the web services available.
    5. 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.
    6. Lists Web Service
    7. In the Web Reference name text box, give a name to the Lists web reference. Lets name it ListsWS.
    8. Click Add Reference.
    9. AddWebReference
  3. 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.
  4. The Lists web service contains an class called Lists. Lets create an object for this Lists class.
    1. ListsWS.Lists list = new ListsWS.Lists();
  5. The List object contains a method called GetListItems(). We need to invoke this method with appropriate parameters to get the items from the list
    1. The first parameter to this method is the name of the list.
    2. 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.
      1. list.GetListItems(“MyListName”, “”, null, null, “”, null,null);
      2. The signature of GetListItems in MSDN
  6. The return type of the GetListItems() is an Xml Node.
  7. The XmlNode’s inner xml looks like the following.
  8. We need to process the xml node in order to access the list values.
    1. 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.
  9. 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;

Hope this helps.



Posted in MOSS 2007 | Tagged: , , , , , , , , , | 22 Comments »