The CrmDataSource is an custom server control that supplies data-bound controls with data from CRM and facilitates inserting, updating and reading of a CRM record. Additional details can be found here.

Adding CrmDataSource to a Page Template

The following control declaration can be added to an ASP.NET page template in the Visual Studio Web Application project.

<adx:CrmDataSource ID="FormViewDataSource" runat="server" />

Control Properties

The CrmDataSource control has several properties that can be specified to modify the behavior.

NameDescription
FetchXml The Fetch XML query used to retrieve data.
EncodeParametersEnabled Value of "true" indicates the parameter values should be HTML Encoded when constructing the FetchXml as a means of input validation.
CrmDataContextName The portal context configuration name that the control binds to.  Default value is unassigned.

FetchXml Examples

The following are examples of specifying the FetchXml declaratively.

Selecting all active account records and only a few attribute values.

<adx:CrmDataSource runat="server" ID="ListViewDataSource" >
	<FetchXml>
		<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
			<entity name="account">
				<attribute name="name" />
				<attribute name="telephone1" />
				<attribute name="accountid" />
				<attribute name="address1_postalcode" />
				<attribute name="address1_line1" />
				<attribute name="address1_stateorprovince" />
				<attribute name="address1_country" />
				<attribute name="address1_city" />
				<order attribute="name" descending="false" />
				<filter type="and">
					<condition attribute="statecode" operator="eq" value="0" />
				</filter>
			</entity>
		</fetch>
	</FetchXml>
</adx:CrmDataSource> 

Selecting a single account record and all attributes.

<adx:CrmDataSource runat="server" ID="FormViewDataSource">
	<FetchXml>
		<fetch mapping='logical'>
			<entity name='account'>
				<all-attributes />
				<filter type='and'>
					<condition attribute = 'accountid' operator='eq' value="{0DDDE30E-4B48-E011-9FFF-00155D035A27}" />
				</filter>
			</entity>
		</fetch>
	</FetchXml>
</adx:CrmDataSource>

A more practical method for specifiying an id for the filter condition is to provide the FetchXml in the Page_Load method. The following example assigns the id of the contact associated with the current authenticated user to the filter condition of the fetch XML to retrieve a single contact record.

FormViewDataSource.FetchXml = string.Format("<fetch mapping='logical'><entity name='{0}'><all-attributes /><filter type='and'><condition attribute = '{1}' operator='eq' value='}'/></filter></entity></fetch>", "contact", "contactid", Contact.ContactId);

Using CrmDataSource with ASP.NET GridView

The following example retrieves accounts from CRM and displays the data in a ASP.NET GridView control.

<adx:CrmDataSource runat="server" ID="AccountDataSource" >
	<FetchXml>
		<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
			<entity name="account">
				<attribute name="name" />
				<attribute name="address1_city" />
				<attribute name="telephone1" />
				<filter type="and">
					<condition attribute="statecode" operator="eq" value="0" />
				</filter>
			</entity>
		</fetch>
	</FetchXml>
</adx:CrmDataSource>
<asp:GridView runat="server" DataSourceID="AccountDataSource" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" PageSize="5">
	<Columns>
		<asp:BoundField DataField='name' HeaderText="Name" SortExpression="name" />
		<asp:BoundField DataField='address1_city' HeaderText="City" />
		<asp:BoundField DataField='telephone1' HeaderText="Telephone" />
	</Columns>
</asp:GridView>

Example GridView output

GridView Output Example

For details on ASP.NET GridView Paging, see  http://msdn.microsoft.com/en-us/library/5aw1xfh3(v=vs.100).aspx

Generating Fetch XML in CRM

The Advanced Find in CRM can be leveraged to produce Fetch XML.

  • Login to CRM
  • Click Advanced Find on the ribbon

Advanced Find ribbon button

  • Build your query
  • Click Results
  • Once satisfied with the results, click Download Fetch XML
  • The file contains the XML that can be assigned to the FetchXml property of the CrmDataSource control

Download Fetch XML