SPQuery.ViewXml property

Gets or sets the XML schema that defines the view.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Public Property ViewXml As String
Dim instance As SPQuery
Dim value As String

value = instance.ViewXml

instance.ViewXml = value

Property value

Type: System.String
A string that contains the view schema in Collaborative Application Markup Language.

The ViewXml property contains a string that corresponds to the inner XML of the View element in CAML.


For information about how to use Language-Integrated Query (LINQ) queries to retrieve list items in SharePoint Foundation, see Managing Data with LINQ to SharePoint.

The following code example uses the ViewXml property to define a query in CAML.

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

Dim webSite As SPWeb = SPContext.Current.Site.AllWebs("Site_Name")
    Dim list As SPList = webSite.Lists("List_Name")

    Dim query As New SPQuery()
    query.ViewXml = "<View><Query><OrderBy><FieldRef Name='ID'/>" + _
        "</OrderBy><Where><Or><Geq><FieldRef Name='Field1'/>" + _
        "<Value Type='Number'>1500</Value></Geq><Leq>" + _
        "<FieldRef Name='Field2'/><Value Type='Number'>500</Value>" + _
        "</Leq></Or></Where></Query><ViewFields>" + _
        "<FieldRef Name='Title'/>" + _
        "<FieldRef Name='Field1'/><FieldRef Name='Field2'/>" + _
        "<FieldRef Name='Field3'/><FieldRef Name='Field4'/>" + _
    Dim items As SPListItemCollection = list.GetItems(query)

    Dim item As SPListItem
    For Each item In  items
        Response.Write((SPEncode.HtmlEncode(item.Xml) + "<BR>"))
    Next item
End Try

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 Disposing Objects.