Microsoft CRM Example

Microsoft CRM Example

This example shows how to create leads in Microsoft CRM 4.0; you would want to remember the IDs of the created records in CRM in a real world application to handle updates appropriately.

We first need to download and unserialize the data, just like in the C# example:

// Helper classes for the JSON unserializer
[DataContract(Name = "lead")]
public class LeadsyncerRecord
	public string uid;
	public string tstamp;
	public string crdate;
	public string deleted;
	public string sheet_uid;
	public string device_uid;
	public LeadsyncerField[] fields;

[DataContract(Name = "field")]
public class LeadsyncerField
	public string title;
	public int type;
	public string value;

string email = "";
string password = "password";

WebRequest request = WebRequest.Create("");
request.Method = "GET";
request.Credentials = new NetworkCredential(email, password);

WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);

string jsonData = reader.ReadToEnd();


// parse the JSON data into our helper classes
string[] lines = jsonData.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

List<LeadsyncerRecord> records = new List<LeadsyncerRecord>();

foreach (string line in lines) 
	MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(line));
	LeadsyncerRecord record = (new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(LeadsyncerRecord))).ReadObject(stream) as LeadsyncerRecord;

Setup CRM connection

This example is by no means complete. You need to add a web reference to your project pointing to the MS CRM service endpoint - if you are unsure how to do this, please have a look into the Microsoft CRM 4.0 SDK available here.

CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = "organizationName";
token.AuthenticationType = 0; // AD authentication

CrmService service = new CrmService();
service.Url = "https://crm.yourdomain.local/MSCRMServices/2007/CrmService.asmx";
service.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
service.CrmAuthenticationTokenValue = token;

Create the leads in CRM

This example assumes that you haven't changed the field titles and that you are using the English example of the form. If you have modified the field titles, you need to change the code accordingly.

We're using a small helper function to move the lead creation code out of the loop:

/// <summary>
/// Returns a Crm lead, if the LeadsyncerRecord contains values for Salutation, Firstname, Lastname and Company.
/// </summary>
public lead MakeCrmLead(LeadsyncerRecord l)
	lead crmLead = new lead();
	if (l.fields.Any(f => f.title.ToLower() == "salutation"))
		crmLead.salutation = l.fields.First(f => f.title.ToLower() == "salutation").value;
		return null;

	if (l.fields.Any(f => f.title.ToLower().Replace(" ", "") == "firstname"))
		crmLead.firstname = l.fields.First(f => f.title.ToLower().Replace(" ", "") == "firstname").value;
		return null;

	if (l.fields.Any(f => f.title.ToLower().Replace(" ", "") == "lastname"))
		crmLead.lastname = l.fields.First(f => f.title.ToLower().Replace(" ", "") == "lastname").value;
		return null;

	if (l.fields.Any(f => f.title.ToLower() == "company"))
		crmLead.companyname = l.fields.First(f => f.title.ToLower() == "company").value;
		return null;

	return crmLead;

Let's iterate over the leads from now and create the leads in CRM:

foreach (LeadsyncerRecord rec in records)
	lead l = MakeCrmLead(rec);
	if (l != null)
		TargetCreateLead tar = new TargetCreateLead();
		tar.Lead = l;

		CreateRequest req = new CreateRequest();
		req.Target = tar;


Remember that this example is not complete; every update of the lead on the iPad creates a new lead in CRM because the time stamp of the record on changes and the record will be retrieved again.

But we hope we could give you a starting point for your own implementation.

You can also download the example as a Visual Studio 2010 project here.

Excel is a registered trademark of Microsoft Corporation. iPad, iPhone, iPod and iOS are registered trademarks of Apple Inc.