|
|
|
|
|
|
|
Page Views :
|
2428
|
|
Downloads :
|
0
|
|
Rating :
|
Rate it
|
|
Level :
|
Intermediate
|
|
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 customizBorisMakushkin.incation.
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, DODB.ConnectModeEnum.adModeRead, ADODB.RecordCreateOptionsEnum.adFailIfNotExists, DODB.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 <= aNum; i++) { string fName = iMessage.Attachments[i].FileName; string eName = fName.Substring(fName.Length-3, 3).ToUpper(); log.Debug("Attachment: " + fName); iPrt = iMessage.Attachments[i]; string attName = Path.GetTempPath() + fName; iPrt.SaveToFile(attName); attachments.Add(Guid.NewGuid(), attName); iPrt = null; }
3. Then we cycle through the cache, containing attachments info and add them to the Activity created:
if (attachments != null) { ICollection keys = attachments.Keys; int attCounter = 0; foreach (Guid o in keys) { 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) { try { log.Debug("Prepare for Mail Activity Attachment 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(); // CRMActivityAttachment proxy object Microsoft.Crm.Platform.Proxy.CRMActivityAttachment activityAttachment = new Microsoft.Crm.Platform.Proxy.CRMActivityAttachment(); activityAttachment.Credentials = credentials; activityAttachment.Url = crmDir + "CRMActivityAttachment.srf"; // Set up the XML string for the activity attachment string strXml = "<activitymimeattachment>"; strXml += "<subject>Activity 1</subject>"; strXml += "<attachmentnumber>" + attachmentNumber + "</attachmentnumber>"; strXml += "<activityid>" + emailId.ToString("B") + "</activityid>"; strXml += "</activitymimeattachment>"; // Create the activity attachment Guid attachmentId = new Guid(activityAttachment.Create(userAuth, strXml)); log.Debug("Create Attachemnt ID: " + attachmentId.ToString("B")); UploadFileToDB(attachmentId, filename, filesize); return attachmentId; } 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(); }
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) { string contentType = "application/octet-stream"; try { Hashtable mimes = LoadMimeDB(Environment.SystemDirectory + /Albaspectrum/ContentType.txt"); if (mimes != null) { string tmpContentType = GetMimeType(mimes, filename); if (tmpContentType != null && !tmpContentType.Equals("")) contentType = tmpContentType; } byte[] memoryData = new byte[filesize]; FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(fs); reader.Read(memoryData, 0, (int)filesize); reader.Close(); fs.Close(); OleDbCommand command = conn.CreateCommand(); command.CommandText = "UPDATE ActivityMimeAttachment SET FileSize = (?), MimeType = (?), FileName = (?), Body = (?) WHERE ActivityMimeAttachmentId = (?)"; command.Prepare(); command.Parameters.Add(new OleDbParameter("FileSize", filesize)); command.Parameters.Add(new OleDbParameter("MimeType", contentType)); command.Parameters.Add(new OleDbParameter("FileName", new FileInfo(filename).Name)); command.Parameters.Add(new OleDbParameter("Body", Convert.ToBase64String(memoryData, 0, (int)filesize))); command.Parameters.Add(new OleDbParameter("ActivityMimeAttachmentId", attachmentId)); log.Debug("Prepare to upload attachemnt " + attachmentId.ToString("B") + " in ActivityMimeAttachment"); command.ExecuteNonQuery(); memoryData = null; } catch (Exception e) { log.Debug(e.Message + "\r\n" + e.StackTrace); } }
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
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
BorisM
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. He is Unix, SQL, C#.Net, Crystal Reports, Microsoft CRM SDK and Exchange Server SDK developer.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|