16 out of 45 rated this helpful - Rate this topic

Lists.GetListItems Method

Windows SharePoint Services 3
Returns information about items in the list based on the specified query.

Web Service: ListsWeb Reference: http://<Site>/_vti_bin/Lists.asmx
[SoapDocumentMethodAttribute("http://schemas.microsoft.com/sharepoint/soap/GetListItems", RequestNamespace="http://schemas.microsoft.com/sharepoint/soap/", ResponseNamespace="http://schemas.microsoft.com/sharepoint/soap/", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Wrapped)] 
public XmlNode GetListItems (
	string listName,
	string viewName,
	XmlNode query,
	XmlNode viewFields,
	string rowLimit,
	XmlNode queryOptions,
	string webID
)

Parameters

listName

A string that contains either the display name or the GUID for the list. It is recommended that you use the GUID, which must be surrounded by curly braces ({}). When querying the UserInfo table, the string contains "UserInfo".

viewName

A string that contains the GUID for the view surrounded by curly braces ({}), which determines the view to use for the default view attributes represented by the query, viewFields, and rowLimit parameters. If this parameter contains an empty string, the default view is used. If the view GUID is supplied, the value of the query, viewFields, or rowLimit parameter overrides the equivalent setting within the view. For example, if the view specified by the viewFields parameter has a row limit of 100 rows but the rowLimit parameter contains 1000, then 1,000 rows are returned in the response.

query

A Query element containing the query that determines which records are returned and in what order, and that can be assigned to a System.Xml.XmlNode object, as in the following example.

<Query>
   <Where>
      <Lt>
         <FieldRef Name="ID" />
         <Value Type="Counter">3</Value>
      </Lt>
   </Where>
</Query>

If supplied, the value of this parameter overrides either the query within the view specified by the viewName parameter or the query within the default view for the list.

viewFields

A ViewFields element that specifies which fields to return in the query and in what order, and that can be assigned to a System.Xml.XmlNode object, as in the following example.

<ViewFields>
   <FieldRef Name="ID" />
   <FieldRef Name="Title" />
</ViewFields>

If supplied, the value of this parameter overrides the view fields within the view specified by the viewName parameter or the view fields in the default view for the list.

rowLimit

A string that specifies the number of items, or rows, to display on a page before paging begins. If supplied, the value of this parameter overrides the row limit set in the view specified by the viewName parameter or the row limit set in the default view for the list.

queryOptions

An XML fragment in the following form that contains separate nodes for the various properties of the SPQuery object, and that can be assigned to a System.Xml.XmlNode object.

<QueryOptions>
   <IncludeMandatoryColumns>FALSE
      </IncludeMandatoryColumns>
   <DateInUtc>TRUE</DateInUtc>
</QueryOptions>

The following table shows the elements that can be used in the Collaborative Application Markup Language (CAML) fragment passed through the queryOptions parameter.

Element

Description

DateInUtc

TRUE to return dates in Coordinated Universal Time (UTC) format. FALSE to return dates in ISO format. This element is optional, and its default value is TRUE.

Folder

Specifies a URL used to filter document library items for items in the specified folder. This element is optional, and its default value is an empty string.

Paging

A string that contains data needed to support paging, including a ListItemCollectionPositionNext attribute. If left empty, the list items returned start from the beginning of the list. Client applications should supply the value of a server-supplied bookmark. The server returns the next page of data based on the bookmark supplied. This element is optional, and its default value is an empty string.

IncludeMandatoryColumns

TRUE to specify that mandatory system columns be returned in addition to the fields specified by the viewFields parameter (for example, owsHiddenVersion, dependent fields, and required fields). This element is optional, and its default value is TRUE.

MeetingInstanceID

An integer value where a positive number represents a specific meeting instance. Negative numbers have the following meanings: -3 = UnSpecified, -2 = AllWithSeries, -1 = AllButSeries, 0 = Series. This element is optional, and its default value is -1. Negative values correspond to values of the Microsoft.SharePoint.Meetings.SPMeeting.SpecialInstance enumeration.

ViewAttributes

A string representing all attributes returned as part of the View element when retrieving a view through the GetView method. This element is optional, and its default value is empty. If a viewName parameter is supplied, the view attributes from the persisted view are used. When this argument is supplied, it overrides any view attributes that can be retrieved from the persisted view specified by the viewName parameter. This element is optional and its default value is an empty string. To return all documents in a library, the ViewAttributes element is used as follows: <ViewAttributes Scope="Recursive" />.

To pass an empty value for this parameter, include an empty QueryOptions element as follows.

<queryOptions xmlns:SOAPSDK9=
      "http://schemas.microsoft.com/sharepoint/soap/" >
   <QueryOptions/>
</queryOptions>

webID

Optional. A string containing the GUID of the parent Web site for the list surrounded by curly braces ({}). Setting this parameter to null means the Web site specified by the Url property of the service will be used, and if the Url property of the service is not specified, the root Web site will be used.

Return Value

An XML fragment in the following form that contains information about the list items and that can be assigned to a System.Xml.XmlNode object.
<listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" 
   xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" 
   xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" 
   xmlns="http://schemas.microsoft.com/sharepoint/soap/">
   <rs:data ItemCount="4">
      <z:row ows_Number_Field="6555.00000000000" 
         ows_Created="2003-06-18T03:41:09Z" 
         ows_ID="3" ows_owshiddenversion="3" />
      <z:row ows_Number_Field="78905456.0000000" 
         ows_Created="2003-06-18T17:15:58Z" 
         ows_ID="4" ows_owshiddenversion="2" />
         ...
   </rs:data>
</listitems>

The GetListItems method supports server-side paging. The XML data returned by this method includes a ListItemCollectionPositionNext attribute inside the rs:Data element that contains the information to support paging. This string contains data for the fields in the sort and for other items needed for paging. You should consider this string internal and not to be modified; modifying it can produce unexpected results. The following example shows the form of this return value when paging is supported.

<rs:Data ListItemCollectionPositionNext=" 
Paged=TRUE&p_ID=100&View=
      %7bC68F4A6A%2d9AFD%2d406C%2dB624%2d2CF8D729901E%7d&PageFirstRow=
      101" Count=1000 >
   <z:row ows_FirstName="Nancy" ows_LastName="Name" ….. />
   ...
</rs:Data>

To get the next page of data, the queryOption parameter is used, as shown in the following example.

<QueryOptions>
  <Paging ListItemCollectionPositionNext=" 
    Paged=TRUE&p_ID=100&View=
    %7bC68F4A6A%2d9AFD%2d406C%2dB624%2d2CF8D729901E%7d&PageFirstRow=
   101" />
</QueryOptions>

The following code example displays items where a Number field contains a value greater than 5,000 and a DateTime field contains a value greater than a specified date. The example uses an XmlDocument object to create XmlNode objects for parameters.

This example requires that a using (Visual C#) or Imports (Visual Basic) directive be included for the System.Xml namespace.

Web_Reference_Folder.Lists listService = new Web_Reference_Folder.Lists();
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","");

ndQueryOptions.InnerXml = 
    "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" + 
    "<DateInUtc>TRUE</DateInUtc>";
ndViewFields.InnerXml = "<FieldRef Name='Field1' />
    <FieldRef Name='Field2'/>";
ndQuery.InnerXml = "<Where><And><Gt><FieldRef Name='Field1'/>" + 
    "<Value Type='Number'>5000</Value></Gt><Gt><FieldRef Name='Field2'/>" + 
    "<Value Type=
        'DateTime'>2003-07-03T00:00:00</Value></Gt></And></Where>";
try
{
    XmlNode ndListItems = 
        listService.GetListItems("List_Name", null, ndQuery, 
        ndViewFields, null, ndQueryOptions, null);
    MessageBox.Show(ndListItems.OuterXml);
}

catch (System.Web.Services.Protocols.SoapException ex)
{
    MessageBox.Show("Message:\n" + ex.Message + "\nDetail:\n" + 
        ex.Detail.InnerText + 
         "\nStackTrace:\n" + ex.StackTrace);
}
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Getting all Items

Currently, the list I'm querying has paging in place and only shows 30 items at a time. Right now, there are between 2k-3k items in the list. Is there an easy way I can get all of the items at once?

The end result that I'm trying to achieve is a total count on various fields. For example, one of the list fields is "Completed" and I would like to get a count of all items where Completed = "No".

GetListItems Paging

Hello everyone.
If anyone is still interested in being able to use the SP GetListItems web service to page through data, look at this:

http://billfotesharepoint.blogspot.com/

Please Rewrite to Not Build XML Via String Manipulation
I really wish you would rewrite the example a bit. It's fine to use XmlDocument to create the basic structure, but I'm at a loss to understand why you break out and start setting InnerXml later on.

You might take this opportunity to rewrite the entire thing using LINQ to XML, but please at least don't show readers how to shoot themselves in the foot by building XML through string manipulation.
Guidance Requested: Selecting documents in chunks
could not delete this post
Missing Asynch content

Where is the GetListItemsAsync method in the MSDN Library? Same with GetListItemsCompletedEventArgs. A web search on these brings up www.hubkey.com with some useless reiteration of the obvious.

Searching just within the MSDN libary returns NO HITS. Only forum references.

<QueryOptions> missing or invalid problem!
I was trying to get all list items out of a List and had to specify the <queryOptions>... I did it like in the documentation above, but it was still giving an error:
"Element <QueryOptions> of parameter queryOptions is missing or invalid"

Then I tried it an other way, using "<QueryOptions><QueryOptions /><QueryOptions>" for an empty declaration instead .. and now .. It is working!

Hope this can be helpful for you, since there is not much information about this error on the web :)

Please change the documentation above to prevent further issues!
Example using Folders

I wrote a blog post that describes how to construct a SOAP envelope that invokes the GetListItems() method in Lists.asmx to retrieve files from a specific folder.  Here it is: http://www.mstechblogs.com/paul/lists-asmx-getlistitems-and-folders

 

Data Connection Wizard for GetListItems for Lists.asmx in InfoPath
Data Connection Wizard for GetListItems for Lists.asmx in InfoPath I have searched high and low for the actual syntax of what is supposed to go in the sample value fields of the Lists.asmx  GetListItems web service, for a data receive connection, and can only find code samples. I need to know exactly what we (non coders) are to place in the sample fields of the Lists.asmx data web connection when setting it up in InfoPath, using the Data Connection Wizard. I am not a coder and only want to do this from the front-end GUI of InfoPath. I have defined various views for my List (and captured all my GUIDs I need for each) and want to create several data connections to populate dropdowns in my InfoPath 2003 form. I am hoping that using this approach, will increase that awful perfomance issues, in InfoPath I am getting when trying to use the alternative XML data receive connections, using the owssvr.dll (with regards to how long it takes to tab from control to control in the Form (even those that do not utilize the connections at all - ie plain text fields))
 
I am referring specifically to these fields in the Data Connection Wizard for GetListItems for Lists.asmx in InfoPath 2003:
tns:ListName ( I know this must be the List's {GUID} of the List so no problem with this field)
tns:viewName ( I know this must be the view's {GUID} of the List so no problem with this field)
tns:query (? is this mandatory, what is the exact syntax)
tns:viewFields (? is this mandatory, given a view is identified, if so what is exact syntax)
tns:rowLimit (I know this is a number so no problem with this field, is it manadatory if already defined in the view?)
tns:queryOptions (? is this mandatory, what is the exact syntax)
tns:webID (? is this mandatory, what is the exact syntax, how do I find this)
 
Various tries give me various errors when trying to complete the data connection from 'missing root elements' if no GUIs are input, to 'Element &lt;ViewFields&gt; of parameter viewFields is missing or invalid' if syntax for defining viewfields are incorrect, to other errors that happen when trying to place queries or queryOptions.
 
Please help us non-coders, understand the proper use of the Lists.asmx GetListitems web service in InfoPath 2003 and successfully complete a data connection setup in InfoPath 2003/2007.
Visual Studio Web-reference and misleading error
$0If you are developing the web service client in Visual Studio 2008 (not sure if other versions too) against a sub-site of the SharePoint site you may come across below issue$0 $0 $0 $0When you add reference to a List Web Service in a sub-site, (http://server/sub-site1/_vti_bin/Lists.asmx) visual studio creates an end-point reference in the config file pointing to the top-level site http://server/_vti_bin/Lists.asmx . You will need to manually change this to the correct site.$0 $0 $0 $0 $0 $0Misleading Error :- "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)." If your web reference/end-point URL is not pointing to the correct subsite site you will get above error, even though the syntax of GUID is correct$0 $0 $0
Misleading Error and WebRefrence issue
$0If you are developing the web service client in Visual Studio 2008 (not sure if other versions too) against a sub-site of the SharePoint site you may come across below issue $0 $0 $0 $0When you add reference to a List Web Service in a sub-site, (http://server/sub-site1/_vti_bin/Lists.asmx) visual studio creates an end-point reference in the config file pointing to the top-level site http://server/_vti_bin/Lists.asmx . You will need to manually change this to the correct site.$0 $0 $0 $0 $0 $0Misleading Error :- "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)." If your web reference/end-point URL is not pointing to the correct subsite site you will get above error, even though the syntax of GUID is correct$0 $0 $0
Why matching results?
I called this method against a List in my sharepoint site hosted by Business Productivity Online service. One of the field is text type, size limit 255, has a value of following:

AAATAFBlcmZAaGVkZ2VpbmRleC5jb20ARgAAAAAAEtfObtNTE0aFX4jrnlHwiQcADwdnS6hwG0qSCGRyZ2uowwCX5ZNrNAAADwdnS6hwG0qSCGRyZ2uowwCX8gJ1AQAA

I query the list to match that field with the following value:

AAATAFBlcmZAaGVkZ2VpbmRleC5jb20ARgAAAAAAEtfObtNTE0aFX4jrnlHwiQcADwdnS6hwG0qSCGRyZ2uowwCX5ZNrNAAADwdnS6hwG0qSCGRyZ2uowwCX8gJ1aQAA

The 4th character from the right is lower-case "a", which is the only letter different from the target value. 

I would expect no match right? However the query returns a match!! WHY???

Here's the code:

// id is the above value
string query = "<Query><Where><Eq><FieldRef Name=\"MyField\" /><Value Type=\"Text\">" + id + "</Value></Eq></Where></Query>";
string viewFields = "<ViewFields><FieldRef Name=\"ID\" /></ViewFields>";
            XElement qNode = XElement.Parse(query);
            XElement vNode = XElement.Parse(viewFields);
            XElement qoNode = XElement.Parse("<QueryOptions/>");
            XElement result = listSoapClient.GetListItems(listName, null, qNode, vNode, "1", qoNode, null);
            XElement rsData = result.Descendants().First<XElement>(e => e.Name.LocalName == "data");
            int count = Int32.Parse(rsData.Attributes("ItemCount").First().Value);

Here's the response:

<listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema" xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<rs:data ItemCount="1">
   <z:row ows_ID="1534" ows_MailID="AAATAFBlcmZAaGVkZ2VpbmRleC5jb20ARgAAAAAAEtfObtNTE0aFX4jrnlHwiQcADwdnS6hwG0qSCGRyZ2uowwCX5ZNrNAAADwdnS6hwG0qSCGRyZ2uowwCX8gJ1AQAA" ows_MetaInfo="1534;#" ows__ModerationStatus="0" ows__Level="1" ows_Title="GEM" ows_owshiddenversion="2" ows_UniqueId="1534;#{E16B5AD2-7536-4895-9BAD-A0FAC19A1CDC}" ows_FSObjType="1534;#0" ows_Created="2010-03-07 11:38:24" ows_FileRef="1534;#Lists/Inbox/1534_.000" />
</rs:data>
</listitems>
Columns missing when using the Lists.GetListItems SharePoint web service
If there is an optional field that is left empty, then the field will not be included in the response XML even if the field is specified in the ViewFields. For details and a possible workaround read the post:
http://pholpar.wordpress.com/2009/06/11/columns-missing-when-using-the-lists-getlistitems-sharepoint-web-service/

Another known issue with GetListItems is that it does not handle the line breaks in multiline text field. Line breaks are simply removed from the response. I don't know any simple workaround for that except creating your own web service using the WSS object model. It would be helpful if all control characters could be returned, setting a parameter in QueryOptions, eg
<QueryOptions><EscapeControlCharacters>TRUE</EscapeControlCharacters></QueryOptions> which could then return these using some agreed format
Need help? Use a Wrapper from CodePlex

A wrapper for accessing the Lists.asmx is avaialble on CodePlex. It is included with the Sharepoint/SSIS Adapter (you do not need to use the adapter for the wrapper).

http://www.codeplex.com/SQLSrvIntegrationSrv

-

The SharepointUtility DLL is a gaccable component which can get column information, get list data - only retrieving columns you want, and also upload/remove files from SharePoint using the Lists.asmx webservice under the hood.

-

It supports a very friendly Linq syntax so you can pass in IEnumerable type of objects of anything to provide the list of columns you want, or a dictionary of data to update in the list.

-

It also wraps a lot of common errors which are NOT clear by the error result that comes back from the webservice. I highly suggest you check out the component if you are using the webservice, it will save you a lot of headache.

Attributes
Where are the attributes defined for what is returned from this call?

returnedItems = _listService.GetListItems(_listID, _viewID, query, viewFieldsNode, "", queryOptions.GetXmlNode(), "");

//Move the values into our list
foreach (System.Xml.Linq.XElement x in returnedItems.GetXElement().Descendants(z + "row")) {
Dictionary<string,object> oneItem = new Dictionary<string,object>();

foreach (XAttribute a in x.Attributes()) {
oneItem.Add(a.Name.LocalName,a.Value);
}
Validating XMLNode for invalid characters?
I was able to implement the web service properly, but is there some way to validate the xmlnode being returned for invalid characters?

For example, the data I am retrieving from the webservice has several items with vertical tabs on them. Upon retrieval via webservice, the application throws an exception of "' ', hexadecimal value 0x0B, is an invalid character", with the ' ' or the 0x0B being the vertical tab.

IncludeMandatoryColumns, multi user and multi lookup columns

It looks like there is an annoying bug with GetListItems:

-

When IncludeMandatoryColumns=TRUE

And the list has at least one mandatory muti user or multi lookup column

And that none of the multi value columns are specified in the ViewFields

GetListItems will return duplicated items - one for each value in the multi-Value column.

--

With one multi value column, one could consider this is by design even though this looks quite strange to adopt this approach in this very specific situation and not in all others.

--

The problem is that things get really buggy when you have a list with multiple multi-value columns.

-

Say we have the following mandatory multi-value columns: MVC01 (multi-user) and MVC02 (multi-lookup)

The following item (I separate multiple values with a comma):

Title : Whatever value

MVC01: John,Rick,Blade

MVC02: Beverages,Condiments

-

If you do not specify any of the multi-value columns in ViewFields element, GetListItems will return 3 items:

Whatever value - John - Beverages

Whatever value - Rick - Condiments

Whatever value - Blade

-

One would expect the following result:

Whatever value - John - Beverages
Whatever value - John - Condiments
Whatever value - Rick - Beverages
Whatever value - Rick - Condiments
Whatever value - Blade - Beverages

Whatever value - Blade - Condiments

--

Things get even worse if you specify one of the multi-value column as part of the ViewFields element.

-

Say you specify MVC02, GetListItems will only return one item:

Whatever value - John - Beverages,Condiments

-

If you specify MVC01, it will return:

Whatever value - John,Rick,Blade - Beverages

True Object reference not set to an instance of an object

I connect this webservice via Nintex but got the error message "True Object reference not set to an instance of an object"

Here is my soap :

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<listName>{33F58256-9964-43B2-B511-0D5AB918F4D6}</listName>
<viewName>{53B6AF25-BA41-421F-812D-045219E3C576}</viewName>
<query>
<OrderBy>
<FieldRef Name="ID" />
</OrderBy>
</query>

<viewFields>
<FieldRef Name="ID" />
</viewFields>
<rowLimit>100</rowLimit>
<queryOptions />
<webID></webID>
</GetListItems>
</soap:Body>
</soap:Envelope>

What's wrong ?

Thanks

Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
"Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)."

For sub-site, When using the name and not the GUID in GetList, you must enter full URL in end-point :

<endpoint address="http://server/sub-site1/sub-site2/_vti_bin/Lists.asmx" ...>

And not :

<endpoint address="http://server/_vti_bin/Lists.asmx" ...>


"Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)."
"Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)."

this is the error i get when i try to use the code given...
webId parameter

I would LOVE to see this code compile as writtern. I have not had any luck in using this method of this web service without something in the 7th parameter (webId). Through trial and error, I've learned that passing an empty string will allow the method to operate like it did under v2.

(or you can use null/nothing for the webID parameter)