This documentation is archived and is not being maintained.

SPDataSource.SelectCommand Property

Gets or sets a query that is used to retrieve list data.

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

public string SelectCommand { get; set; }

Property Value

Type: System.String
A System.String object that contains a query expressed as a Collaborative Application Markup Language (CAML) fragment.

The query string in the SelectCommand property is used by the associated SPDataSourceView object to select data.

The following example shows page markup that links a data-bound grid control to an SPDataSource control that is configured to query the Contacts list in the root Web site of a site collection. The SelectCommand property of the data source control contains a Where clause that filters list data to include only items where the job title contains the word "sales." The query also specifies that the data should be sorted on the Company field.


The query string in the example has been modified to make it easier to read. On an actual ASP.NET page the string should be on a single line.

<SharePoint:SPDataSource ID="SPDataSource1" runat="server"
                          <FieldRef Name='JobTitle'/>
                          <Value Type='Text'>Sales</Value>
                        <FieldRef Name='Company' Ascending='true' />
        <asp:Parameter Name="WebID" DefaultValue="RootWeb" />
        <asp:Parameter Name="ListName" DefaultValue="Contacts"  />
<asp:GridView ID="GridView1" runat="server" EnableViewState="false"
        <asp:BoundField HeaderText="Company" DataField="Company" />
        <asp:BoundField HeaderText="First Name" DataField="FirstName" />
        <asp:BoundField HeaderText="Last Name" DataField="Title" />
        <asp:BoundField HeaderText="Job Title" DataField="JobTitle" />
        <asp:BoundField HeaderText="Business Phone" DataField="WorkPhone" />
        <asp:BoundField HeaderText="Email Address" DataField="Email" />