Send Emails and Attachments in CRM 2011 using the SDK

by Steven Jennings 7. July 2011 18:18

The other day I had a request from a client to create a customer ASPX webpage that sends emails through CRM along with the attached Excel file. Here is an example of how this can be completed. Start with the Send Email sample code from Microsoft website located here and added the functionality of attaching files to the email.

 

Adding an attached file to the Email

 

Step 1) First you need to attach a file to the ActivityMimeAttachment as base64

 

Code:

public ActivityMimeAttachment AttachFileToEmail(String fileName)

{

// Open a file and read its contents into a byte array.

var fileLocation    = ConfigurationManager.AppSettings["ExcelPath"] + fileName;

var stream          = File.OpenRead(fileLocation);

var byteData        = new byte[stream.Length];

 

stream.Read(byteData, 0, byteData.Length);

 

// Encode the data using base64.

var encodedData = Convert.ToBase64String(byteData);

var extension = Path.GetExtension(fileLocation).ToLower();

 

String mimeType = null;

 

switch (extension)

{

case ".xls":

mimeType = "application/vnd.ms-excel";

break;

case ".xlsx":

mimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

break;

}

 

var sampleAttachment = new ActivityMimeAttachment

{

ObjectId        = new EntityReference(Email.EntityLogicalName, _emailId),

ObjectTypeCode  = Email.EntityLogicalName,

FileName        = fileName,

MimeType        = mimeType,

Body            = encodedData,

};

 

return sampleAttachment;

}

 

Step 2) Add the EncodedDate to the Create an e-mail message in the Sample Code

Code:

// Create an e-mail message.

Email email = new Email

{

To = new ActivityParty[] { toParty },

From = new ActivityParty[] { fromParty },

Subject = "SDK Sample e-mail",

Description = "CRM SDK Sample Body

DirectionCode = true,

email_activity_mime_attachment = new[] { AttachFileToEmail("FileName") }

};

 

The Sample of the Closed Activity Record in CRM

 image

You can download my SendEmail sample code here.

 

If you have any questions or need help please contact me @ steven.jennings[at]aresgrp.com

Tags:

CRM 2011

New Features in CRM 2011 - Last View Record Count and lines selected

by Steven Jennings 6. July 2011 01:13

One of new features added in Microsoft CRM 2011 was the record count and line selected. This was a feature requested by the users and partners of CRM version 4.0. This new feature will allow the users to see how many total records each Entity’s contacts.

Old Way in CRM version 4.0

clip_image001

New way in CRM 2011

clip_image002

There is a limitation to this new feature. You can only see a MAX count of 5000 records.

You might be asking yourself WHY??? This is because Microsoft Dynamics CRM 2011 is using FetchXML to return the total count and by default FetchXML returns a maximum of 5000 records.

clip_image003

There is an easy work around on getting the total record count for an Entity and that is to create a personal Chart to see the total count of the records.

· In the Active Accounts view, create a new chart from the Chart tab. In the Chart Designer, select appropriate Series (Example: Status) and Category values (Example: Status). Then save the chart.

clip_image004

Tags:

CRM 2011

Page List

About the author

I work for a consulting firm in Dublin Ohio called Affiliated Resource Group. For the last five years I have been spearheading our Microsoft Dynamics CRM practice. I have a deep appreciation for the Microsoft CRM platform and I am very excited about it. You might even describe me as a Microsoft CRM Advocate. I have many battle scars from my experience with the product and I’m constantly being asked questions about CRM and how-to-do something in it. Hence, this BLOG is to help disseminate that knowledge and information to everyone. As of last year I was posting links to many other blogs to help spread the knowledge, but now with the community.dynamics.com doing that for me, I will be following that practice unless a really juicy article catches my eye. Many people have asked where my post are for the first half of 2010, my company had me posting to another blog and maintain two was near impossible. I am now down to just this blog. So good luck and I hope that this blog may help in some way. If you have suggestions or questions, please email me them.

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012 BillOnCRM