Microsoft Dynamics CRM Portal Development #MSDynCRM #CRM2013 Binding CRM data to ASP .NET Web Forms in an Application

There are many ways you can bind data from Microsoft Dynamics CRM into an ASP.Net web form. Dynamics CRM SDK provides a set of DLLs and helper classes that allows for some innovative portal development in Dynamics CRM. The most interesting of which, in my view, is the ability to bind a webpage to Dynamics CRM view so that every time you change the view, the changes are then automatically reflected on your web page on the next refresh.

So, in order to bind Dynamics CRM data to your website pages, you can either use:

  1. asp:LinqDataSource to bind CRM fields data to an ASP.Net webpage,
  2. crm:SavedQueryDataSource to bind a Dynamics CRM view into an Web Form so that any change to the view is reflected on the webpage,
  3. crm:CrmDataSource to have a webpage that renders a contact data entry form based on a Microsoft Dynamics CRM view definition in which case the Web Form displays the columns defined in your Dynamics CRM View, Or,
  4. Use code behind to connect a Microsoft Dynamics CRM data source to an ASP.NET GridView control.

To try the above, first create an ASP.Net Web Application in Visual Studio. Run the CRM Service Utility CRMSvcUtil.exe to generate all your early bind entity classes and service contexts. Add your generated class into your Web Application project.

Also, make sure you add the following references from the SDK\bin folder:
AntiXSSLibrary.dll
Microsoft.Crm.Sdk.Proxy.dll
Microsoft.Xrm.Client.dll
Microsoft.Xrm.Portal.dll
Microsoft.Xrm.Portal.Files.dll
Microsoft.Xrm.Sdk.dll

and then add the following references from .NET:
Microsoft.IdentityModel.dll
Microsoft.Data.Entity.dll
System.Data.Services.dll
System.Data.Services.Client.dll
System.Runtime.Caching.dll
System.Runtime.Serialization.dll

In your Web.config, make sure you add the following:

<configuration>
 <configSections>
 <section name="microsoft.xrm.client"
 type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client" />
<connectionStrings>
 <add name="Xrm" connectionString="Server=http://crm/contoso; Domain=CONTOSO; Username=Administrator; Password=pass@word1" />
</connectionStrings>
<microsoft.xrm.client>
 <contexts>
 <add name="Xrm" type="Xrm.XrmServiceContext, YourWebApplicationProjectName" />
 </contexts>
</microsoft.xrm.client>
 <system.web>
 <pages>
 <controls>
 <add tagPrefix="crm" namespace="Microsoft.Xrm.Portal.Web.UI.WebControls" assembly="Microsoft.Xrm.Portal" />

In each of the above Four cases, you need to create an ASP.Net Form and then add the following code for each case:

(1)

<!--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>

(2)

<crm:SavedQueryDataSource ID="ActiveContacts" SavedQueryName="Active Contacts" runat="server" />
<asp:GridView DataSourceID="ActiveContacts" runat="server" />

(3)

<asp:ScriptManager runat="server" />
<crm:CrmDataSource ID="Contacts" runat="server" />
<crm:CrmEntityFormView DataSourceID="Contacts" EntityName="contact" SavedQueryName="Create Contact Web Form" runat="server" />

(4)

<asp:GridView ID="ContactsGridView" 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>

and the code behind:

using System;
using System.Linq;
using Xrm;
namespace YourCRMPortalProjectNameSpace
{
 public partial class WebForm_CodeBehind : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {
 var xrm = new XrmServiceContext("Xrm");
 //Use all contacts where the email address ends in @example.com.
 var exampleContacts = xrm.ContactSet
 .Where(c => c.EMailAddress1.EndsWith("@example.com"));
 ContactsGrid_CodeBehind.DataSource = exampleContacts;
 ContactsGrid_CodeBehind.DataBind();
 }
 }
}

In all of the above cases, once you create your web form, just save all files, build the project/solution and then right click and click run the webpage in the browser.

You should then end up with something like this:

ASP .Net Webpage rendering Dynamics CRM Data
ASP .Net Webpage rendering Dynamics CRM Data

Examples in this post are taken directly from Dynamics CRM SDK Portal Walkthrough page which describes the ability to build Dynamics CRM Websites and Portals and their webpages and web forms that are directly connected and linked to CRM as a backend.