04.06.2011, 08:16 | #1 |
Участник
|
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.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.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.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.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. 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, напишите личное сообщение администратору. |
|
|
Опции темы | Поиск в этой теме |
Опции просмотра | |
|