locked
Reusing CRM E-mail Attachment RRS feed

  • Question

  • We have developed a plugin that when an e-mail message is Sent, it will create additional e-mail messages (based on the recipient).
    If the source message contains an attachment, each of those messages that are being created will need to contain an attachment as well.

    The messages are being created, as well as an attachment, but when I open one of those newly created e-mail messages, there is an attachment with no name, size and when clicking it opens up an empty screen. I am pretty sure this is not the way it is supposed to work.

    Code below:

          Guid emailid = CreateEmailMessage(id, groupid, entityname, sender, toparty, cc, subject, description);
          // SendEmail(emailid);
    
          // Retrieve All Current Attachments and Add them to 
        EntityCollection attachments = RetrieveAttachments(id);
    
          if (attachments.Entities.Count > 0)
          {
           foreach (Entity attachment in attachments.Entities)
           {
            EntityReference objectid = new EntityReference("email", emailid);
            string objecttypecode = "email";
            string attachmentsubject = attachment.Contains("subject") ? attachment["subject"].ToString() : "";
            string attachmentfilename = attachment.Contains("filename") ? attachment["filename"].ToString() : "";
            Guid attachmentid = attachment.Id;
    
            Entity linkedattachment = new Entity("activitymimeattachment");
            linkedattachment.Attributes["objectid"] = objectid;
            linkedattachment.Attributes["objecttypecode"] = objecttypecode;
            if (!string.IsNullOrEmpty(attachmentsubject))
             linkedattachment.Attributes["subject"] = attachmentsubject;
            //if (!string.IsNullOrEmpty(attachmentfilename))
            // linkedattachment.Attributes["filename"] = attachmentfilename;
            linkedattachment.Attributes["attachmentid"] = attachmentid;
            service.Create(linkedattachment);
           }
          }
    
    
    Tuesday, June 21, 2011 7:45 PM

Answers

  • The problem was with the attachmentid attribute. It doesn't reference the attachment.Id, but the attachmentid attribute (different entity):

    linkedattachment.Attributes[

    "attachmentid"] = new EntityReference("attachment", ((EntityReference)attachment.Attributes["attachmentid"]).Id);

    Hope this helps someone...

    • Marked as answer by Aric Levin Friday, July 22, 2011 11:22 PM
    Friday, July 22, 2011 11:22 PM

All replies

  • You should also copy the following attributes:

    • filesize
    • mimetype
    • body

    The body attribute contains the BASE64 encoded content of the attachment, without which an empty attachment would be the expected behaviour.


    --pogo (pat) @ pogo69.wordpress.com
    Tuesday, June 21, 2011 8:08 PM
  • If I copy the body, then it will be the full attachment.
    This is not the intention. The intention is to have referenced/reused attachments:

    See Reusing E-mail Attachments section in the following post on MSDN:

    http://msdn.microsoft.com/en-us/library/gg334229.aspx

    Because you are reusing an existing attachment file, you do not have to specify the ActivityMimeAttachment.Body and ActivityMimeAttachment.FileName attribute values while you are creating and associating e-mail attachment records to e-mails or e-mail templates.

    Thanks

     

    Wednesday, June 22, 2011 7:38 AM
  • Anyone know this???
    Wednesday, June 29, 2011 9:05 PM
  • The problem was with the attachmentid attribute. It doesn't reference the attachment.Id, but the attachmentid attribute (different entity):

    linkedattachment.Attributes[

    "attachmentid"] = new EntityReference("attachment", ((EntityReference)attachment.Attributes["attachmentid"]).Id);

    Hope this helps someone...

    • Marked as answer by Aric Levin Friday, July 22, 2011 11:22 PM
    Friday, July 22, 2011 11:22 PM