Friday, April 22, 2011

Writing Leads from your corporate web site to on-premise Microsoft Dynamics CRM 2011 using XRM, LINQ to CRM, C#, and Web Services

Scenario

I was asked by our sales team to modify their web form so it wouldn’t just send emails but also write a new Lead to our Dynamics CRM 2011 system.

l4fpv030

>

aocqtheo
Email

+

yzjjyw0f

CRM

Solution

LINQ to CRM Generation

I used Hosk’s method to generate and use the LINQ to CRM classes for our specific server.

 

C# Function to Write Lead to CRM
public void AddLead(string subject, string contactFullName,
string companyName, Guid ownerId, LeadSourceWebSiteName leadSourceWebSiteName,
Uri organizationUri, string runAsUser, string runAsPassword, string runAsDomain, string emailAddress,
string phone, string message)
{
try
{
var credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new System.Net.NetworkCredential(runAsUser, runAsPassword, runAsDomain); ;
var serviceProxy = new OrganizationServiceProxy(organizationUri, null, credentials, null);
serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
var oc = new ErServiceContext(serviceProxy);
var newLead = new Lead()
{
Subject = subject,
LastName = contactFullName,
CompanyName = companyName,
OwnerId = new EntityReference(SystemUser.EntityLogicalName, ownerId),
LeadSourceCode = new OptionSetValue(8),
new_LeadSourceWebSiteName = leadSourceWebSiteName.ToString(),
EMailAddress1 = emailAddress,
Telephone1 = phone,
Description = string.Format("Message from web lead on {0}: {1}", DateTime.Now, message)
};
//serviceProxy.Create(newLead);
oc.AddObject(newLead);
oc.SaveChanges();
}
catch (Exception ex)
{
throw ex;
}
}

Calling it from the Web Form
protected void SendLeadToCrm(string subject, string name, string company, string email, string phone, string message)
{
try
{
//Colyer
var ownerId = new Guid("AB56EF33-4913-DE11-9EE8-00156D0A7301"); //You have to get this from the server
var serviceUrl = new Uri("http://server/company/xrmservices/2011/Organization.svc");
var serviceAccountUser = "serviceAccount";
var serviceAccountPassword = "password";
var crm = new CrmServiceClient();
crm.AddLead("Web Lead", name, company, ownerId, LeadSourceWebSiteName.extendedresults,
serviceUrl, serviceAccountUser, serviceAccountPassword, "yourdomain", email, phone, message);
}
catch (Exception ex)
{
//
}
}

Conclusions


This was a fun but long and drawn out project.  I got to learn a lot about CRM 2011 and how it works with C#.  It is way better than CRM 4.0 and the LINQ to CRM stuff is pretty slick.


I hope you find the above useful.  There are a lot of “hidden tricks” in the code above but I think it’ll get you pointed in the right direction if you dissect it.


Happy programming!

3 comments:

Unknown said...

Cool man. I will revisit your post when I get deeper into CRM 2011.

Bruce said...

Hey Veal,cool post. Just wondering what your doing in your wrapper class "ErServiceContext"?I think ive come across a similar problem you would have had..i.e. trying to assign the owner on the lead ( i see you commented out the service.create! ) What i ended up doing was creating the lead and then calling an AssignmentRequest...but it appears you did something in your custom class??:)

Anonymous said...

I've tried the above. However I didn't succeed.

I installed the CRM 2011 SDK. Then when I include the above code I needed to add a coupel fo references to the SDK dlls. Issue: CRM SDK is built on .net 4 and SharePoint 2010 on 3.5. How did you resolve that?