Dynamics CRM in the Field

Information from the Microsoft Dynamics CRM PFE team working in the field

Microsoft Dynamics CRM 2011 Custom Contact Entry Website using Early-Bound entity Classes.

Microsoft Dynamics CRM 2011 Custom Contact Entry Website using Early-Bound entity Classes.

  • Comments 6

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 know attribute names in CRM that have the ability to use IntelliSense will remove the need to continually look up 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.

image

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

image

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 ”

image

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.

image

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.

image

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

image

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

image

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.

image

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

image

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

 image

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/2011/Organization.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.

image

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.

protected void Button1_Click(object sender, EventArgs e)

    {

        //Authenticate using credentials of the logged in user;       

        ClientCredentials Credentials = new ClientCredentials();

        Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

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

        Uri OrganizationUri = new Uri("http://<SERVERURL>/<ORGNAME>/XRMServices/2011/Organization.svc");

        Uri HomeRealmUri = null;

 

        //OrganizationServiceProxy serviceProxy;       

        using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(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 = "";

        }

    }

 

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.

image

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.

image

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

image

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;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        ClientCredentials Credentials = new ClientCredentials();

        Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

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

        Uri OrganizationUri = new Uri("http://<ServerName>/<Orgname>/XRMServices/2011/Organization.svc");

        Uri HomeRealmUri = null;

 

        //OrganizationServiceProxy serviceProxy;       

        using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(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.

image

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.

image

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

image

 

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

Development Workshop

Code Review

  • useful psot, please tell how can i add tracing in this code and also how can i debug aspx website page in CRM.

  • Hi,

    I m getting this error:

    InvalidOperationException was unhandled by user code.

    Metadata contains a reference that cannot be resolved: 'http://win-l4g6g1o2m61:5555/LocusGroup/XRMServices/2011/Organization.svc?wsdl'.

    so, how to resolve this error.

    Could you please give me some suggestion.

  • Hi   JMorlock,

    if suppose custom entity contains lookup field or picklist mean how you will map them..

    Please give me solution for this.

    Thanks and Regards,

    Gangadhar

  • I have a similar question to Gangadhar.  Do you have a workaround for when an entity contains a lookup field?

  • Hi

    If you are exposing this web site to the internet you will need to get the External Connector License.

    Regards

    FK

    for more CRM help check http://www.immix.co.za  

  • Hi ,

    I want to develop an html application , Without using c# code, only java script allowed.

    Have in solution or demo ??

Page 1 of 1 (6 items)
Leave a Comment
  • Please add 2 and 1 and type the answer here:
  • Post