We help IT Professionals succeed at work.

how to download annotation attachments from Microsoft dynamics 4.0 Cases

HI All,

I am working on a self service portal as part of extension of the Microsoft Dynamics.

I have a web scenario where I am logging Cases directly in MSD from Web using the Web portal.

I am able to log new case and insert/retreive Notes/Artilces from the Case as part of discussions.

I also have one option to upload the attachements with the Note which is working fine for me.


The Issue comes when i try to download the attachments.

I am able to download only txt files correctly all other formats are always corrupted when i download the file.

Below is the code which i m using to download file


   private void DownloadFile(string FileData, String FileName, int FileSize, String MimeType)
    {
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/x-unknown";
        Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + FileName + "\"");
        Response.BinaryWrite(Convert.FromBase64String(FileData));
        Response.End();
    }

For uploading the file i m using

annotation.documentbody=Convert.ToBase64String(fuAttachment.FileBytes)


Can please anyone help me on the issue that why is the file not downloaded correctly.

One thing more The attachments which are uploaded from web can be downloaded from the MSD Case correctly so there is no issue with uploaded format.

Thanks in Advance..!

Comment
Watch Question

Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Hi amar31282,
Try the following code snippet
   #region How to download attachment from annotation record
               
                // SDK: annotationId = new Guid("{bee08735-09d3-de11-9d71-00155da4c706}");
                Guid annotationId = setupAnnotationId;

                // Define the columns to retrieve from the annotation record.
                ColumnSet cols1 = new ColumnSet();
                cols1.Attributes = new string[] { "filename", "documentbody" };

                // Retrieve the annotation record.
                annotation annotationAttachment = (annotation)service.Retrieve(EntityName.annotation.ToString(), annotationId, cols1);

                // Download the attachment in the current execution folder.
                using (FileStream fileStream = new FileStream(annotationAttachment.filename, FileMode.OpenOrCreate))
                {
                    byte[] fileContent = new UTF8Encoding(true).GetBytes(annotationAttachment.documentbody);
                    fileStream.Write(fileContent, 0, fileContent.Length);
                }

                #endregion

Code source : http://msdn.microsoft.com/en-us/library/cc151179.aspx
Regards,
Chinmay


Author

Commented:
HI Chinmay,

Thanks for reply.

I have already tried this but its also giving same issue. The file saved by filestream is working fine for Text file but all other pdf/word/image are always corrupted..


Also i need to download the file by clicking on a button so i want it through Response..

Regards,
Amarjit

Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Hi Amarjit,
Well you can always get it downloaded on server and then do a response.redirect to the file.
Ok for the files which are not working properly.. can you do one thing? Open that file in a notepad and tell me what you see. Do you see binary data or something else?
Regards,
Chinmay


Author

Commented:
HI Chinmay

yes i can download them but i was simply not intrested in saving them :)
anyways in what ever way it works i will be fine with that..

I have off today from office and will be there on monday so i can't assure the file text 100% right now but as far as i remember it is in binary format.

also one thing When i download the Image the image is opened but it have just the upper left corner printed correctly rest all is always blank....


Regards,
Amarjit


Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Hi Amarjit,
Now I am very curious, I will check this out in my environment as soon as I get some free time.
Regards,
Chinmay


Author

Commented:
ok sure and thanks for your effords.. I appreciate it. :).

I am not sure how CRM stores the attachements when we save it from CRM but I am saving it using

annotation.documentbody=Convert.ToBase64String(fuAttachment.FileBytes)

and it is opening fine in CRM ... so not sure what i am missing and its not opening in a web browser.

one question In my browser i am having all the Browser history and rest everything in No Save mode so my browser doesn't ever remember anything (and i have not checked the attachments on any other system yet ). Is there any minimum possibility that it could be the reason for corrupted file or incomplete jpg

Regards,
Amarjit
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Really Don't know. It won't take me long to figure it out once I check it in my environment so don't worry. At most I think some kind of minor disposition or encoding issue. And if it is not than it will be interesting to figure it out.
Regards,
Chinmay


Author

Commented:
Nice.. to hear


I will be waiting Impatiently

