locked
How to download an attachment from the context.InputParameters - help!! RRS feed

  • Question

  • Hi Guys.

    Ive spent more than 4 hours trying to figure out the correct way for downloading a attachment from a "DeliverIncoming" message of an email.
    I wanted to trap some events from inside the incoming email that is.

    So any suggestions, friendly or otherwise is free...please comment if you can.
    Here is my snippet:

     

     

    Thanks.....

    foreach (PropertyBagEntry prop in context.InputParameters.Properties)
    
    {
    
     if (prop.Name == "Attachments")
    
     {
    
      BusinessEntityCollection busEntity = (BusinessEntityCollection)context.InputParameters.Properties["Attachments"];
    
    
    
      if (busEntity.EntityName == EntityName.activitymimeattachment.ToString())
    
      {
    
      List<BusinessEntity> entity = (List<BusinessEntity>)busEntity.BusinessEntities;
    
       foreach (DynamicEntity mime in entity) 
    
       {
    
       foreach (Property pr in mime.Properties)
    
       {
    
        if (pr.Name == "mimetype")
    
        {
    
        //script here
    
    }}}}}
    
    

     

    Thursday, April 29, 2010 7:06 AM

Answers

  • My impression is you should work with annotation or activitymimeattachment entity's Pre-Create message, as DeliverIncoming message doesn't actually post attachment to CRM server. 

    When working with annotation or activitymimeattachment Pre-Create message, you should be able to easily find the email activity's ID. 


    Daniel Cai | http://danielcai.blogspot.com
    • Marked as answer by tribeka88 Friday, April 30, 2010 3:40 AM
    Thursday, April 29, 2010 12:35 PM
  • This is what I use to convert a attachment in the DeliverIncoming message PreStage Create

    //Attachments
    BusinessEntityCollection Attachments = (BusinessEntityCollection)context.InputParameters.Properties["Attachments"];
    if (Attachments.BusinessEntities.Count > 0)
    {
      foreach (DynamicEntity attachment in Attachments.BusinessEntities)
      {
     annotation note = new annotation();
     note.objecttypecode = new EntityNameReference(EntityName.letter.ToString());
     note.objectid = new Lookup(EntityName.letter.ToString(),letterID);
     note.documentbody = (attachment.Properties.Contains("body")) ? Convert.ToString(attachment.Properties["body"]) : ""; ;
     note.filesize = (attachment.Properties.Contains("filesize")) ? (CrmNumber)(attachment.Properties["filesize"]): CrmNumber.Null ;
     note.mimetype = (attachment.Properties.Contains("mimetype")) ? Convert.ToString(attachment.Properties["mimetype"]): "";
     note.filename = (attachment.Properties.Contains("filename")) ? Convert.ToString(attachment.Properties["filename"]): "";
     note.subject = (attachment.Properties.Contains("subject")) ? Convert.ToString(attachment.Properties["subject"]) : "";
     objService.Create(note);
      }


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Thursday, April 29, 2010 1:28 PM
    Moderator

All replies

  • You have done hard work, what are you actually having trouble with. The body property contains the file contents stored as base 64.

     

    Here are some references

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

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


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Thursday, April 29, 2010 7:21 AM
    Moderator
  • Well your code seems fine, just use the attribute "body" instead of "mimetype" to retrieve the file content. It is a string encoded in Base64

     

    EDIT: Rhett was faster than me... sorry for the double answer...


    My blog : http://mscrmtools.blogspot.com

    All my tools on my new dedicated site: MSCRMTools Repository
    Thursday, April 29, 2010 7:23 AM
    Moderator
  • Thanks for the reply.

    Actually this is my problem, because whenever I test this in the "DeliverIncoming" message on a pre-stage - It doesnt seem like it is ever going inside of the condition expression above.  Thats why I couldnt figure out if I was actually doing something wrong, or that I am missing something in my code.

    Any others thoughts why its not hitting the above code?
    Thanks

    Thursday, April 29, 2010 12:04 PM
  • My impression is you should work with annotation or activitymimeattachment entity's Pre-Create message, as DeliverIncoming message doesn't actually post attachment to CRM server. 

    When working with annotation or activitymimeattachment Pre-Create message, you should be able to easily find the email activity's ID. 


    Daniel Cai | http://danielcai.blogspot.com
    • Marked as answer by tribeka88 Friday, April 30, 2010 3:40 AM
    Thursday, April 29, 2010 12:35 PM
  • This is what I use to convert a attachment in the DeliverIncoming message PreStage Create

    //Attachments
    BusinessEntityCollection Attachments = (BusinessEntityCollection)context.InputParameters.Properties["Attachments"];
    if (Attachments.BusinessEntities.Count > 0)
    {
      foreach (DynamicEntity attachment in Attachments.BusinessEntities)
      {
     annotation note = new annotation();
     note.objecttypecode = new EntityNameReference(EntityName.letter.ToString());
     note.objectid = new Lookup(EntityName.letter.ToString(),letterID);
     note.documentbody = (attachment.Properties.Contains("body")) ? Convert.ToString(attachment.Properties["body"]) : ""; ;
     note.filesize = (attachment.Properties.Contains("filesize")) ? (CrmNumber)(attachment.Properties["filesize"]): CrmNumber.Null ;
     note.mimetype = (attachment.Properties.Contains("mimetype")) ? Convert.ToString(attachment.Properties["mimetype"]): "";
     note.filename = (attachment.Properties.Contains("filename")) ? Convert.ToString(attachment.Properties["filename"]): "";
     note.subject = (attachment.Properties.Contains("subject")) ? Convert.ToString(attachment.Properties["subject"]) : "";
     objService.Create(note);
      }


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Thursday, April 29, 2010 1:28 PM
    Moderator
  • Thanks much Rhett.

    This did work. 

    What did confuse me is that in my code I went through all propertyBagEntry and tried to execute my piece of code when I recognize a PropertyBag that I will need and work only on those that i need - i think this is more efficient.
    But apparently, for some reason this does not work - either the Name property is not populated for Attachments in the pre-stage or some freakin odd reason or what.

    The following are just the Names recognized when stepping through each PropertyBagEntry in the context.InputParameters
    >>OptionalParameters;Bcc;SubmittedBy;From;Body;To;MessageId;Cc;ReceivedOn;Subject;
    It does not recognize the "Importance" and "Attachments" propertyBagEntry.

    What I am now doing is do what you did in your snippet above, and take that out of the foreach statement I initially had. 

    Odd - but if anybody knows why, or can shed light on my confusion here - I would really appreciate it.  Another learning experience for me...:)

     

    Thursday, April 29, 2010 3:21 PM
  • In my code I check using if(context.InputParameters.Properties.Contains("attachments"))

     


    MSCRM Bing'd - http://bingsoft.wordpress.com
    Thursday, April 29, 2010 4:17 PM
    Moderator
  • Yes I did that too.

    It just took me hours to know why I am not hitting the Attachments property when searching by name on a foreach (as written way up this thread) when I know that PropertyBagEntry is there.

    Thursday, April 29, 2010 5:13 PM
  • Hi Guys,

    In my code writing "Properties" after "context.InputParameters." gives me an error.

    Am i missing any References ?

    Thursday, June 27, 2013 11:03 AM