Walkthrough: Build a web application that connects to Microsoft Dynamics CRM 2015 using developer extensions 

Applies To: Dynamics CRM 2015

This walkthrough demonstrates how to write a simple web application that connects to Microsoft Dynamics CRM 2015 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

Set up your web application project in Visual Studio

Create a webpage – Contact Grid 1

Create another webpage – Contact Grid 2

Create a WCF data service

Create a webpage – Contact Form 1

Create another webpage – Contact Grid 3

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 global assembly cache, when you run this command. The first command shown here is for an on-premises organization while the second command is for a CRM Online organization. Both commands should be executed as a single command line with no line breaks.

    
        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
    
    
        CrmSvcUtil.exe
        /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration"
        /out:Xrm.cs /url:https://<mydomain>.api.crm.dynamics.com/XRMServices/2011/Organization.svc
        /username:<myusername>@<mydomain>.onmicrosoft.com /password:<mypassword> /namespace:Xrm /serviceContextName:XrmServiceContext
    

    Tip

    The CrmSvcUtil tool is available in the Bin folder of the SDK download or by installing the Microsoft.CrmSdk.CoreTools NuGet package.

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. You can skip this step and the next by simply installing the Microsoft.CrmSdk.Extensions NuGet package.

    • 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.

    • System.IdentityModel.dll

    • Microsoft.Data.Entity.dll

    • System.Data.Services.dll

    • System.Data.Services.Client.dll

    • System.Runtime.Caching.dll

    • System.Runtime.Serialization.dll

  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’ll 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 webpage – Contact Grid 1

Create a basic webpage that displays all contacts in your 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 webpage – Contact Grid 2

Create a webpage that displays contacts in your CRM system in an ASP.NET data grid based on a 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 webpage – Contact Form 1

Create a webpage that renders a contact data entry form based on a Microsoft Dynamics CRM view definition:

  1. In CRM, go to Settings, Customizations, and Customize the System. Create a new view for the contact entity called “Create Contact Webform”.

    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 webpage – Contact Grid 3

Create a webpage 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 webpage 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

See Also

Portal developer guide for Microsoft Dynamics CRM 2015
Portal walkthroughs for Dynamics CRM 2015

© 2016 Microsoft. All rights reserved. Copyright