Click to Rate and Give Feedback

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (6)
SPQuery Class (Microsoft.SharePoint)
Represents a query in a list view.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
Public Class SPQuery
Visual Basic (Usage)
Dim instance As SPQuery
C#
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
public class SPQuery

The following code example displays the titles of items in a Tasks list where the Status column equals Completed. The example uses Collaborative Application Markup Language (CAML) to define the query.

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

Visual Basic
Dim webSite As SPWeb = SPContext.Current.Site.RootWeb
Try
    Dim list As SPList = webSite.Lists("Tasks")

    Dim query As New SPQuery()
    query.Query = "<Where><Eq><FieldRef Name='Status'/>" + _
      "<Value Type='Text'>Completed</Value></Eq></Where>"
    Dim items As SPListItemCollection = list.GetItems(query)

    Dim item As SPListItem
    For Each item In  items
        Response.Write((SPEncode.HtmlEncode(item("Title").ToString()) + _
          "<BR>"))
    Next item
Finally
    webSite.Dispose()
End Try
C#
using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)
{

    SPList oList = oWebsiteRoot.Lists["Tasks"];

    SPQuery oQuery = new SPQuery();
    oQuery.Query = "<Where><Eq><FieldRef Name='Status'/>" +
        "<Value Type='Text'>Completed</Value></Eq></Where>";
    SPListItemCollection collListItems = oList.GetItems(oQuery);

    foreach (SPListItem oListItem in collListItems)
    {
        Response.Write(SPEncode.HtmlEncode(oListItem["Title"].ToString()) + 
            "<BR>");
    }
}
NoteNote:

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.

System.Object
  Microsoft.SharePoint.SPQuery
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
SPQuery can get the items from the sub folder      Akhilesh Tiwari   |   Edit   |   Show History
To Get the Search Items from the sub folders of the list using SPQuery you can do following
SPQuery.ViewAttributes = "Scope=\"Recursive\"";
However this is undocumented/non public information so MS can change it any time in the further releases and you code will start breaking.
Instead, one can use SPSiteDateQuery class to search across multiple Lists. you can go to my blog for further detail and example.
http://www.sharepointblogs.com/akhileshtiwari/archive/2007/10/11/query-multiple-lists-using-spsitedataquery-class.aspx

CAML query does not include time for searching lists      puneetspeed   |   Edit   |   Show History

Caml query not using the time part in datetime. If your are doing a caml query and you see that your time is being ignored in datetime try this:

<Value Type=”DateTime” IncludeTimeValue=”TRUE”><Today /></Value>

You have to use IncludeTimeValue=”TRUE”.

Check http://puneetspeed.spaces.live.com/blog/cns!AFAFB88D250BEE36!308.entry for more

Flag as ContentBug
Do not reuse SPQuery instances!      LZandman   |   Edit   |   Show History

Be sure to create a new SPQuery instance each time you use it. You cannot create one SPQuery instance and then reuse it multiple times in a loop where you alter the SPQuery's Query value.

Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker