ASP.NET web forms and data binding (Dynamics CRM 2013)
Applies To: Dynamics CRM 2013
One of the most popular web programming technologies is ASP.NET web forms, which binds data to controls. Developer Extensions for Microsoft Dynamics CRM 2013 has a number of mechanisms that make it easy to build an ASP.NET web form that binds to Microsoft Dynamics CRM data.
In This Topic
Use the CrmService context and LinqDataSource control
Use CrmDataSource control and FetchXML
Use CrmMetadataDataSource control
Use code-behind data binding
Use saved queries in a portal
Use the CrmService context and LinqDataSource control
The recommended practice for using Developer Extensions for Microsoft Dynamics CRM 2013 is to run the CrmSvcUtil.exe code generation tool, with the Microsoft.Xrm.Client.CodeGeneration extension, to generate the service context and data transfer object classes that match your Microsoft Dynamics CRM entity model. The service context class includes an IQueryable interface for every CRM entity in your solution. The service context class can easily be used with the ASP.NET LinqDataSource control to facilitate declarative data-binding that doesn’t need any code-behind. You set the ContextTypeName attribute of the LinqDataSource control to the type name of your service context and set the TableName attribute to the property name of the entity that you want to query. After the Microsoft Dynamics CRM data is bound to the data source control, you can use any ASP.NET data bound control to extract and display the data including repeaters and the GridView control.
The following code uses a LinqDataSource control to bind to the CRM contacts and display them with a GridView. This same example can be seen in Walkthrough: Build a web application that connects to Microsoft Dynamics CRM 2013 using developer extensions.
<!--This example lists all contacts from the Microsoft Dynamics CRM system. -->
<asp:LinqDataSource ID="Contacts" ContextTypeName="Xrm.XrmServiceContext" TableName="ContactSet" runat="server" />
<asp:GridView DataSourceID="Contacts" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label Text='<%# Eval("firstname")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label Text='<%# Eval("lastname")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label Text='<%#Eval("address1_city") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<!--This example lists all contacts from the Microsoft Dynamics CRM system -->
<asp:LinqDataSource ID="Contacts" ContextTypeName="Xrm.XrmServiceContext" TableName="ContactSet" runat="server" />
<asp:GridView DataSourceID="Contacts" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label Text='<%# Eval("firstname")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label Text='<%# Eval("lastname")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label Text='<%#Eval("address1_city") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This shows the following results in HTML.
Use CrmDataSource control and FetchXML
If you don’t want to use the CrmSvcUtil.exe tool to generate a domain context and data transfer object, you can use the Microsoft.Xrm.Client.CrmOrganizationServiceContext.
The following example uses FetchXml to query Microsoft Dynamics CRM and the CrmDataSource control to bind to CRM data. The results are displayed in a GridView control. Note that the DataItem in the grid view is of type Entity. To bind to columns in the results, you can either cast the Container.DataItem to a Entity class or you can bind to the properties in the default indexer by using Eval("[attribute-logical-name]").
<crm:CrmDataSource ID="Contacts" runat="server">
<FetchXml>
<fetch>
<entity name="contact" />
</fetch>
</FetchXml>
</crm:CrmDataSource><asp:GridView DataSourceID="Contacts" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField HeaderText="Full Name">
<ItemTemplate>
<asp:Label Text='<%# Eval("[fullname]") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created On">
<ItemTemplate>
<asp:Label Text='<%# Eval("[createdon]") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Modified">
<ItemTemplate>
<asp:Label Text='<%# Eval("[modifiedon]") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns></asp:GridView>
<crm:CrmDataSource ID="Contacts" runat="server">
<FetchXml>
<fetch>
<entity name="contact" />
</fetch>
</FetchXml>
</crm:CrmDataSource>
<asp:GridView DataSourceID="Contacts" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField HeaderText="Full Name">
<ItemTemplate>
<asp:Label Text='<%# Eval("[fullname]") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created On">
<ItemTemplate>
<asp:Label Text='<%# Eval("[createdon]") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Modified">
<ItemTemplate>
<asp:Label Text='<%# Eval("[modifiedon]") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This shows the following results in HTML.
Use CrmMetadataDataSource control
The CrmMetadataDataSource control allows for data binding to Microsoft Dynamics CRM metadata. The most useful purpose is to bind a drop-down list to a CRM option set that you use in a data form that is collecting data to store in CRM.
The following example demonstrates using the CrmMetadataDataSource control to bind to three separate option sets in the CRM case (incident) entity.
<h2>Incident Picklists</h2>
<h3>Priority</h3>
<crm:CrmMetadataDataSource ID="PriorityCodes" runat="server"
EntityName="incident"
AttributeName="prioritycode" />
<asp:DropDownList runat="server"
DataSourceID="PriorityCodes"
DataTextField="OptionLabel"
DataValueField="OptionValue" />
<h3>Satisfaction Rating</h3>
<crm:CrmMetadataDataSource ID="SatisfactionCodes" runat="server"
EntityName="incident"
AttributeName="customersatisfactioncode" />
<asp:DropDownList runat="server"
DataSourceID="SatisfactionCodes"
DataTextField="OptionLabel"
DataValueField="OptionValue" />
This shows the following results in HTML.
Use code-behind data binding
The previous examples used specific data source controls that implement declarative data binding. The XrmServiceContext class can also be used with data binding using code-behind techniques. The service context that is generated by the CrmSvcUtil.exe tool (with the Microsoft.Xrm.Client.CodeGeneration extension) has IQueryable properties for all CRM entities. You can write standard .NET Language-Integrated Query (LINQ) queries or use expressions with these properties and bind them directly to your repeaters or DataGrid controls.
More information: Walkthrough: Build a web application that connects to Microsoft Dynamics CRM 2013 using developer extensions
Use saved queries in a portal
Microsoft Dynamics CRM has the ability to allow the customer to save a query or a view for later retrieval. The view has column order and definitions, sorting order, and filter options. It is a convenient technique to allow users to configure a view in CRM and then a developer can use the SavedQueryDataSource control to bind to that view and use a GridView control with AutoGenerateColumns property turned on. This allows a developer to drop a CRM view directly into a webpage and have the view contents be managed in Microsoft Dynamics CRM. Business users can then change the contents of the view without having to work through a developer to make a webpage change and promote a new version of the website to the production server.
For an example of this, as well as an example of how to generate a data entry form from a saved query using the CrmEntityFormView control, see Walkthrough: Build a web application that connects to Microsoft Dynamics CRM 2013 using developer extensions.
See Also
Portal developer guide for Microsoft Dynamics CRM 2013
Prepare for portal development (Dynamics CRM 2013)
Manage portal content (Dynamics CRM 2013)
Portal authentication (Dynamics CRM 2013)
Use the Website Copy tool (Dynamics CRM 2013)
Portal walkthroughs (Dynamics CRM 2013)