Pages

Wednesday 5 March 2014

Connecting CRM to Custom Web Page

Here a Custom Web Page is connected to CRM. Now for doing so we require SDK and Visual Studio.

the following Steps can be used to do so

a)  Open Visual Studio create a new Web Site of type ASP.NET Empty Web Site give a desired name and click on Ok. I have my form name as "crmsample".


 
b) now In Solution Explorer, right click on Project(crmsample), go to Add and click on New Item. this new Item is the actual web page in which the fields are displayed. Now select the Web Form type of template.

 

c) Within the Solution Explorer, right Click on References and Click on Add Reference, now a pop-up will be displayed select the following Assemblies or browse it to the SDK/Bin folder there you can get these Assemblies.
  • System.ServiceModel.Description
  • Microsoft.Xrm.Sdk.Client
  • System.Net
  • Microsoft.Xrm.Sdk

d) Now go to Solution Explorer, right click on the form name(I had given name as Default) and click on View Designer.
 
e)  Now add corresponding labels and text boxes and button with the help of toolbox{here I am creating a contact record on clicking of a button}. The below screen shot explains it.

 
f) I've changed the Id's of text boxes,  by right clicking on text boxes go to properties, In properties toolbox you can change its ID.
for First Name - txtFirstName
for Last Name - txtLastName
for Email Address - txtEmailAddress
for Phone Number - txtPhoneNumber

g)  Now on click of Submit button the record must be created, so for doing thse we must give an action to the button so double click on button, then a snippet for the action of button on clicking will be displayed. add the following gode within the on click event of Submit button.
------------------>
protected void Button1_Click(object sender, EventArgs e)
        {
            ClientCredentials Credentials = new ClientCredentials();
            Credentials.UserName.UserName = "your User Name";
            Credentials.UserName.Password = "your Password";
            //Uncomment th below lines to use the default username and password for the browser in which CRM is currently opened
            //Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
            //This URL needs to be updated to match the servername and Organization for the environment.
            Uri OrganizationUri = new Uri("Organization svc URL");
            Uri HomeRealmUri = null;
      
            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 = "";
            }
        }
----------------->
also add the included references to this file at top of the code.

h) Now click Complie and Run the code. The webpage will be opened in default web browser. the following screen shot shows it
 
i) Now fill the fields and click on Submit Button. Now go to your CRM and see the contact entity, a record will be created.
Note:- The Mandatory Fields in CRM should be filled or else the record will not be created and instead it will throw an error. 

No comments:

Post a Comment