How to: Return List Items

This programming task shows how to create a Windows Form that uses the GetListItems method of the Lists Web service to return items from a list.

Procedures

Before you begin, create a Windows Application as described in Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio. For information about setting a Web reference to a Windows SharePoint Services Web service, see Web Service Guidelines.

To add code to display the collection of list items

  1. After creating a Windows Application project and adding a Web reference, open Form1 in Design view, open the Toolbox, and then drag a Label control and a Button control onto the form.

  2. Right-click the Label1 control, click Properties, and then delete the value Label1 from the Text property of the control.

  3. Double-click the button to display the Code Editor, and add the following lines of code to the Click event handler:

    Visual Basic
    'Declare and initialize a variable for the Lists Web service.
    Dim listService As New Web_Reference.Lists()
    
    'Authenticate the current user by passing their default 
    'credentials to the Web service from the system credential cache.
    listService.Credentials = System.Net.CredentialCache.DefaultCredentials
    
    'Set the Url property of the service for the path to a subsite.
    listService.Url = "http://Server_Name/Subsite_Name/_vti_bin/Lists.asmx"
    
    'Instantiate an XmlDocument object.
    Dim xmlDoc As New System.Xml.XmlDocument()
    
    'Assign values to the string parameters of the GetListItems method, 'using GUIDs for the listName and viewName variables. For listName, 'using the list display name will also work, but using the list GUID 
    'is recommended. For viewName, only the view GUID can be used. 
    'Using an empty string for viewName causes the default view 
    'to be used.
    Dim listName As String = "{17991794-81BB-494F-9910-CFBF1093A7CF}"
    Dim viewName As String = "{7137FFF8-48FF-4C69-8C76-0E3BBD1EA7F9}"
    Dim rowLimit As String = "150"
    
    'Use the CreateElement method of the document object to create 'elements for the parameters that use XML.
    Dim query As System.Xml.XmlElement = xmlDoc.CreateElement("Query")
    Dim viewFields As System.Xml.XmlElement = xmlDoc.CreateElement("ViewFields")
    Dim queryOptions As System.Xml.XmlElement = xmlDoc.CreateElement("QueryOptions")
    
    'To specify values for the parameter elements (optional), assign CAML fragments to the InnerXml property of each element.
    query.InnerXml = "<Where><Gt><FieldRef Name=""ID"" />" + "<Value Type=""Counter"">3</Value></Gt></Where>"
    viewFields.InnerXml = "<FieldRef Name=""Title"" />"
    queryOptions.InnerXml = ""
    
    'Declare an XmlNode object and initialize it with the XML response 'from the GetListItems method. The last parameter specifies 
    'the GUID of the Web site containing the list. Setting it to 
    'Nothing causes the Web site specified by the Url property 
    'to be used.
    Dim nodeListItems As System.Xml.XmlNode = listService.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, Nothing)
    
    'Loop through each node in the XML response and display each item.
    Dim listItem As System.Xml.XmlNode
    For Each listItem In  nodeListItems
       label1.Text += listItem.OuterXml
    Next listItem

    C#
    /*Declare and initialize a variable for the Lists Web service.*/
    Web_Reference.Lists listService = new Web_Reference.Lists();
    
    /*Authenticate the current user by passing their default 
    credentials to the Web service from the system credential cache.*/
    listService.Credentials = 
        System.Net.CredentialCache.DefaultCredentials;
    
    /*Set the Url property of the service for the path to a subsite.*/
    listService.Url = 
        "http://Server_Name/Subsite_Name/_vti_bin/Lists.asmx";
    
    // Instantiate an XmlDocument object
    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
    
    /* Assign values to the string parameters of the GetListItems method, using GUIDs for the listName and viewName variables. For listName, using the list display name will also work, but using the list GUID is recommended. For viewName, only the view GUID can be used. Using an empty string for viewName causes the default view 
    to be used.*/
    string listName = "{17991794-81BB-494F-9910-CFBF1093A7CF}";
    string viewName = "{7137FFF8-48FF-4C69-8C76-0E3BBD1EA7F9}";
    string rowLimit = "150";
    
    /*Use the CreateElement method of the document object to create elements for the parameters that use XML.*/
    System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
    System.Xml.XmlElement viewFields = 
        xmlDoc.CreateElement("ViewFields");
    System.Xml.XmlElement queryOptions = 
        xmlDoc.CreateElement("QueryOptions");
    
    /*To specify values for the parameter elements (optional), assign CAML fragments to the InnerXml property of each element.*/
    query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" +
        "<Value Type=\"Counter\">3</Value></Gt></Where>";
    viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
    queryOptions.InnerXml = "";
    
    /* Declare an XmlNode object and initialize it with the XML response from the GetListItems method. The last parameter specifies the GUID of the Web site containing the list. Setting it to null causes the Web site specified by the Url property to be used.*/
    System.Xml.XmlNode nodeListItems = 
        listService.GetListItems
        (listName, viewName,query,viewFields,rowLimit,queryOptions,null);
    
    /*Loop through each node in the XML response and display each item.*/
    foreach (System.Xml.XmlNode listItem in nodeListItems)
    {
        label1.Text += listItem.OuterXml;
    }
    Note Note:

    You must provide your own values for the listName, viewName, and rowLimit variables that are used in the example.

  4. On the Debug menu, click Start to test the form. Click Button1 to display the list items from the specified list.

Tags :


Community Content

Michael K. Kim
Parameter signature should contain webID for successful build

Assigning a value for webID parameter is optional but the parameter itself should exist for successful build either as italicized in the following or NOTHING should be assigned:

 

'Optional whether to assign a value or not
 Dim webID As String = "{216bcc39-bb08-4037-846a-ca25f7e5be30}"

...

Dim nodeListItems As System.Xml.XmlNode = listService.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, webID )

OR

Dim nodeListItems As System.Xml.XmlNode = listService.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, NOTHING )

Tags :

Lawrence Liu - MSFT
Retrieving SharePoint List Items' Attachment URLs using Lists Web Service
http://blogs.msdn.com/sharepoint/archive/2007/03/25/retrieving-sharepoint-list-items-attachment-urls-using-lists-web-service.aspx
Tags :

John Morales
FieldRef parameters with spaces need _x0020_

When you wish to query or display a field with a space in the name use _x0020_ instead of the space.

<FieldRef Name="First_x0020_Name"/>

Tags :

Mak2008
sometimes do not show in the xml results
Can you please redirect some information about <ViewFields>?

I am only interest why some fields will not show even when I explicity state the fields i want to display.

For example: there is a column called package (package is an internal name) on the list. the package is optional and it is not mandatory field. It is a drop down list. when i use GetListItems to retrieve the data wtih specific records (using Query and Eq element), the result was in xml data. Inside the xml data, I noticed that Package column data is not include! Do you have any ideas how to force Package column to return empty column?
Tags :

Page view tracker