Friday 19 October 2012

Build a Web Application That Connects to Microsoft Dynamics CRM 2011 Using Developer Extensions


This walkthrough demonstrates how to write a simple Web application that connects to Microsoft Dynamics CRM 2011 and performs a basic create contact transaction.
You can find the sample code that this walkthrough produces in the Sdk\Walkthroughs\Portal\WebAppWalkthrough folder.

In This Topic


Generate Early Bound Types

  1. Run the CrmSvcUtil.exe tool, with the Microsoft.Xrm.Client.CodeGeneration extension, to generate your entity classes and service contexts. The following example command creates a file called “Xrm.cs” that points at an instance of Microsoft Dynamics CRM. Note that the Microsoft.Xrm.Client.CodeGeneration.dll file must be in the same directory as the CrmSvcUtil.exe file, or in the system GAC, when you run this command.
    CrmSvcUtil.exe /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration" /out:Xrm\Xrm.cs /url:http://Crm/Contoso/XRMServices/2011/Organization.svc /domain:CONTOSO /username:administrator /password:pass@word1 /namespace:Xrm /serviceContextName:XrmServiceContext
    

Set up your Web application project in Visual Studio

  1. Create a new ASP.NET Web application project in Microsoft Visual Studio. This sample uses “WebAppWalkthrough” as the project name.
    Create Web application in Visual Studio
  2. 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
  3. 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

    If you do not have the Microsoft.IdentityModel.dll file, you must install Windows Identity Foundation.
  4. Right-click the project in Visual Studio, click Add, and then click Existing Item.
  5. Select the “xrm.cs” file that you created when you generated the early bound types.
  6. Edit the web.config file to register the <microsoft.xrm.client> section. You will need to add a section into the configSections node of the configuration as shown here.
    <configuration>
      <configSections>
        <section name="microsoft.xrm.client"
          type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client" />
    
  7. Edit the web.config file with your specific connection string and context. For the connection string, set the name to “Xrm”. In the <microsoft.xrm.client> section add a context with the name “Xrm” and set the type to the namespace and service context name you provided in Step 1 when you set up the Web application project. In the following example it is Xrm.XrmServiceContext and the assembly part of the type is the name of your Web application, “WebAppWalkthrough”.
    <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, WebAppWalkthrough" />
      </contexts>
    </microsoft.xrm.client>
    
  8. Add the following to the <controls> section of the web.config file to register the Microsoft.Xrm.Portal controls with this Web application.
       <system.web>
         <pages>
           <controls>
             <add tagPrefix="crm" namespace="Microsoft.Xrm.Portal.Web.UI.WebControls" assembly="Microsoft.Xrm.Portal" />
    

Create a Web Page – Contact Grid 1

Create a basic Web page that displays all contacts in your Microsoft Dynamics CRM system in an ASP.NET data grid.
  1. Right-click your project and add a new Web form called “WebForm_LinqDataSource.aspx”.
  2. Add the following to the new aspx page:
    <!--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>
    
  3. Build the project.
  4. Right-click the aspx page and select View in Browser. The results should look something like this:
    View in browser

Create Another Web Page – Contact Grid 2

Create a Web page that displays contacts in your Microsoft Dynamics CRM system in an ASP.NET data grid based on a Microsoft Dynamics CRM view definition.
  1. Right-click your project and add a new Web form called “WebForm_SavedQueryDataSource.aspx”.
  2. Add the following to the new aspx page.
    <crm:SavedQueryDataSource ID="ActiveContacts" SavedQueryName="Active Contacts" runat="server" />
    <asp:GridView DataSourceID="ActiveContacts" runat="server" />
    
  3. Build the project.
  4. Right-click the aspx page and select View in Browser. This page will use the view definition “Active Contacts” to return the records and display the attributes of the view in an ASP.NET GridView control. The results should look something like this:
    View in browser

Create a WCF Data Service

Create a WCF Data Service for Microsoft Dynamics CRM.
  1. Right-click your project and add a new WCF Data Service called “CrmData.svc”:
    Create data service
  2. You need to point the WCF data service at the XrmServiceContext created at the beginning of the walkthrough. Edit the CrmData.svc.cs file as follows:
    namespace WebAppWalkthrough
    {
        public class CrmData : DataService<Xrm.XrmServiceContext>
        {
            // This method is called only once to initialize service-wide policies.
            public static void InitializeService(DataServiceConfiguration config)
            {
                config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
                config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
                config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
            }
        }
    }
    

Create a Web Page – Contact Form 1

Create a Web page that renders a Contact data entry form based on a Microsoft Dynamics CRM view definition:
  1. In Microsoft Dynamics CRM, go to Settings, Customizations, and Customize the System. Create a new view for the Contact entity called “Create Contact Web Form”.
    Create a Web page
  2. Add columns to the view that you want to have appear as fields in the generated form.
  3. Click Save and Publish.
  4. Right-click your Web project in Microsoft Visual Studio and add a new Web form called “WebForm_FromSavedQuery.aspx”.
  5. Add the following code to the new aspx page:
    <asp:ScriptManager runat="server" />
    <crm:CrmDataSource ID="Contacts" runat="server" />
    <crm:CrmEntityFormView DataSourceID="Contacts" EntityName="contact" SavedQueryName="Create Contact Web Form" runat="server" />
    
  6. Build the project.
  7. Right-click the aspx page and click View in Browser. The results should look something like this:
    View in browser

Create Another Web Page – Contact Grid 3

Create a Web page that uses code behind to connect a Microsoft Dynamics CRM data source to an ASP.NET GridView control.
  1. Right-click your project and add a new Web page called “WebForm_CodeBehindDataSource.aspx”.
  2. Add the following code to the new aspx page.
    <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>
    
  3. Edit the code-behind file WebForm_CodeBehind.aspx.cs as follows:
    using System;
    using System.Linq;
    using Xrm;
    
    namespace WebAppWalkthrough
    {
        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();
            }
        }
    }
    
  4. Build the project.
  5. Right-click the aspx page and click View in Browser. The results should look something like this:
    View in browser

No comments:

Post a Comment