Description:
In this example we explain that how to create Contact
or Account in CRM using asp.net web form or from asp.net website. Or how to
create Contact using CRM Discovery web services to generate contact or Account
through programmatically. We already explain that how to create lead from
asp.net web form in previous example. Here we explain how to create contact or
Account programmatically in CRM using C#.
To create Contact in CRM through programmatically create
web form in your asp.net web application and use the below code.
CreateContact.aspx:
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeBehind="CreateContact.aspx.cs"
Inherits="crmdemo1.CreateContact"
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table border="1">
<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>
Phone Number :
</td>
<td>
<asp:TextBox ID="txtPhoneNumber"
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>
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 CreateContact
: 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://demokiritcrm-2015.corpnet.co.in:444/CRM2015/XRMServices/2011/Organization.svc");
Uri HomeRealmUri = null;
//OrganizationServiceProxy serviceProxy;
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"] = Convert.ToString(txtFirstName.Text);
contact["lastname"] = Convert.ToString(txtLastName.Text);
contact["emailaddress1"]
= Convert.ToString(txtEmailAddress.Text);
contact["telephone1"] = Convert.ToString(txtPhoneNumber.Text);
Guid newContactId =
service.Create(contact);
ClientScript.RegisterStartupScript(this.GetType(),
"alert", "alert('Contact
Created Sucessfully.')", true);
ClearControl();
}
catch (Exception)
{
ClientScript.RegisterStartupScript(this.GetType(),
"alert", "alert('Error
Occured while Creating Contact.')", true);
}
}
private void
ClearControl()
{
txtFirstName.Text = string.Empty;
txtLastName.Text = string.Empty;
txtEmailAddress.Text = string.Empty;
txtPhoneNumber.Text = string.Empty;
}
}
}
0 comments:
Post a Comment