Condividi tramite


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

Syntax

'Declaration
<ClientCallableAttribute> _
<ClientCallableConstraintAttribute(Type := ClientCallableConstraintType.Custom, FixedId := "aa", Value := "It MUST defines view element")> _
Public Property ViewXml As String
    Get
    Set
'Usage
Dim instance As SPQuery
Dim value As String

value = instance.ViewXml

instance.ViewXml = value
[ClientCallableAttribute]
[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.

Remarks

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

Note

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.

Examples

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")
Try
    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'/>" + _
        "</ViewFields><RowLimit>100</RowLimit></View>"
    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
Finally
    webSite.Dispose()
End Try
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'/>" +
        "</ViewFields><RowLimit>100</RowLimit></View>";
    SPListItemCollection collListItemsAvailable = oList.GetItems(oQuery);

    foreach (SPListItem oListItemAvailable in collListItemsAvailable)
    {
        Response.Write(SPEncode.HtmlEncode(oListItemAvailable.Xml) + 
          "<BR>");
    }
}

Note

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.

See Also

Reference

SPQuery Class

SPQuery Members

Microsoft.SharePoint Namespace