Saturday, October 5, 2024

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.

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.

How to create MS Exchange handler is outside of the scope, please see this article:

http://www.albaspectrum.com/Customizations_Whitepapers/Dexterity_SQL_VBA_Crystal/ExchangeHandlerExample.htm

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 {
&nbsp&nbsp log.Debug("Prepare for Mail Activity Creating");
&nbsp&nbsp // BizUser proxy object
&nbsp&nbsp Microsoft.Crm.Platform.Proxy.BizUser bizUser = new Microsoft.Crm.Platform.Proxy.BizUser();
&nbsp&nbsp ICredentials credentials = new NetworkCredential(sysUserId, sysPassword, sysDomain);
&nbsp&nbsp bizUser.Url = crmDir + "BizUser.srf";
&nbsp&nbsp bizUser.Credentials = credentials;
&nbsp&nbsp Microsoft.Crm.Platform.Proxy.CUserAuth userAuth = bizUser.WhoAmI();
&nbsp&nbsp // CRMEmail proxy object
&nbsp&nbsp Microsoft.Crm.Platform.Proxy.CRMEmail email = new Microsoft.Crm.Platform.Proxy.CRMEmail();
&nbsp&nbsp email.Credentials = credentials;
&nbsp&nbsp email.Url = crmDir + "CRMEmail.srf";
&nbsp&nbsp // Set up the XML string for the activity
&nbsp&nbsp string strActivityXml = "<emailactivity>";
&nbsp&nbsp strActivityXml += "<messagesubject><![CDATA[" + subject + "]]></messagesubject>";
&nbsp&nbsp strActivityXml += "<messagebody><![CDATA[" + body.Replace("\n", "<br>") + "]]></messagebody>";
&nbsp&nbsp strActivityXml += "<ownerid type=\"" + Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString() +"\">";
&nbsp&nbsp strActivityXml += userId.ToString("B") + "</ownerid>";
&nbsp&nbsp strActivityXml += "</emailactivity>";
&nbsp&nbsp // Set up the XML string for the activity parties
&nbsp&nbsp string strPartiesXml = "<activityparties>";
&nbsp&nbsp strPartiesXml += "<activityparty>";
&nbsp&nbsp strPartiesXml += "<addressused>" + crmUser.GetEmailAddress() + "</addressused>";
&nbsp&nbsp strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otSystemUser.ToString() + "</partyobjecttypecode>";
&nbsp&nbsp strPartiesXml += "<partyid>"+ crmUser.GetId().ToString("B") + "</partyid>";
&nbsp&nbsp strPartiesXml += "<participationtypemask>";
&nbsp&nbsp strPartiesXml += Microsoft.Crm.Platform.Types.ACTIVITY_PARTY_TYPE.ACTIVITY_PARTY_TO_RECIPIENT.ToString();
&nbsp&nbsp strPartiesXml += "</participationtypemask>";
&nbsp&nbsp strPartiesXml += "</activityparty>";
&nbsp&nbsp strPartiesXml += "<activityparty>";
&nbsp&nbsp strPartiesXml += "<addressused>" + mailFrom + "</addressused>";
&nbsp&nbsp if (objectType == Microsoft.Crm.Platform.Types.ObjectType.otAccount) {
&nbsp&nbsp strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otAccount.ToString() + "</partyobjecttypecode>";
&nbsp&nbsp }
&nbsp&nbsp else if (objectType == Microsoft.Crm.Platform.Types.ObjectType.otContact) {
&nbsp&nbsp strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otContact.ToString() + "</partyobjecttypecode>";
&nbsp&nbsp }
&nbsp&nbsp else if (objectType == Microsoft.Crm.Platform.Types.ObjectType.otLead) {
&nbsp&nbsp strPartiesXml += "<partyobjecttypecode>" + Microsoft.Crm.Platform.Types.ObjectType.otLead.ToString() + "</partyobjecttypecode>";
&nbsp&nbsp }
&nbsp&nbsp strPartiesXml += "<partyid>"+ objectId.ToString("B") + "</partyid>";
&nbsp&nbsp strPartiesXml += "<participationtypemask>";
&nbsp&nbsp strPartiesXml += Microsoft.Crm.Platform.Types.ACTIVITY_PARTY_TYPE.ACTIVITY_PARTY_SENDER.ToString();
&nbsp&nbsp strPartiesXml += "</participationtypemask>";
&nbsp&nbsp strPartiesXml += "</activityparty>";
&nbsp&nbsp strPartiesXml += "</activityparties>";
&nbsp&nbsp log.Debug(strPartiesXml);
&nbsp&nbsp // Create the e-mail object
&nbsp&nbsp Guid emailId = new Guid(email.Create(userAuth, strActivityXml, strPartiesXml));
&nbsp&nbsp 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 {
&nbsp&nbsp OleDbCommand command = conn.CreateCommand();
&nbsp&nbsp command.CommandText = "UPDATE ActivityBase SET DirectionCode = (?), StateCode = (?), PriorityCode = (?) WHERE ActivityId = (?)";
&nbsp&nbsp command.Prepare();
&nbsp&nbsp command.Parameters.Add(new OleDbParameter("DirectionCode", Microsoft.Crm.Platform.Types.EVENT_DIRECTION.ED_INCOMING));
&nbsp&nbsp command.Parameters.Add(new OleDbParameter("StateCode", Microsoft.Crm.Platform.Types.ACTIVITY_STATE.ACTS_CLOSED));
&nbsp&nbsp command.Parameters.Add(new OleDbParameter("PriorityCode", Microsoft.Crm.Platform.Types.PRIORITY_CODE.PC_MEDIUM));
&nbsp&nbsp command.Parameters.Add(new OleDbParameter("ActivityId", emailId));
&nbsp&nbsp log.Debug("Prepare to update activity code " + emailId.ToString("B") + " in ActivityBase");
&nbsp&nbsp command.ExecuteNonQuery();
&nbsp&nbsp }
&nbsp&nbsp catch(Exception e) {
&nbsp&nbsp log.Debug(e.Message + "\r\n" + e.StackTrace);
&nbsp&nbsp }
}

Andrew Karasev is Chief Technology Officer in Alba Spectrum Technologies USA nationwide Microsoft CRM, Microsoft Great Plains customization company, based in Chicago, California, Texas, New York, Georgia and Florida and having locations in multiple states and internationally (www.albaspectrum.com), he is Dexterity, SQL, C#.Net, Crystal Reports and Microsoft CRM SDK developer.

Related Articles

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles