1 out of 1 rated this helpful - Rate this topic

SPList.GetItems Method (SPQuery)

Windows SharePoint Services 3
Returns a collection of items from the list based on the specified query.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
public SPListItemCollection GetItems (
	SPQuery query
)

Parameters

query

An SPQuery object that contains the query.

Return Value

An SPListItemCollection object that represents the items.

The following code example uses the GetItems method to return and display items with a "Schedule" field value that equals "2 weeks".

This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

The example assumes the existence of an .aspx page that contains a label control.

SPSite oSiteCollection = SPContext.Current.Site;
SPList oList = oSiteCollection.AllWebs["Site_Name"].Lists["List_Name"];
SPQuery oQuery = new SPQuery();
oQuery.Query = "<Where><Eq><FieldRef Name='Schedule'/>" +
        "<Value Type='CHOICE'>2 weeks</Value></Eq></Where>";
SPListItemCollection collListItems = oList.GetItems(oQuery);

foreach (SPListItem oListItem in collListItems)
{
    Label1.Text += SPEncode.HtmlEncode(oListItem["Title"].ToString()) 
        + " -- " + SPEncode.HtmlEncode(oListItem["EndDate"].ToString())   
        + "<BR>";
}

After instantiating an SPQuery object through a constructor, the example uses Collaborative Application Markup Language Core Schemas to define criteria for the query, which is passed as parameter in the GetItems method.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
To get non-folder items recursively
To get items recursively

SPQuery query= new SPQuery();
query.ViewAttributes = "Scope=\"Recursive\"";

To get all items including folders recursively

SPQuery query= new SPQuery();
query.ViewAttributes = "Scope=\"RecursiveAll\"";

No need of &lt;QUERY&gt;element in the SPQuery.Query
Nevermind, I posted this but didn´t actually realized that it doesn´t say to include QUERY element. :-)

I was having a issue where it would not return the correct result for that query. I found out that in Query propertie of SPQuery there´s no need for the QUERY element. Check this link: http://sharepointxperience.blogspot.com/2007/10/spquery-returns-all-items.html

Also, there´s a tool called "CAML Query Builder" from U2U, which is very very helpfull on creating the CAML query.