Menu

 

Thoughts on Software Engineering

Accessing Microsoft CRM from .NET Application

Today a customer of mine requested to integrate our ASP.NET based Web application with Microsoft CRM 3.0. Initially I thought this will be a simple task but few hours later I found that this is not straightforward.

Accessing MS CRM 3.0 from .NET

The best way to access MS CRM 3.0 from .NET application is to use its Web services. We first need to add a Web reference to the CRM Web sirvice URL. In my case it was the following:

http://crm.myserver.com/MSCRMServices/2006/CrmService.asmx?WSDL

I hope in CRM 4.0 it will be similar. Assume we are given the name “MSCRM30” for my Web reference.

Listing all Existing Leads

To list all existing leads we can use the following C# code:

// Authenticate the client using Windows authentication (NTLM)
MSCRM30.CrmService crmService = new MSCRM30.CrmService();
crmService.Credentials = new NetworkCredential("crmadmin", "pass@word1");

// List all leads in the CRM
MSCRM30.QueryExpression query = new MSCRM30.QueryExpression();
query.EntityName = MSCRM30.EntityName.lead.ToString();
MSCRM30.ColumnSet columnsToRetrieve = new MSCRM30.ColumnSet();
columnsToRetrieve.Attributes = new string[] {
  "subject", "firstname", "lastname", "emailaddress1" };
query.ColumnSet = columnsToRetrieve;
MSCRM30.BusinessEntityCollection leads =
  crmService.RetrieveMultiple(query);
foreach (MSCRM30.lead lead in leads.BusinessEntities)
{
  Console.WriteLine("{0} by {1} {2} ({3})",
  lead.subject, lead.firstname, lead.lastname, lead.emailaddress1);
}

Creating a Lead

To create a new lead we can use the following C# code:

// Authenticate the client using Windows authentication (NTLM)
MSCRM30.CrmService crmService = new MSCRM30.CrmService();
crmService.Credentials = new NetworkCredential("crmadmin", "pass@word1");

// Create new lead in the CRM
MSCRM30.lead newLead = new MSCRM30.lead();
newLead.firstname = "Pesho";
newLead.lastname = "Ivanov";
newLead.companyname = "Mente Soft";
newLead.subject = "I am created by a web service!";
newLead.emailaddress1 = "pesho.ivanov@mente.com";
crmService.Create(newLead);
Previews (4,011), Views (287), Comments (0)

RSS feed for comments on this post. TrackBack URL

LEAVE A COMMENT