Create record using Early Bound and LINQ in CRM

Task: Create Contact record

Solution: Follow the below steps,

Step 1: Open Visual Studio and Create Console Application Project.

Step 2: First we have to establish connection to CRM. Please follow my earlier post Connect to CRM Online or On-premise using C# SDK.

Step 3: Generate Early bound class using Crmsvcutil.exe. Please follow my earlier post Generate Early bound Class using CrmSvcUtil.Exe in CRM.

Step 4: Add EarlyBound.cs to the project.

Step 5: Include the namespace using EarlyBound.cs.

Step 6: Below method will create Contact record,

createContactRecord("Arun", "Potti", "9876543210", "arunpotti@xyzorg.com");

public static void createContactRecord(string firstName, string lastName, string mobileNumber, string email)
{
Contact c = new Contact()
{
FirstName = firstName,
LastName =lastName,
MobilePhone = mobileNumber,
EMailAddress1 = email
};
Guid contactGuid = _service.Create(c);
if (contactGuid != Guid.Empty)
Console.WriteLine("Contact record created");
}

Step 7: Final Code will be like below,

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using EarlyBound;
 
namespace LinqToCRM
{
class Program
{
static IOrganizationService _service;
static void Main(string[] args)
{
ConnectToMSCRM("arunpotti@MyOrg.onmicrosoft.com", "Password", "https://MyOrg.api.crm5.dynamics.com/XRMServices/2011/Organization.svc");
Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;

if (userid == Guid.Empty) return;
createContactRecord("Arun", "Potti", "9876543210", "arunpotti@xyzorg.com");
}

public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
{
try
{
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = UserName;
credentials.UserName.Password = Password;
Uri serviceUri = new Uri(SoapOrgServiceUri);
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
proxy.EnableProxyTypes();
_service = (IOrganizationService)proxy;
}
catch (Exception ex)
{
Console.WriteLine("Error while connecting to CRM " + ex.Message);
Console.ReadKey();
}
}

public static void createContactRecord(string firstName, string lastName, string mobileNumber, string email)
{
Contact c = new Contact()
{
FirstName = firstName,
LastName = lastName,
MobilePhone = mobileNumber,
EMailAddress1 = email
};

Guid contactGuid = _service.Create(c);
if (contactGuid != Guid.Empty)
{
Console.WriteLine("Contact record created");
Console.ReadKey();
}
}
}
}

Step 8: Execute the project for the below output,

LinqToCrm Create Output

Please provide your valuable comments on this article.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s