locked
extracting pdf attachments RRS feed

  • Question

  • I wrote a program to pull all the attachment from a CRM 2013 instance.  I think the code is typical:

            /// <summary>
            /// Takes an attachment and writes out the document to an existing directory named for the objectid
            /// </summary>
            /// <param name="att">an activitymimeattachment</param>
            static void writeAttachment(Entity att) {
                if (att.Contains("body")) {
                    string filepath = string.Format(@".\{0}\{1}", att.GetAttributeValue<EntityReference>("objectid").Id.ToString()
                                                                , att.GetAttributeValue<string>("filename"));
    
                    using (FileStream fs = new FileStream(filepath, FileMode.Create)) {
                        byte[] attContent = new UTF8Encoding(true).GetBytes(att.GetAttributeValue<string>("body"));
                        fs.Write(attContent, 0, attContent.Length);
                    }
                }
            }
    

    I was spot checking and discovered that any pdf I tried to open with Acrobat Reader came with the error message, "...could not open, ... because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."

    I know that the way they got into CRM was by tracking the email via the Outlook client.  Hence, the example caught my attention.

    What gives?

    many thanks,

    Friday, April 4, 2014 2:12 PM

Answers

  • That appears to be the problem/fix.

    Hope this waste of your time is useful for someone in the future!

    • Marked as answer by mardukes Friday, April 4, 2014 4:08 PM
    Friday, April 4, 2014 4:08 PM

All replies

  • Now I just checked on a Word doc and got a file corrupted error.  I'm wondering if UTF8Encoding is the way to go.
    Friday, April 4, 2014 2:17 PM
  • I found this (newer than sample from 4.0) http://msdn.microsoft.com/en-us/library/gg328429(v=crm.6).aspx

    So, I'm trying this:

    byte[] attContent = Convert.FromBase64String(att.GetAttributeValue<string>("body"));


    • Edited by mardukes Friday, April 4, 2014 4:09 PM
    Friday, April 4, 2014 2:35 PM
  • That appears to be the problem/fix.

    Hope this waste of your time is useful for someone in the future!

    • Marked as answer by mardukes Friday, April 4, 2014 4:08 PM
    Friday, April 4, 2014 4:08 PM