HTML Dropdown

Thursday, 14 July 2016

Create a Sample web application based on Microsoft Dynamics CRM 2013


1. Generate early bound types

Run the CrmSvcUtil.exe tool, with the “Microsoft.Xrm.Client.CodeGeneration” extension, to generate your entity classes and service contexts. The following is an example command to create 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 is for an on-premises organization. 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.cs /url:https://<mydomain>.api.crm.dynamics.com/XRMServices/2011/Organization.svc

/username:<myusername>@<mydomain>.onmicrosoft.com /password:<mypassword> /namespace:Xrm /serviceContextName:XrmServiceContext

2. Create a ASP.NET Empty Web Application project in Visual studio 2012.


 

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

 
4. Add the following references from .NET.

System.IdentityModel.dll

Microsoft.ServiceBus.dll

System.Data.Services.dll

System.Data.Services.Client.dll

System.Runtime.Serialization.dll



5. Make sure the web application project specifies .NET Framework 4.5.2 as the target framework and not .NET Framework 4.5.2 Client Profile. Check the properties of the project by selecting the ConsoleApplication1 project in Solution Explorer and clicking Alt-Enter.

Right Click WebApplication1 -> Properties

6. Right-click the project in Visual Studio, click Add, and then click Existing Item.

Select the “Xrm.cs” file that you created when you generated the early bound types.

Right-click your project again, click Add, and then click New Item.

Select Application Configuration File from the options and then click Add.

Edit the Web.config configuration file with your specific connection string.

<?xml version="1.0" encoding="utf-8"?>

<!--

  For more information on how to configure your ASP.NET application, please visit

  http://go.microsoft.com/fwlink/?LinkId=169433

  -->

<configuration>

  <configSections>

    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

    <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client" />   

 

  </configSections>

  <connectionStrings>

    <add name="Xrm" connectionString="Server=ServerName; Domain=DomainName; Username=UserName; Password=Password" />

  </connectionStrings>

  <microsoft.xrm.client>

    <contexts>

      <add name="Xrm" type="Xrm.XrmServiceContext" />

    </contexts>

  </microsoft.xrm.client>

 

  <system.web>

    <compilation debug="true" targetFramework="4.0" />

    <httpRuntime />

    <pages>

      <controls>

        <add tagPrefix="crm" namespace="Microsoft.Xrm.Portal.Web.UI.WebControls" assembly="Microsoft.Xrm.Portal" />

      </controls>

    </pages>

  </system.web>

 

  <entityFramework>

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">

      <parameters>

        <parameter value="mssqllocaldb" />

      </parameters>

    </defaultConnectionFactory>

    <providers>

      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />

    </providers>

  </entityFramework>

<system.serviceModel>

  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

</system.serviceModel>

</configuration>

 

7. Right Click your web application -> add MasterPage Site.master -> Design it like below:-

 <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="WebApplication1.Site" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head id="Head1" runat="server">

                <title></title>

                <link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />

                <asp:ContentPlaceHolder ID="HeadContent" runat="server">

                </asp:ContentPlaceHolder>

</head>

<body>

                <form id="Form1" runat="server">

                <div class="page">

                                <div class="header">

                                                <div class="title">

                                                                <h1>

                                                                                Web Application TestProject to connect with CRM

                                                                </h1>

                                                </div>

                                                <div class="clear hideSkiplink">

                                                                <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">

                                                                                <Items>

                                                                                                <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home" />

                                                                                                <asp:MenuItem NavigateUrl="~/WebForm_LinqDataSource.aspx" Text="DisplayAllContactsFromCRM" />

                                                                                                <asp:MenuItem NavigateUrl="~/WebForm_SavedQueryDataSource.aspx" Text="DisplayActiveContactsView" />

                                                                                                <asp:MenuItem NavigateUrl="~/WebForm_FromSavedQuery.aspx" Text="DisplayCreateContactView" />

                                                                                                <asp:MenuItem NavigateUrl="~/WebForm_CodeBehindDataSource.aspx" Text="Code Behind Data Source" />

                                                                                </Items>

                                                                </asp:Menu>

                                                </div>

                                </div>

                                <div class="main">

                                                <asp:ContentPlaceHolder ID="MainContent" runat="server" />

                                </div>

                                <div class="clear">

                                </div>

                </div>

                <div class="footer">

                </div>

                </form>

</body>

   <div class="footer">

       <asp:ContentPlaceHolder ID="FootContent" runat="server" />

   </div>

</html>

8. Create a webpage – Contact Grid 1: DisplayAllContactsFromCRM


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:

<asp:Content ContentPlaceHolderID="MainContent" runat="server">

   <!--This example lists all contacts from the 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>

</asp:Content>

9. Build the project.

Right-click the aspx page and select View in Browser. The results should look something like this:

  

 


10. Create another webpage – Contact Grid 2: DisplayActiveContactsView


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.

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

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

   <asp:GridView ID="GridView1" DataSourceID="ActiveContacts" runat="server" />

</asp:Content>

 

11. Build the project.

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:


 

12. Create 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”:


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:

using System;

using System.Collections.Generic;

using System.Data.Services;

using System.Data.Services.Common;

using System.Linq;

using System.ServiceModel.Web;

using System.Web;

 

namespace WebApplication1

{

    public class CrmData : DataService<Xrm.XrmServiceContext>  /* TODO: put your data source class name here */

    {

        // This method is called only once to initialize service-wide policies.

        public static void InitializeService(DataServiceConfiguration config)

        {

            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.

            // Examples:

              config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);

              config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);

              config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;

        }

    }

}

 

13. Create a webpage – Contact Form 1: DisplayCreateContactView


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



  1. Add columns to the view that you want to have appear as fields in the generated form.
  2. Click Save and Publish.
  3. Right-click your web project in Microsoft Visual Studio and add a new web form called “WebForm_FromSavedQuery.aspx”.
  4. Add the following code to the new aspx page:

 

<asp:Content ContentPlaceHolderID="MainContent" runat="server">

   <asp:ScriptManager runat="server" />

   <crm:CrmDataSource ID="Contacts" runat="server" />

   <crm:CrmEntityFormView DataSourceID="Contacts" EntityName="contact" SavedQueryName="Create Contact Webform" runat="server" />

</asp:Content>

 

  1. Build the project.
    Right-click the aspx page and click View in Browser. The results should look something like this:

15. Create another webpage – Contact Grid 3 :Code Behind Data Source


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:Content ContentPlaceHolderID="MainContent" runat="server">

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

            <asp:TemplateField HeaderText="Email">

                        <ItemTemplate>

                               <asp:Label ID="Label1" Text='<%# Eval("emailaddress1") %>' runat="server" />

                        </ItemTemplate>

                 </asp:TemplateField>

          </Columns>

   </asp:GridView>

</asp:Content>

  1. Edit the code-behind file WebForm_CodeBehind.aspx.cs as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Xrm;
     
    namespace WebApplication1
        {
        public partial class WebForm_CodeBehindDataSource : System.Web.UI.Page
            {
            protected void Page_Load(object sender, EventArgs e)
                {
     
                    var xrm = new XrmServiceContext("Xrm");
     
                    //grab all contacts where the email address ends in @example.com
                    var exampleContacts = xrm.ContactSet
                        .Where(c => c.EMailAddress1.EndsWith("@******"));
     
                    ContactsGridView.DataSource = exampleContacts;
                    ContactsGridView.DataBind();
     
                }
            }
        }

16. Build the project.

Right-click the aspx page and click View in Browser. The results should look something like this:

 


Home.aspx


 

 

4 comments: