locked
CRM 4.0: Error when opening the PDF Activitymimeattachment RRS feed

  • Question

  • I use the following code to create and attach a PDF on the fly from a string 
    to an email but when I open that PDF Attachment I get "Adobe Reader could not 
    open "testpdf.pdf" because it is either not a supported file type or because 
    the file has been damaged...." error message. The Email message gets created 
    and I see an attachment on it but can't open it. Do I need to add "pdf header 
    info" to the string??
    
    Here is the code:
    
    //Create email
     email x = new email();
                          x.subject = "Test Email subject";
                            x.description = "this is my email body";
                            Guid emailID = myService.Create(x);
    
                            string tmp;
                            tmp = "Test pdf body";
                            Byte[] data;
                            ASCIIEncoding encode = new ASCIIEncoding();
                            data = encode.GetBytes(tmp);
    
                       string encodedData = System.Convert.ToBase64String(data);
                            // create activitymimeattachment 
                            activitymimeattachment ata = new 
    activitymimeattachment();
                            ata.activityid = new Lookup("email", emailID);
                            ata.filename = "TestPDF.pdf";
                            ata.mimetype = "application/pdf";
                            ata.body = " " ;
                            ata.subject = "test subject";
                            ata.attachmentnumber = new CrmNumber();
                            ata.attachmentnumber.Value = 1;
                            Guid ataid = myService.Create(ata);
    
    
                            AllColumns colp = new AllColumns();
                            activitymimeattachment ataread =
                            
    (activitymimeattachment)myService.Retrieve(EntityName.activitymimeattachment.ToString(), ataid, colp);
                            ataread.mimetype = "application/pdf";
                            ataread.body = encodedData;
                            ataread.filename = "TestPDF.pdf";
                            myService.Update(ataread);
    Friday, August 14, 2009 2:15 PM

Answers

  • i c...

    so...you are creating it from just a string...

    This can be the problem...

    PDF files contain header information with the content. If you simply add string as pdf file, it will not work. thats why you are getting an error saying... cannot open/ corrupted.

    First create a pdf file in the hard drive, 
    retrieve the bytes and then convert it to base64encodedstring
    and then try with your code to create an attachment.


    Probably you will get the same error if you do the following:

    1. create a new file with name abc.txt
    2. open it with notepad
    3. input some texts and save
    4. rename it to abc.pdf
    5. open the file or add it as an attachment


    then try to open it from your explorer using acrobat reader or from crm....u will get the same error...

    hope this helps...


    regards,
    jahedur 
    Friday, August 14, 2009 2:59 PM

All replies

  • Hi.

    Why do you use two request instead of one? Use one. Check this url .
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Friday, August 14, 2009 2:22 PM
    Moderator
  • Thanks for the reply. Just to clarify. this is a .NET Console program not a workflow or plugin. It is running on the CRM Server.

    Anyway, I tried only creating the attchment (one request) but I get the same error. I saw some code online and that is why I am doing a create and an update.

    Thanks.

    Friday, August 14, 2009 2:31 PM
  • Hi. I suggest you to cover you code with

    try
    {
    }
    catch (SoapException exc)
    {
    throw new Exception(exc.Detail.InnerText);
    }

    for detailed exception text.

    I hope this will help you.
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Friday, August 14, 2009 2:33 PM
    Moderator
  • Try setting the encoded data at the first attempt and avoid the second update action.

    And dont set the MimeType attribute. CRM will set it automaticaly to octet stream.
    Friday, August 14, 2009 2:33 PM
  • I only included part of the code but he whole thing is in a try/catch. There is no error creating the attachment, just when I open it.

    Friday, August 14, 2009 2:34 PM
  • Hi, so what exception text?
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Friday, August 14, 2009 2:35 PM
    Moderator
  • Well just create attachment as .rtf and once it is been created as email attachment ...open the attachment with wordpad / office word ..

    So give me file name as  follows ..

    ata.filename = "TestRTF.rtf";

    we have used this approach for one of our clients...see if this one works for you..
    Friday, August 14, 2009 2:41 PM
  • I get the same result when I "Try setting the encoded data at the first attempt and avoid the second update action"
    Friday, August 14, 2009 2:41 PM
  • There is no exception . The only error I get is when I open the attachment in CRM.

    Friday, August 14, 2009 2:42 PM
  • yes, try creating .rtf attachment and see it works...(as per my above post)
    Friday, August 14, 2009 2:48 PM
  • I get the same result when I "Try setting the encoded data at the first attempt and avoid the second update action"
    have you tried with the second option?

    "And dont set the MimeType attribute. CRM will set it automaticaly to octet stream."
    Friday, August 14, 2009 2:48 PM
  • Hi.

    Try to fill filesize property of attachment.

    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Friday, August 14, 2009 2:50 PM
    Moderator
  • I tried not setting the Mimetype but I get the same result.

    I tried create the attachment as rtf (without setting the mimetype) and the attachment got created and I can open with no problems.

    This makes me wonder if I need to add some PDF tags\header info to the text.
    Friday, August 14, 2009 2:56 PM
  • i c...

    so...you are creating it from just a string...

    This can be the problem...

    PDF files contain header information with the content. If you simply add string as pdf file, it will not work. thats why you are getting an error saying... cannot open/ corrupted.

    First create a pdf file in the hard drive, 
    retrieve the bytes and then convert it to base64encodedstring
    and then try with your code to create an attachment.


    Probably you will get the same error if you do the following:

    1. create a new file with name abc.txt
    2. open it with notepad
    3. input some texts and save
    4. rename it to abc.pdf
    5. open the file or add it as an attachment


    then try to open it from your explorer using acrobat reader or from crm....u will get the same error...

    hope this helps...


    regards,
    jahedur 
    Friday, August 14, 2009 2:59 PM
  • I agree. that is where I was going with with my question about adding pdf tags/header info. I will have to look into creating the pdf file from the string.

    Thanks.
    Friday, August 14, 2009 3:28 PM