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)

🙂

Advertisements
Advertisements

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

  1. File name ABC.pdf is invalid – error while creating attachment programmatically « Rajeev Pentyala – Dynamics CRM Blog Avatar

    […] The other day I was getting “Invalid file name” error when I try to create an attachment with .pdf file and associating to Email, in my plug-in. (link) […]

  2. kiran Avatar
    kiran

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

  3. […] Adding file as “Attachment” to activities programmatically in plug-in […]

  4. ssahanajainSAHANA Avatar

    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

    1. Rajeev Pentyala Avatar

      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

      1. Sahana Avatar

        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!

  5. nitesh Avatar
    nitesh

    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….

  6. nitesh Avatar
    nitesh

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

  7. Nishant Avatar
    Nishant

    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

    1. Rajeev Pentyala Avatar

      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)

Leave a comment