Wednesday, February 19, 2025

Microsoft CRM Customization Programming Email Activity Attachment

Share

Microsoft CRM is now on the scene and it is increasing its market share, due to Microsoft Business Solutions muscles and marketing strategy.

It is tightly integrated with other Microsoft Business Solutions products such as Microsoft Great Plains, Solomon, Navision. Being relatively inexpensive in comparison to competitors, like Siebel, Oracle – Microsoft CRM opens you the door for worldwide operations automation. In this small article we would like to give you, software developer, some hints on Microsoft CRM customization.

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 email attachment – this is the main discussion topic. We’ll use C#.Net.

In Exchange handler/event sink you create Activity of email type in MS CRM and one of the tasks is transfer the attachment(s) from the body of the incoming email to the attachment(s) in the Activity. You can realize it through direct access to Microsoft CRM DB. Let’s see C# code:

1. First we are getting access to the letter via ExOLEDB:

CDO.Message iMessage = new CDO.MessageClass();
CDO.IBodyPart iPrt;

iMessage.DataSource.Open(bstrURLItem, null, ADODB.ConnectModeEnum.adModeRead,

ADODB.RecordCreateOptionsEnum.adFailIfNotExists, ADODB.RecordOpenOptionsEnum.adOpenSource, "", "");

2. Next – we come through the attachment list, get their names and save their bodies into temporary catalogue:

for(int i = 1; i

3. Then we cycle through the cache, containing attachments info and add them to the Activity created:

if (attachments != null) {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp ICollection keys = attachments.Keys;
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp int attCounter = 0;

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp foreach (Guid o in keys) {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp string attName = (string)(attachments[o]);

crmConnector.AddAttachmentToActivity(emailId, attName, (new FileInfo(attName)).Length, attCounter);

File.Delete(attName);
attCounter++;
}
}

4. Here is the method of adding attachment to Activity:

public Guid AddAttachmentToActivity(Guid emailId, string filename, long filesize, int attachmentNumber) {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp try {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp log.Debug("Prepare for Mail Activity Attachment Creating");

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp // BizUser proxy object
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Microsoft.Crm.Platform.Proxy.BizUser bizUser = new Microsoft.Crm.Platform.Proxy.BizUser();

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp ICredentials credentials = new NetworkCredential(sysUserId, sysPassword, sysDomain);

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp bizUser.Url = crmDir + "BizUser.srf";
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp bizUser.Credentials = credentials;
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Microsoft.Crm.Platform.Proxy.CUserAuth userAuth = bizUser.WhoAmI();

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp // CRMActivityAttachment proxy object
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Microsoft.Crm.Platform.Proxy.CRMActivityAttachment activityAttachment = new Microsoft.Crm.Platform.Proxy.CRMActivityAttachment();
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp activityAttachment.Credentials = credentials;
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp activityAttachment.Url = crmDir + "CRMActivityAttachment.srf";

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp // Set up the XML string for the activity attachment
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp string strXml = "<activitymimeattachment>";
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp strXml += "<subject>Activity 1</subject>";
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp strXml += "<attachmentnumber>" + attachmentNumber + "</attachmentnumber>";
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp strXml += "<activityid>" + emailId.ToString("B") + "</activityid>";
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp strXml += "</activitymimeattachment>";

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp // Create the activity attachment
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Guid attachmentId = new Guid(activityAttachment.Create(userAuth, strXml));
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp log.Debug("Create Attachemnt ID: " + attachmentId.ToString("B"));

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp UploadFileToDB(attachmentId, filename, filesize);

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp return attachmentId;
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp }
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp catch (System.Web.Services.Protocols.SoapException e) {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp log.Debug("ErrorMessage: " + e.Message + " " + e.Detail.OuterXml + " Source: " + e.Source);
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp }
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp catch (Exception e) {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp log.Debug(e.Message + "\r\n" + e.StackTrace);
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp }

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp return new Guid();
&nbsp&nbsp&nbsp&nbsp }

5. Main problem, however is attachment body adding to MS CRM database. Main requirement is - attachment must be encoded as BASE64 stream and its length must be specified correctly together with Nine Type and file name of the file it will be knows as an attachment in activity. Let's look at the C# code:

public void UploadFileToDB(Guid attachmentId, string filename, long filesize) {
&nbsp&nbsp&nbsp&nbsp string contentType = "application/octet-stream";

&nbsp&nbsp&nbsp&nbsp try {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Hashtable mimes = LoadMimeDB(Environment.SystemDirectory + "/Albaspectrum/ContentType.txt");

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp if (mimes != null) {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp string tmpContentType = GetMimeType(mimes, filename);

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp if (tmpContentType != null && !tmpContentType.Equals(""))
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp contentType = tmpContentType;
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp }

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp byte[] memoryData = new byte[filesize];

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp BinaryReader reader = new BinaryReader(fs);

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp reader.Read(memoryData, 0, (int)filesize);

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp reader.Close();
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp fs.Close();

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp OleDbCommand command = conn.CreateCommand();

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp command.CommandText = "UPDATE ActivityMimeAttachment SET FileSize = (?), MimeType = (?), FileName = (?), Body = (?) WHERE ActivityMimeAttachmentId = (?)";
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp command.Prepare();
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp command.Parameters.Add(new OleDbParameter("FileSize", filesize));
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp command.Parameters.Add(new OleDbParameter("MimeType", contentType));
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp command.Parameters.Add(new OleDbParameter("FileName", new FileInfo(filename).Name));
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp command.Parameters.Add(new OleDbParameter("Body", Convert.ToBase64String(memoryData, 0, (int)filesize)));
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp command.Parameters.Add(new OleDbParameter("ActivityMimeAttachmentId", attachmentId));

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp log.Debug("Prepare to upload attachemnt " + attachmentId.ToString("B") + " in ActivityMimeAttachment");

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp command.ExecuteNonQuery();

&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp memoryData = null;
&nbsp&nbsp&nbsp&nbsp }
&nbsp&nbsp&nbsp&nbsp catch (Exception e) {
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp log.Debug(e.Message + "\r\n" + e.StackTrace);
&nbsp&nbsp&nbsp&nbsp }
&nbsp&nbsp&nbsp }

6. File ContectType.txt is matching list of the files extensions and their mime-type in the following format:

asc application/pgp-encrypted Armored Encrypted file (PGP)
asd application/astound Autosave file (Word for Windows)
asm PC ASM File
asn application/astound

etc.

Boris Makushkin is senior software developer in Alba Spectrum Technologies US nation-wide Great Plains, Microsoft CRM customization company, based in Chicago and having locations in multiple states and internationally (www.albaspectrum.com ), he is Unix, SQL, C#.Net, Crystal Reports, Microsoft CRM SDK and Exchange Server SDK developer. You can reach Boris: borism@albaspectrum.com

Table of contents

Read more

Local News

Injecting local traffic to a dermatology practice – seo freelance expert.