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:
1. First we need to understand the entity we need to create activity for: Account, Lead or Contact. The selection should use specific criteria – in our case this is email address:
if ((crmAccount = crmConnector.GetAccount(mailboxFrom)) != null) {
}
else if ((crmContact = crmConnector.GetContact(mailboxFrom)) != null) {
}
else if ((crmLead = crmConnector.GetLead(mailboxFrom)) != null) {
}
2. Then we have to get GUID of MS CRM user, who owns this entity, C# code like this:
crmUser = crmConnector.GetUser(crmAccount.GetOwnerId());
3. Next step is closed Activity creation:
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);
4. The method to create closed activity:
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 = กก;
strActivityXml += กก;
strActivityXml += กก) + กก;
strActivityXml += กก;
strActivityXml += userId.ToString(กBก) + กก;
strActivityXml += กก;
// Set up the XML string for the activity parties
string strPartiesXml = กก;
strPartiesXml += กก;
strPartiesXml += กก + mailTo + กก;
if (toObjectType == Microsoft.Crm.Platform.Types.ObjectType.otSystemUser) {
strPartiesXml += กก + Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString() + กก;
}
else if (toObjectType == Microsoft.Crm.Platform.Types.ObjectType.otAccount) {
strPartiesXml += กก + Microsoft.Crm.Platform.Types.ObjectType.otAccount.ToString() + กก;
}
else if (toObjectType == Microsoft.Crm.Platform.Types.ObjectType.otContact) {
strPartiesXml += กก + Microsoft.Crm.Platform.Types.ObjectType.otContact.ToString() + กก;
}
else if (toObjectType == Microsoft.Crm.Platform.Types.ObjectType.otLead) {
strPartiesXml += กก + Microsoft.Crm.Platform.Types.ObjectType.otLead.ToString() + กก;
}
strPartiesXml += กก+ toObjectId.ToString(กBก) + กก;
strPartiesXml += กก;
strPartiesXml += Microsoft.Crm.Platform.Types.ACTIVITY_PARTY_TYPE.ACTIVITY_PARTY_TO_RECIPIENT.ToString();
strPartiesXml += กก;
strPartiesXml += กก;
strPartiesXml += กก;
strPartiesXml += กก + mailFrom + กก;
if (fromObjectType == Microsoft.Crm.Platform.Types.ObjectType.otSystemUser) {
strPartiesXml += กก + Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString() + กก;
}
else if (fromObjectType == Microsoft.Crm.Platform.Types.ObjectType.otAccount) {
strPartiesXml += กก + Microsoft.Crm.Platform.Types.ObjectType.otAccount.ToString() + กก;
}
else if (fromObjectType == Microsoft.Crm.Platform.Types.ObjectType.otContact) {
strPartiesXml += กก + Microsoft.Crm.Platform.Types.ObjectType.otContact.ToString() + กก;
}
else if (fromObjectType == Microsoft.Crm.Platform.Types.ObjectType.otLead) {
strPartiesXml += กก + Microsoft.Crm.Platform.Types.ObjectType.otLead.ToString() + กก;
}
strPartiesXml += กก+ fromObjectId.ToString(กBก) + กก;
strPartiesXml += กก;
strPartiesXml += Microsoft.Crm.Platform.Types.ACTIVITY_PARTY_TYPE.ACTIVITY_PARTY_SENDER.ToString();
strPartiesXml += กก;
strPartiesXml += กก;
strPartiesXml += กก;
log.Debug(strPartiesXml);
// Create the email 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 + ก
ก + e.StackTrace);
}
return new Guid();
}
5. To make the activity just created be shown correctly you need to setup it’s flags according to MS CRM standards:
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 + ก
ก + 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 + ก
ก + e.StackTrace);
}
}
Happy customizing, implementing and modifying! If you want us to do the job give us a call 18665280577! [email protected]
About The Author
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: 18665280577 or [email protected].
[email protected]
This article was posted on October 25, 2004
by Boris Makushkin