Thursday, 5 March 2015

03:34 - No comments

How to Create a Simple Webpage Leveraging The CRM 2013 IOrganizationService Web Service | MS CRM

Create a  Custom Web Site
Step by Step

1.      Create a new Web Site:

a.           Open Visual Studio 2010 to create a new web site
b.       Click File | New | Web Site
c.        Under Installed Templates, click Visual C# and choose ASP .NET Empty Web Site. Name the
           project "CRMContactDataEntry". Click OK.
d.       Click Website|Add New Item. This is the actual aspx page that we will develop on.
e.       Under Installed Templates, click Visual C# and choose Web Form. Click OK.
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.       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. (i.e. “http://crmsrv:5555/org1/XRMServices/2011/Organization.svc”)
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 = newUri("http://<SERVERURL>/<ORGNAME>/XRMServices/2011/Organization.svc");
        Uri HomeRealmUri = null;

        //OrganizationServiceProxy serviceProxy;       
        using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
        {
            IOrganizationService service = (IOrganizationService)serviceProxy;

            //Instantiate the contact object and populate the attributes.
            Entity contact = new Entity("contact");
            contact["firstname"] = txtFirstName.Text.ToString();
            contact["lastname"] = txtLastName.Text.ToString();
            contact["emailaddress1"] = txtEmailAddress.Text.ToString();
            contact["telephone1"] = txtPhoneNumber.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 = "";
        }
    } 
5. Add the Microsoft.Xrm.Sdk and Microsoft.crm.sdk.proxy assemblies as references
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.  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;
e. 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;

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 = newUri("http://<ServerName>/<Orgname>/XRMServices/2011/Organization.svc");
        Uri HomeRealmUri = null;

        //OrganizationServiceProxy serviceProxy;       
        using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
        {
            IOrganizationService service = (IOrganizationService)serviceProxy;

            //Instantiate the contact object and populate the attributes.
            Entity contact = new Entity("contact");
            contact["firstname"] = txtFirstName.Text.ToString();
            contact["lastname"] = txtLastName.Text.ToString();
            contact["emailaddress1"] = txtEmailAddress.Text.ToString();
            contact["telephone1"] = txtPhoneNumber.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 = "";
        }
    }
}

6.    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.
Note: The user will need to be a CRM user for the contact to be created.

       

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

         

0 comments:

Post a Comment