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
{
	[DataMember()]
	public string uid;
	[DataMember()]
	public string tstamp;
	[DataMember()]
	public string crdate;
	[DataMember()]
	public string deleted;
	[DataMember()]
	public string sheet_uid;
	[DataMember()]
	public string device_uid;
	[DataMember()]
	public LeadsyncerField[] fields;
}

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


string email = "your@emailaddress.com";
string password = "password";

WebRequest request = WebRequest.Create("https://leadsyncer.com/restapi/lead/byTimestamp/0");
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();

reader.Close();
dataStream.Close();
response.Close();

// 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;
	records.Add(record);
}


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 leadsyncer.com 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;
	else
		return null;

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

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

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

	return crmLead;
}

Let's iterate over the leads from leadsyncer.com 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;

		service.Execute(req);
	}
}

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 leadsyncer.com 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.

ExportLeadsToCRM.zip


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