locked
Create Record through a Web Form RRS feed

  • Question

  • I have created a web form in Visual Studio that when I run locally works great. It takes the input fields and creates a contact in the CRM. I can use the html to put the web form up on a website but then that is where I am stuck and can't seem to find any more information. Where am I connecting the server side code (shown below) so that it captures the input values from the form once published on our website.

    I followed this link here: http://blogs.msdn.com/b/crminthefield/archive/2011/05/18/how-to-create-a-simple-webpage-leveraging-the-crm-2011-iorganizationservice-web-service.aspx to create this. I also copied the html into a web resource and the form successfully shows up but again the code to actually create the contact isn't connecting so I'm not sure how to do that.

    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;
    
    namespace CRMContactDataEntry
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            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://server/name/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 = "";
    
                }
            }
        }
    }

    Thanks for any help!

    -Trevor

    Wednesday, December 4, 2013 3:35 PM

Answers

  • To run the code on the server-side you need to create an ASPX page - is this what you've created ? If so, you will need to deploy the code to a virtual directory within Internet Information Services (IIS) on the web server. The other thing you need to do is configure the application in IIS to use an application pool that has the identity of an AD account that is associated with a CRM user


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Wednesday, December 4, 2013 8:01 PM
    Moderator

All replies

  • If you want to use html webresurce you need to use clientside code to creat entity record, you can use rest endpoints or soap endpoints, download latest SDK for sample code

    HTH


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, December 4, 2013 4:26 PM
    Moderator
  • Have you set the server and organisation name to the correct ones, this line:

    Uri OrganizationUri = new Uri("http://server/name/XRMServices/2011/Organization.svc");

    regards,

    Matt

    • Proposed as answer by Matt_Hirst_UK Wednesday, December 4, 2013 4:50 PM
    Wednesday, December 4, 2013 4:33 PM
  • Thanks for the responses. Matt, yes I do have the correct organization name. When I compile and run the web form in visual studio locally everything runs fine and a contact record is created. The issue I am running into is I can't use C# on the web page. I am missing the link that when the web form is on our website it sends the information to the CRM. I'm not sure how I would go about getting the information sent from the web form to the CRM to create the record. Would it be contained in a solution, web resource, what? There must be some link between that I am missing.

    The goal here is to have a web form that captures some basic person information on our website then creates a contact or lead in the CRM. I currently have a plugin set up that the web form sends an email to me and I track it and it parses the email and creates a lead from that. But I'd like to eliminate that one extra step and just have the lead created as soon as the information is submitted through the web form. I don't understand how the server side code would work to get the information from the web form. Where would the server side code be contained? I would need to trigger the submit button on the form to send the information to the CRM and when that happens execute my C# code to create the contact or lead. Sorry I feel like I'm struggling to explain this properly. I am trying to learn all of this as I go.

    Thanks for the help!


    Wednesday, December 4, 2013 5:09 PM
  • To run the code on the server-side you need to create an ASPX page - is this what you've created ? If so, you will need to deploy the code to a virtual directory within Internet Information Services (IIS) on the web server. The other thing you need to do is configure the application in IIS to use an application pool that has the identity of an AD account that is associated with a CRM user


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Wednesday, December 4, 2013 8:01 PM
    Moderator
  • David, thanks. I will look into this more. I did create the web form as an aspx page. I'm not very familiar with IIS but perhaps some of the guys in our IT department will know. It will give me a starting point, which is what I needed. Thanks again!
    Thursday, December 5, 2013 4:42 PM