Friday 27 November 2015

How to Create Lead in Dynamic CRM from asp.net web form.

create Lead in CRM using CRM Discovery Service


Description:


In this example we explain that how to create Lead in Dynamics CRM from external contact form. Or how to create Lead using asp.net website or web application. Here we explain how to create Lead programmatically in CRM using C#.

Here we create simple webpage leveraging the CRM 2013 IOrganizationService web service. Here we use CRM Discovery service to create Lead in CRM programmatically from asp.net website or web application.

To create lead in CRM through programmatically create web form in your asp.net web application and use the below code.


CreateLead.aspx:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CreateLead.aspx.cs" Inherits="crmdemo1.CreateLead" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <table border="1">

        <tr>

            <td>

                Topic Name :

            </td>

            <td>

                <asp:TextBox ID="txtTopicName" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                First Name :

            </td>

            <td>

                <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                Last Name :

            </td>

            <td>

                <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                Email Address :

            </td>

            <td>

                <asp:TextBox ID="txtEmailAddress" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                Mobile Phone :

            </td>

            <td>

                <asp:TextBox ID="txtMobilePhone" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                Country :

            </td>

            <td>

                <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                State :

            </td>

            <td>

                <asp:TextBox ID="txtState" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                City :

            </td>

            <td>

                <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                Street1 :

            </td>

            <td>

                <asp:TextBox ID="txtStreet1" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                Street2 :

            </td>

            <td>

                <asp:TextBox ID="txtStreet2" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />

            </td>

        </tr>

    </table>

    </form>

</body>

</html>

CreateLead.aspx.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.ServiceModel.Description;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Microsoft.Xrm.Sdk.Client;

using Microsoft.Xrm.Sdk;


namespace crmdemo1

{

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

    {


        protected void btnSubmit_Click(object sender, EventArgs e)

        {

            try

            {

                //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 OrganizationUri = new Uri("https://crm-2015.corpnet.co.in:444/CRM2015/XRMServices/2011/Organization.svc");

                Uri HomeRealmUri = null;

                OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null);

                IOrganizationService service = (IOrganizationService)serviceProxy;

                //Instantiate the contact object and populate the attributes.

                Entity Lead = new Entity("lead");

                Lead["firstname"] = Convert.ToString(txtFirstName.Text);

                Lead["lastname"] = Convert.ToString(txtLastName.Text);

                Lead["subject"] = Convert.ToString(txtTopicName.Text);

                Lead["emailaddress1"] = Convert.ToString(txtEmailAddress.Text);

                Lead["telephone1"] = Convert.ToString(txtMobilePhone.Text);

                Lead["address1_line1"] = Convert.ToString(txtStreet1.Text);

                Lead["address1_line2"] = Convert.ToString(txtStreet2.Text);

                Lead["address1_country"] = Convert.ToString(txtCountry.Text);

                Lead["address1_stateorprovince"] = Convert.ToString(txtState.Text);

                Lead["address1_city"] = Convert.ToString(txtCity.Text);

                Guid newContactId = service.Create(Lead);

                ClearControl();

                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Lead Created Sucessfully.')", true);

            }

            catch (Exception)

            {

                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Error Occured while Creating Lead.')", true);

            }



        }


        private void ClearControl()

        {

            txtFirstName.Text = string.Empty;

            txtLastName.Text = string.Empty;

            txtTopicName.Text = string.Empty;

            txtStreet1.Text = string.Empty;

            txtStreet2.Text = string.Empty;

            txtCountry.Text = string.Empty;

            txtState.Text = string.Empty;

            txtCity.Text = string.Empty;

        }

    }


}

0 comments:

Post a Comment