Regard,
Amarjit
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Your last message pushed me to do this right now rather than later so here is the result, with the code I've sent there was an error
you are supposed to use  byte[] fileContent = Convert.FromBase64String(annotationAttachment.documentbody);
I did this with a WinForms application so you still might face some issues with directly writing file to response.
First confirm that this is working fine or not and then we will see why it is giving you issues with Response.Write. I doubt that FileData is culprit but let's see how it goes later on.

Regards,
Chinmay


Author

Commented:
hmm not really following you

i m using DownloadFile(annotationAttachment.documentbody, annotationAttachment.FileName, annotationAttachment.FileSize, annotationAttachment.mimetype)

To call the download function so i think the same way as u said..


Hmm I am really frustrated that i can't test it right now as i am not at office today and will be there on monday only..

did u find some luck to download other format correctly with this code ..?
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Of course.. I did it with a PNG and a simple text file, you want me to try other formats?


Author

Commented:
can u paste your code here

Author

Commented:
Please try with pdf also
Chief Technology Ninja
Distinguished Expert 2019
Commented:
Yupp. It works like a charm...
Guid annotationId = new Guid("C55CE9DB-C3C1-46BA-8EA1-8C36A8AF4213");

            // Define the columns to retrieve from the annotation record.
            ColumnSet cols1 = new ColumnSet();
            cols1.Attributes = new string[] { "filename", "documentbody" };

            // Retrieve the annotation record.
            annotation annotationAttachment = (annotation)logonProvider.CrmServiceInstance.Retrieve(EntityName.annotation.ToString(), annotationId, cols1);

            // Download the attachment in the current execution folder.
            using (FileStream fileStream = new FileStream(annotationAttachment.filename, FileMode.OpenOrCreate))
            {
                byte[] fileContent = Convert.FromBase64String(annotationAttachment.documentbody);
                fileStream.Write(fileContent, 0, fileContent.Length);
            }

Author

Commented:
hmm i have really tried the same code yesterday but it never worked fine for me

Strange...

Seems like now i need to check on another browser seems like my browser settings are causing this issue..

Anyways i thanks a lot for you help I will close the question now as i have seen that i m not doing anything wrong in my code.

I will post here if it worked fine for me on another browser or open new question if it still didn't work..:)

Thanks a lot for all your help and time...

Regards,
Amarjit

Author

Commented:
thanks a lot.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
You are welcome.. and thanks for closing this question... I am kind of.. just 2K points away from my next title in Experts-Exchange. :)

Author

Commented:
haha yes i have seen that i read your blog .. By the way i m also an expert on EE but not getting enough time to spend here.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Ya.. I quit my job and kind of spending time as I see it fit.. EE is not priority as I've got lot of other things to do but sometimes out of curiosity I give priority to EE questions. :)

Author

Commented:
Hey Chinmay,

Today Once again i tried to download the annotation attachments using the code given by you.

  // Download the attachment in the current execution folder.
             using (FileStream fileStream = new FileStream(annotationAttac
hment.filename,  FileMode.OpenOrCreate))
            {
                byte[]  fileContent = Convert.FromBase64String(annotationAttachment.documentbody);
                 fileStream.Write(fileContent, 0,  fileContent.Length);
            }


But it was still not working for me... I scrubbed my head a lot and finally found two issues

Guess what..?

1. I was passing the annotaion.documentbody to downloadfile method as string and string length was not able to handle so long data so it was truncated.
----after  solving issue 1
2. I was using CRM SDK for retrieving the attachements and again the SDK's annotation.documentBody length was not enough to handle whole data so it was again truncated.

So finally i used the Web referenced service and got it working lol...

Regards,
Amarjit

Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Hi Amarjit,
:)... funny... see I asked you.. try downloading to your local folder first than response.redirect but ya this SDK issues new to me... I will give it a shot if I get some free time. And Thanks for the tip :)
Regards,
Chinmay


Author

Commented:
Hey chinmay

one more strange thing

see this link
http://allfaq.org/forums/t/178615.aspx

i found it today when i was searching for my solution
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
WTF!!! A Site RIP Dude... Report this to EE... You do it in a minute or I will... that's my bloody code.. n your bloody question... These Rascals.. are taking it away... GRRR GRRRR DO something please... report to EE please.

Author

Commented:
i have already done dear.. :)
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Kewl... Thanks. :)