Home > CRM 2011, Plug-Ins > Adding file as “Attachment” to activities programmatically in plug-in

Adding file as “Attachment” to activities programmatically in plug-in

Hi,

We can add file as attachment and associate to the CRM objects i.e., Email, Task etc…

Email with attachment

Email with .pdf file as attachment

The entity that store attachment information is “activitymimeattachment”

Below is the code snippet to add “.pdf” file as attachment to the Email activity

Entity attachment = new Entity(“activitymimeattachment”);

attachment[“subject”] = “My Subject”;

string fileName = “Sample.pdf”;

attachment[“filename”] = fileName;

byte[] fileStream= = new byte[] { }; //Set file stream bytes

attachment[“body”] = Convert.ToBase64String(fileStream);

attachment[“mimetype”] = “text/plain”;

attachment[“attachmentnumber”] = 1;

Guid emailId; //GUID of Email activity

attachment[“objectid”] = new EntityReference(“email”, emailId);

attachment[“objecttypecode”] = “email”;

IOrganizationService service;

service.Create(attachment);

Note :-

  • In the above code the property “objecttypecode” is slight misleading, as it expects “EntityLogicalName” rather than “EntityTypeCode”
  • So make sure to set  attachment[“objecttypecode”] = EntityLogicalName (i.e., email)

🙂

Advertisement
  1. kiran
    September 28, 2012 at 8:44 PM

    your note saved me lot of time…
    attachment[“objecttypecode”] = EntityLogicalName (i.e., email)
    thanks….

  2. February 12, 2015 at 10:15 AM

    Hi Rajeev,
    I am rendering a Report using Plugin and emailing it as an attachment.
    I face an issue of blank data in Report, though it runs and renders fine from CRM.
    And all the required parameters are passed.
    Actually the data where Filtered View is not used in the Dataset is coming fine, but for other entities- ACtivity Pointer and a Custom entity, I get no data when using FilteredView, and error while trying to use without Filtered.
    I am running the Plugin as an Adminiastrator,
    The Credentials for the REport Execution SErvice is- rs.Credentials = System.Net.CredentialCache.DefaultCredentials

    • February 12, 2015 at 1:55 PM

      Hi,
      Since you are authenticating Report server from your Plug-in make sure the Plug-in context user has rights to the Report Server.

      You can troubleshoot by explicitly providing credentials of User who has access to Report sever.

      reportService.UseDefaultCredentials = false;
      reportService.Credentials = new NetworkCredential(UserId,Password,Domain);

      You can check the User’s accessibility to your report server using Report services configuration manager in Report Server. You can also browse http://servername/ReportServer.

      Thanks,
      Rajeev

      • May 19, 2015 at 2:41 PM

        Thank you very much Rajeev,
        Some how It seems there’s problem with taking the running User for Report in case of a Plugin, When I switched to a Workflow, it worked fine!

  3. nitesh
    March 26, 2015 at 2:28 PM

    I uses same code for creating attachment .. and my attachment body is coming from my WCF service in byte[] .. this is ok .. I am able to create attachment on create of email using plugin … but when I send this email (manually click on save) it gives me error “Unexpected error is occurred” .. if I save the attachment and remove the attachment .. again attach the same attachment from saved location.. mail sent without error .. what could be the reason of this….

  4. nitesh
    March 26, 2015 at 2:54 PM

    got the problem … in mimetype I was using @”application\pdf” .. I updated to “text/plain” .. now working fine …. 🙂

  5. Nishant
    September 28, 2015 at 2:12 AM

    Hi Rajeev,
    I getting error while creating a new record in annotation error: 0x8004021f The parent’s object type is invalid.\n Platform\n”
    I am converting pdf file into ToBase64 and creating a new record in notes which i can use further, i am using plug-in CRM 4.0

    • September 28, 2015 at 5:45 PM

      Hi,

      Hope you read ‘Notes’ section in the article and passing valid ‘objecttypecode’.

      Note:
      •In the above code the property “objecttypecode” is slight misleading, as it expects “EntityLogicalName” rather than “EntityTypeCode”
      •So make sure to set attachment[“objecttypecode”] = EntityLogicalName (i.e., email)

  1. July 9, 2012 at 7:39 PM
  2. January 15, 2014 at 1:26 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: