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
        
Set up your Web application project in Visual Studio
Create a Web Page
Create Another Web Page
Create a WCF Data Service
Create a Web Page
Create Another Web Page
        
      
      Set up your Web application project in Visual Studio
Create a Web Page
Create Another Web Page
Create a WCF Data Service
Create a Web Page
Create Another Web Page
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 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
- 
              Create a new ASP.NET Web application project in 
Microsoft Visual Studio. This sample uses “WebAppWalkthrough” as the 
project name.
 
- 
              Add the following references from the SDK\binfolder.
 - AntiXSSLibrary.dll
- Microsoft.Crm.Sdk.Proxy.dll
- Microsoft.Xrm.Client.dll
- Microsoft.Xrm.Portal.dll
- Microsoft.Xrm.Portal.Files.dll
- Microsoft.Xrm.Sdk.dll
 
- 
              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.
 
- 
              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.
 
- 
              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" /> 
- 
              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> 
- 
              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.
        
      
      - 
              Right-click your project and add a new Web form called “WebForm_LinqDataSource.aspx”.
 
- 
              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> 
- 
              Build the project.
 
- 
              Right-click the aspx page and select View in Browser. The results should look something like this:
 
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.
        
      
      - 
              Right-click your project and add a new Web form called “WebForm_SavedQueryDataSource.aspx”.
 
- 
              Add the following to the new aspx page. 
 <crm:SavedQueryDataSource ID="ActiveContacts" SavedQueryName="Active Contacts" runat="server" /> <asp:GridView DataSourceID="ActiveContacts" runat="server" /> 
- 
              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:
 
Create a WCF Data Service
Create a WCF Data Service for Microsoft Dynamics CRM.
        
      
      - 
              Right-click your project and add a new WCF Data Service called “CrmData.svc”:
 
- 
              You need to point the WCF data service at the XrmServiceContext created at the beginning of the walkthrough. Edit the CrmData.svc.csfile 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:
        
      
      - 
              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”.
 
- 
              Add columns to the view that you want to have appear as fields in the generated form. 
 
- 
              Click Save and Publish.
 
- 
              Right-click your Web project in Microsoft Visual Studio and add a new Web form called “WebForm_FromSavedQuery.aspx”.
 
- 
              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" /> 
- 
              Build the project.
 
- 
              Right-click the aspx page and click View in Browser. The results should look something like this:
 
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.
- 
              Right-click your project and add a new Web page called “WebForm_CodeBehindDataSource.aspx”.
 
- 
              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> 
- 
              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(); } } } 
- 
              Build the project.
 
- 
              Right-click the aspx page and click View in Browser. The results should look something like this:
 
 
No comments:
Post a Comment