Add dates programmatically to CRM

Does that word actually exist in English? Programmatically?

Anyway. When you're working on a program which uses the SDK to add or modify data in CRM, then you will find out that there is not much documentation about how to use this data. And when you enter wrong data to CRM, then the error message doesn't help you much either: 0x80040216 An unexpected error occurred.

Well. Here are 2 code examples which should get you started. Make sure that you do not pass in time data if it is a date only field.
Example 1 (Add data including time):

contact contact = new contact();
contact.new_dateandtime = new CrmDateTime();
contact.new_dateandtime.Value = "5/27/2005T12:00:00";

Example 2 (Add data excluding time):

contact contact = new contact();
contact.new_dateandtime = new CrmDateTime();
contact.new_dateandtime.Value = "5/27/2005";


daniel said...

Hi Ronald

I am getting this same error, ie
An unexpected error occurred.
I have been trying to debug it for hours and it is driving me nuts, s o if you could help I would really appreciate it. I am posting some xml to an aspx page.

Here is the code that deals with it, in the c# file:
if(campaignID == "")
//get campaign from config
campaignID = ConfigurationSettings.AppSettings["defaultCampaignID"];
sMessage = sMessage + " Now we create a campaign response...campaignID is " + campaignID;
sMessage = sMessage + ", subject = " + subject + " clType = " + clType + " uploadID = " + uploadID + " ";
campaignresponse cr = new campaignresponse();
cr.responsecode = new Picklist();
cr.responsecode.Value = 1;
cr.subject = subject;
CrmDateTime crActualEnd = new CrmDateTime();
crActualEnd.Value = DateTime.Now.ToString();
cr.actualend = crActualEnd;

Lookup lookup = new Lookup();
lookup.Value = new Guid(campaignID);
lookup.type = EntityName.campaign.ToString();

cr.regardingobjectid = lookup;

// Create the party
activityparty party = new activityparty();

// Set the properties of Activityparty.
party.partyid = new Lookup();
if(clType == "contact")
party.partyid.type =;
party.partyid.type = EntityName.lead.ToString();
party.partyid.Value = new Guid(uploadID);

cr.customer = new activityparty[]{party};
sMessage = sMessage + " the line before we create the campaign response...";
Guid crID = new Guid();
crID = service.Create(cr);

catch(System.Web.Services.Protocols.SoapException ex){
sMessage = sMessage + "Message : " + ex.Message + "InnerException : " + ex.InnerException + "Detail : " + ex.Detail + "Code : " + ex.Code + "InnerXml : " + ex.Detail.InnerXml + "InnerText : " + ex.Detail.InnerText;
sMessage = sMessage + "exception to string : " + ex.ToString();

And here is the output:

Now we create a campaign response...campaignID is 09A01757-C4A9-DB11-BDDD-001321B40659, subject = test note clType = lead uploadID = D10A255C-8358-4566-ABB3-7F854E996FCE the line before we create the campaign response...Message : Server was unable to process request.InnerException : Detail : System.Xml.XmlElementCode : : InnerText :
An unexpected error occurred.
exception to string : System.Web.Services.Protocols.SoapException: Server was unable to process request.
at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at CRMService.holocentricCRMService.CrmService.Create(BusinessEntity entity)
at strategicMixCrm.notes.createCampaignResponse2(String uploadID, String subject, String clType, String campaignID)

Now here is the kicker: it works fine on my dev machine, doesn't work on the production server. The code is the same. The .NET version is the same (1.1.4322). They are posting to the same CRM Server. My dev machine is WinXP, the prod Server is Win2003

I have googled this extensively, can't find anything.

Is there a way of monitoring the calls on the CRM Server side, to see where it is going wrong?

Thanks in advance

daniel said...

Hey I worked it out. It was down to regional settings: my machine had dates formatted one way, and the prod server had them going the other way(I live in Australia, where we format dates this way: 25/04/2007 ). So the CrmDateTime didn't get a recognised date on some days of the month. Which made it real tricky to spot, especially with the non-informative error message!


Ronald Lemmen said...

Great that you fixed it already and thanks for the update Daniel!

Anonymous said...

hi, y'all!

you can often find more information on "unexpected errors" in the trace-file (C:\Program Files\Microsoft Dynamics CRM Server\Trace), if you have tracing enabled; i.e. something like "Unable to cast object of type 'Microsoft.Crm.Sdk.StringProperty' to type 'Microsoft.Crm.Sdk.OwnerProperty'." which can sure help alot.

happy coding,

Anonymous said...

the important thing is the culturel info of your crm platform, not your application's deployed platform. for example if your crm platform is in regional settings of US , you cannot send a datetime as format of arabic. you should use ISO formats. for example
DateTime x = DateTime.Now;
string x_iso = x.ToString("s");
so x_iso will be in format of "2010-10-01T00:00:00" ...

Ronald Lemmen said...

Thanks for this valuable addition!

Anonymous said...

I had this error trying to put a string property into a lookup property.