Microsoft CRM Programming Secrets - Tips for Developers

This article is for advanced Microsoft CRM SDK C# developers. It describes the technique of direct SQL programming, when SDK doesn't have the functionality to do the job.


This article is for advanced Microsoft CRM SDK C# developers. It describes the technique of direct SQL programming, when SDK doesn't have the functionality to do the job.

Introduction. Looks like Microsoft CRM becomes more and more popular, partly because of Microsoft muscles behind it. Now it is targeted to the whole spectrum of horizontal and vertical market clientele. It is tightly integrated with other Microsoft Business Solutions products such as Microsoft Great Plains, Solomon, Navision (the last two in progress). Here we describe the technique of creating closed activity-email using MS CRM SDK and direct SQL programming.

Imaging something like this. You need to handle incoming email before it is committed to MS Exchange database. You need to analyze if incoming email doesn't have GUID in its Subject (GUID will allow MS CRM Exchange Connector to move email to Microsoft CRM and attach it to the Contact, Account or Lead) - then you still need to lookup MS CRM in case if one of the accounts, contacts or leads has email address that matches with sender email address - then you need to create closed activity-email in MS CRM, attached to the object and placed into general queue.

Now the code below is classical MS CRM SDK and it will create activity email:

public Guid CreateEmailActivity(Guid userId, int objectType, Guid objectId, string mailFrom, CRMUser crmUser, string subject, string body)
{
try
{
log.Debug("Prepare for Mail Activity Creating");
// BizUser proxy object
Microsoft.Crm.Platform.Proxy.BizUser bizUser = new Microsoft.Crm.Platform.Proxy.BizUser();
ICredentials credentials =
new NetworkCredential(sysUserId, sysPassword, sysDomain);
bizUser.Url = crmDir + "BizUser.srf";
bizUser.Credentials = credentials;
Microsoft.Crm.Platform.Proxy.CUserAuth userAuth = bizUser.WhoAmI();
// CRMEmail proxy object
Microsoft.Crm.Platform.Proxy.CRMEmail email = new Microsoft.Crm.Platform.Proxy.CRMEmail();
email.Credentials = credentials;
email.Url = crmDir + "CRMEmail.srf";
// Set up the XML string for the activity
string strActivityXml = "<emailactivity>";
strActivityXml += "<messagesubject><![CDATA[" + subject + "]]></messagesubject>";
strActivityXml += "<messagebody><![CDATA[" + body.Replace("\n", "<br>") + "]]></messagebody>";
strActivityXml += "<ownerid type=\"" + Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString() +"\">";
strActivityXml += userId.ToString("B") + "</ownerid>";
strActivityXml += "</emailactivity>";
// Set up the XML string for the activity parties
string strPartiesXml = "<activityparties>";
strPartiesXml += "<activityparty>";
strPartiesXml += "<addressused>" + crmUser.GetEmailAddress() + "</addressused>";
strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString() + "</partyobjecttypecode>";
strPartiesXml += "<partyid>"+ crmUser.GetId().ToString("B") + "</partyid>";
strPartiesXml += "<participationtypemask>";
strPartiesXml += Microsoft.Crm.Platform.Types.ACTIVITY_PARTY_TYPE.ACTIVITY_PARTY_TO_RECIPIENT.ToString();
strPartiesXml += "</participationtypemask>";
strPartiesXml += "</activityparty>";
strPartiesXml += "<activityparty>";
strPartiesXml += "<addressused>" + mailFrom + "</addressused>";
if (objectType == Microsoft.Crm.Platform.Types.ObjectType.otAccount)
{
strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otAccount.ToString() + "</partyobjecttypecode>";
}
else if (objectType == Microsoft.Crm.Platform.Types.ObjectType.otContact)
{
strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otContact.ToString() + "</partyobjecttypecode>";
}
else if (objectType == Microsoft.Crm.Platform.Types.ObjectType.otLead)
{
strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otLead.ToString() + "</partyobjecttypecode>";
}
strPartiesXml += "<partyid>"+ objectId.ToString("B") + "</partyid>";
strPartiesXml += "<participationtypemask>";
strPartiesXml += Microsoft.Crm.Platform.Types.ACTIVITY_PARTY_TYPE.ACTIVITY_PARTY_SENDER.ToString();
strPartiesXml += "</participationtypemask>";
strPartiesXml += "</activityparty>";
strPartiesXml += "</activityparties>";
log.Debug(strPartiesXml);
// Create the e-mail object
Guid emailId = new Guid(email.Create(userAuth, strActivityXml, strPartiesXml));
return emailId;
}
catch (System.Web.Services.Protocols.SoapException e)
{
log.Debug("ErrorMessage: " + e.Message + " " + e.Detail.OuterXml + " Source: " + e.Source);
}
catch (Exception e)
{
log.Debug(e.Message + "\r\n" + e.StackTrace);
}
return new Guid();
}

Now I would like to share the trick with you - there is no method to make this activity closed in MS CRM SDK 1.2 (if somebody knows the one - I owe you small pocket aquarium - smile!). Obviously Microsoft doesn't support if you do direct SQL programming bypassing SDK. However I would say this is not direct objects creation - this is rather flags correction. So here is what we have - this procedure will do the job and make activity closed:

public void UpdateActivityCodes(Guid emailId)
{
try
{
OleDbCommand command = conn.CreateCommand();
command.CommandText = "UPDATE ActivityBase SET DirectionCode = (?), StateCode = (?), PriorityCode = (?) WHERE ActivityId = (?)";
command.Prepare();
command.Parameters.Add(
new OleDbParameter("DirectionCode", Microsoft.Crm.Platform.Types.EVENT_DIRECTION.ED_INCOMING));
command.Parameters.Add(
new OleDbParameter("StateCode", Microsoft.Crm.Platform.Types.ACTIVITY_STATE.ACTS_CLOSED));
command.Parameters.Add(
new OleDbParameter("PriorityCode", Microsoft.Crm.Platform.Types.PRIORITY_CODE.PC_MEDIUM));
command.Parameters.Add(
new OleDbParameter("ActivityId", emailId));
log.Debug("Prepare to update activity code " + emailId.ToString("B") + " in ActivityBase");
command.ExecuteNonQuery();
}
catch(Exception e)
{
log.Debug(e.Message + "\r\n" + e.StackTrace);
}
}

Happy customizing!