AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 04.06.2011, 08:16   #1  
Blog bot is offline
Blog bot
Участник
 
25,584 / 848 (80) +++++++
Регистрация: 28.10.2006
crminthefield: Microsoft Dynamics CRM 2011 Custom Contact Entry Website using Early-Bound entity Classes.
Источник: http://blogs.msdn.com/b/crminthefiel...y-classes.aspx
==============

Previously I had posted a blog article on developing a custom website using the Late-Bound Entity Class. This will use a similar project, but with the Early-Bound entity Classes. One benefit of using Early bound types is that IntelliSense will now work when working with CRM entities. This is similar to how the WSDL was used in CRM 4.0. For developers that do now know attribute names in CRM having the ability to use IntelliSense will remove the need to continually lookup schema names while coding. Please refer to the SDK for more documentation on Early-Bound entity Classes and best practices.

Scenario:

The CRM users need the ability to add contacts into the CRM system without using the main CRM application. They would like a simple data entry website with minimal fields. To accomplish this we will develop a web application that connects to the CRM 2011 platform using the IOrganizationService Web Service. We will use the CRM Code Generation Tool to generate early-bound entity classes. Note: Each time that you make customizations to the system the classes must be regenerated to reflect the new entities/attributes.

 

The custom website will allow the user to specify the contact name, email address, and phone number. When the submit button is pressed on the page it will automatically create the contact record within CRM. Since this is using AD authentication the project will only work for CRM 2011 On-Premise.

 

Requirements:

-          Visual Studio 2010

-          CRM 2011 SDK

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=420f0f05-c226-4194-b7e1-f23ceaa83b69

Exercise 1: Create a Custom Web Application

Step by Step

1.       Create a new Web Application:
a.       Open Visual Studio 2010 to create a new web application.

b.       Click File | New | Project.

c.        Under Installed Templates, click Visual C# | Web and choose ASP .NET Empty Web Application. Name the project "ContactDataEntry". Click OK.

d.       Click Project|Add New Item. This is the actual aspx page that we will develop on.

e.       Under Installed Templates, click Visual C# | Web and choose Web Form. Name the Item “Default.aspx”. Click Add.

2.       Add a Title and Field Names to the Default.aspx page.
a.       Within Solution Explorer right click on the Default.aspx page and choose View Designer.



b.       Click in the box on the Default.aspx page and type the text “CONTACT ENTRY FORM” to give the page a title.



c.        Hit the Enter Key to create a new line below the title and type the text “First Name “.

d.       Add three more lines with the text “Last Name “, “Email Address ”, and “Phone Number ”



3.       Add four Textbox’s and a Button to the aspx page.
a.       Click View|Toolbox. When the toolbox window opens click the pin icon to keep the window open.



b.       Find the TextBox control within the Toolbox window. Drag the TextBox control onto the Default.aspx page behind the text “First Name”. This creates a single Textbox on the page.



c.        Right click the TextBox control on the default.aspx page and choose Properties. The properties window will display on your right hand side.



d.       Find the ID property and change the value to txtFirstName.



e.       Repeat steps b-d to create three more TextBox’s with the following names.

·   txtLastName

·   txtEmailAddress

·   txtPhoneNumber                      

f.         Find the Button control within the Toolbox window. Drag the Button control onto the Default.aspx page Under the text “Phone Number”. This creates a single Button on the page.



g.       Right click the Button control on the default.aspx page and choose Properties. The properties window will display on your right hand side.



h.       Find the Text property and change the value to Submit.

 

4.       Generate Early-Bound Entity Classes
a.       Open a command prompt and change the directory to "C:\Program Files\Microsoft Dynamics CRM\Tools".

cd "C:\Program Files\Microsoft Dynamics CRM\Tools"
b.       Run the following command to generate an early-bound classes file on the Desktop with the name "GeneratedTypes.cs". The GeneratedTypes.cs file has a namespace of "Microsoft.Crm.Sdk.Samples" and creates a service context with a name of "Org1Context".

NOTE: Use arguments that match the organization you are targeting (e.g., server name, port, Orgname).

CrmSvcUtil.exe /url:http://crmsrv:5555/Org1/XRMServices/...ganization.svc
/out:%userprofile%\Desktop\GeneratedTypes.cs /namespace:Microsoft.Crm.Sdk.Samples
/serviceContextName:Org1Context

c.        You should see a message stating the file was created successfully.

Code written to C:\Users\Administrator\Desktop\GeneratedTypes.cs

5.       Add GeneratedTypes.cs File to Project.
a.       Click Project | Add Existing Item.

b.       Browse to the GeneratedTypes.cs file on the Desktop and Click Add.

6.       Add code to the Button’s Click Event.
a.       Double-Click the Button control on the Default.aspx page. This will take you to the Default.aspx.cs file where we can see the Button1_Click method.



b.        Add the following code within the Button1_Click code method.

NOTE: You will need to update the Organization URL to match your CRM Servername and OrgName.

