Export (0) Print
Expand All

SPQuery.ViewXml Property

Gets or sets the XML schema that defines the view.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

[ClientCallableConstraintAttribute(Type = ClientCallableConstraintType.Custom, FixedId = "aa", Value = "It MUST defines view element")]
public string ViewXml { get; set; }

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.

using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["Site_Name"])
    SPList oList = oWebsite.Lists["List_Name"];

    SPQuery oQuery = new SPQuery();

    oQuery.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'/>" +
    SPListItemCollection collListItemsAvailable = oList.GetItems(oQuery);

    foreach (SPListItem oListItemAvailable in collListItemsAvailable)
        Response.Write(SPEncode.HtmlEncode(oListItemAvailable.Xml) + 

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.

Community Additions

© 2014 Microsoft