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!

Wednesday, April 06, 2011

Query String Parameters as a Table for Debugging in Fiddler

cwcyjycs

I frequently run into this problem of wanting to know what’s in the query string for debugging web apps.  Here’s where to get it in Fiddler.  Pretty handy!!!

Inspectors > WebForms > QueryString