Microsoft CRM Customization – programming Closed Email Activity
by Boris Makushkin
Microsoft CRM is CRM answer from Microsoft and attempt to get market share from Siebel, Oracle and others traditional Client Relationship Management System vendors. Microsoft CRM uses all the spectrum of Microsoft recent technologies: .Net, MS Exchange, MS Outlook, MS SQL Server, Replication, Indexing, Active Directory, Windows 2000/2003 security model, C#, VB.Net, HTML, XML Web Service, XLTP, Javascript to name a few.
Today's topic is Activity of email type programming - you usually deal with these customizations when you improve Microsoft Exchange CRM connector. How do you create closed activity - this is the main discussion topic. We’ll use C#.Net coding
One of the roles of our Exchange Event Handler/Sink is creation MS CRM Closed Activity in handling incoming and outgoing email messages. The interaction with Microsoft CRM uses two approached – using MS CRM SDK (handling inbound and outbound XML messages) and via direct access to MS CRM Database. Let’s first look at the Closed Activity creation algorithm:
if ((crmAccount = crmConnector.GetAccount(mailboxFrom)) != null) {
}
else if ((crmContact = crmConnector.GetContact(mailboxFrom)) != null) {
}
else if ((crmLead = crmConnector.GetLead(mailboxFrom)) != null) {
}
crmUser = crmConnector.GetUser(crmAccount.GetOwnerId());
emailId = crmConnector.CreateEmailActivity(
crmUser.GetId(),
Microsoft.Crm.Platform.Types.ObjectType.otAccount, crmAccount.GetId(),
Microsoft.Crm.Platform.Types.ObjectType.otSystemUser, crmUser.GetId(),
crmAccount.GetEmailAddress(), crmUser.GetEmailAddress(), sSubject, sBody);
public Guid CreateEmailActivity(Guid userId, int fromObjectType, Guid fromObjectId, int toObjectType, Guid toObjectId, string mailFrom, string mailTo, 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>" + mailTo + "</addressused>";
if (toObjectType == Microsoft.Crm.Platform.Types.ObjectType.otSystemUser) {
strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString() + "</partyobjecttypecode>";
}
else if (toObjectType == Microsoft.Crm.Platform.Types.ObjectType.otAccount) {
strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otAccount.ToString() + "</partyobjecttypecode>";
}
else if (toObjectType == Microsoft.Crm.Platform.Types.ObjectType.otContact) {
strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otContact.ToString() + "</partyobjecttypecode>";
}
else if (toObjectType == Microsoft.Crm.Platform.Types.ObjectType.otLead) {
strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otLead.ToString() + "</partyobjecttypecode>";
}
strPartiesXml += "<partyid>"+ toObjectId.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 (fromObjectType == Microsoft.Crm.Platform.Types.ObjectType.otSystemUser) {
strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString() + "</partyobjecttypecode>";
}
else if (fromObjectType == Microsoft.Crm.Platform.Types.ObjectType.otAccount) {
strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otAccount.ToString() + "</partyobjecttypecode>";
}
else if (fromObjectType == Microsoft.Crm.Platform.Types.ObjectType.otContact) {
strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otContact.ToString() + "</partyobjecttypecode>";
}
else if (fromObjectType == Microsoft.Crm.Platform.Types.ObjectType.otLead) {
strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otLead.ToString() + "</partyobjecttypecode>";
}
strPartiesXml += "<partyid>"+ fromObjectId.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();
}
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);
}
}
public void UpdateActivityQueueCodes(Guid emailId, Guid queueId) {
try {
OleDbCommand command = conn.CreateCommand();
command.CommandText = "UPDATE QueueItemBase SET Priority = (?), State = (?), QueueId = (?) WHERE ObjectId = (?)";
command.Prepare();
command.Parameters.Add(new OleDbParameter("Priority", Microsoft.Crm.Platform.Types.PRIORITY_CODE.PC_MEDIUM));
command.Parameters.Add(new OleDbParameter("State", Microsoft.Crm.Platform.Types.ACTIVITY_STATE.ACTS_CLOSED));
command.Parameters.Add(new OleDbParameter("QueueId", queueId));
command.Parameters.Add(new OleDbParameter("ObjectId", emailId));
log.Debug("Prepare to update activity queue code " + emailId.ToString("B") + " in QueueItemBase");
command.ExecuteNonQuery();
}
catch(Exception e) {
log.Debug(e.Message + "\r\n" + e.StackTrace);
}
}
Happy customizing, implementing and modifying! If you want us to do the job - give us a call 1-866-528-0577! help@albaspectrum.com
Boris Makushkin is Lead Software Developer in Alba Spectrum Technologies – USA nationwide Microsoft CRM, Microsoft Great Plains customization company, based in Chicago, Boston, San Francisco, San Diego, Los Angeles, Houston, Dallas, Atlanta, Miami, Montreal, Toronto, Vancouver, Madrid, Moscow, Europe and internationally (www.albaspectrum.com), he is Microsoft CRM SDK, C#, VB.Net, SQL, Oracle, Unix developer. Boris can be reached: 1-866-528-0577 or borism@albaspectrum.com
Alba Spectrum Technologies