protectedvoid Button1_Click(object sender, EventArgs e)

    {

        //Authenticate using credentials of the logged in user;       

        [COLOR= ]ClientCredentials[/COLOR] Credentials = newClientCredentials();

        Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

        //This URL needs to be updated to match the servername and Organization for the environment.

        Uri OrganizationUri = newUri("http:////XRMServices/2011/Organization.svc");

        Uri HomeRealmUri = null;

 

        //OrganizationServiceProxy serviceProxy;       

        using (OrganizationServiceProxy serviceProxy = newOrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))

        {[COLOR= ][/COLOR]

[COLOR= ]          [/COLOR]serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

            IOrganizationService service = (IOrganizationService)serviceProxy;

 

            //Instantiate the contact object and populate the attributes.                          

             Contact contact = new Contact();

             contact.FirstName = txtFirstName.Text.ToString();

             contact.LastName = txtLastName.Text.ToString();

             contact.EMailAddress1 = txtEmailAddress.Text.ToString();

 

             Guid newContactId = service.Create(contact);           

            //This code will clear the textboxes after the contact is created.

           txtFirstName.Text = "";

            txtLastName.Text = "";

            txtEmailAddress.Text = "";

            txtPhoneNumber.Text = "";

        }

    }

 

7.       Add the Microsoft.Xrm.Sdk and Microsoft.crm.sdk.proxy assemblies and other related Assemblies.
a.       Locate the Solution Explorer, right-click the ProjectName and choose Add Reference.



b.       In the Add Reference browser window, click the Browse tab and browse to the location of this assembly reference (this is located in the Bin directory of the downloaded SDK).

c.        Choose Microsoft.Xrm.Sdk and Microsoft.crm.sdk.proxy, click OK.



d.       Locate the Solution Explorer, right-click the ProjectName and choose Add Reference.



e.       In the Add Reference browser window, click the .Net tab.

f.         Choose System.Runtime.Serialization and System.ServiceModel, click OK.

g.       Add the following using statements at the top of the Default.aspx.cs file.

using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk.Client;

using System.Net;

using Microsoft.Xrm.Sdk;

using Microsoft.Crm.Sdk.Samples;
h.       The completed code should look similar to the following.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ServiceModel.Description;

using Microsoft.Xrm.Sdk.Client;

using System.Net;

using Microsoft.Xrm.Sdk;

using Microsoft.Crm.Sdk.Samples;

 

publicpartialclass_Default : System.Web.UI.Page

{

    protectedvoid Page_Load(object sender, EventArgs e)

    {

 

    }

    protectedvoid Button1_Click(object sender, EventArgs e)

    {

        ClientCredentials Credentials = newClientCredentials();

        Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

        //This URL needs to be updated to match the servername and Organization for the environment.

        Uri OrganizationUri = newUri("http:////XRMServices/2011/Organization.svc");

        Uri HomeRealmUri = null;

 

        //OrganizationServiceProxy serviceProxy;       

        using (OrganizationServiceProxy serviceProxy = newOrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))

        {

            serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

            IOrganizationService service = (IOrganizationService)serviceProxy;

 

            //Instantiate the contact object and populate the attributes.                          

            Contact contact = new Contact();

            contact.FirstName = txtFirstName.Text.ToString();

            contact.LastName = txtLastName.Text.ToString();

            contact.EMailAddress1 = txtEmailAddress.Text.ToString();

 

            Guid newContactId = service.Create(contact);           

           

            //This code will clear the textboxes after the contact is created.

            txtFirstName.Text = "";

            txtLastName.Text = "";

            txtEmailAddress.Text = "";

            txtPhoneNumber.Text = "";

        }

    }

}
i.         Type the following line of code after “contact.EmailAddress1 = txtEmailAddress.Text.ToString();” . Notice when you type the period after contact the list of available attributes appear.

     contact.Telephone1 = txtPhoneNumber.Text.ToString();

8.       Compile and Run the Custom Page.
a.       Click Build|Build Solution. If everything is correct you will see it say Build Succeeded at the bottom of the Visual Studio Window.



b.       Click Debug|Start Debugging. This will launch the website locally within Internet Explorer and allow for testing.

c.        Enter information into each of the text boxes and then click the Submit button. The textboxes will automatically clear out once the contact has been created.



d.       Open the CRM Web Client to see the newly created Contact Record.



 

You can find more information regarding related services we provide here.

Development Workshop

Code Review




Источник: http://blogs.msdn.com/b/crminthefiel...y-classes.aspx
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
Microsoft Dynamics CRM Team Blog: Released! Customer Care Accelerator for Microsoft Dynamics CRM 2011 Blog bot Dynamics CRM: Blogs 0 13.05.2011 23:12
Microsoft Dynamics CRM Team Blog: Microsoft Dynamics CRM top sites and downloads ~ 2011 Blog bot Dynamics CRM: Blogs 0 01.04.2011 06:13
Microsoft Dynamics CRM Team Blog: Convergence 2011 Concept Box Invitation Blog bot Dynamics CRM: Blogs 0 16.03.2011 19:11
Все о Microsoft Dynamics CRM: Как установить Microsoft Dynamics CRM 2011 Beta Blog bot Dynamics CRM: Blogs 0 31.10.2010 15:08
Microsoft Dynamics CRM Team Blog: Highlight: Available downloads for Microsoft Dynamics CRM Blog bot Dynamics CRM: Blogs 0 05.11.2008 22:05

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 17:47.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.