Tuesday, March 16, 2010

Solving Issues with opening/Downloading attachments in Notes after Rollup 7

Downloading Notes in MS CRM

        We can download the attachments of Notes by framing the Following URL.
window.open("/MicrosoftCRM/Activities/Attachment/download.aspx?AttachmentType=1001&AttachmentId=4108f5f9-63f7-dd11-b2e0-0003ff2d0264");


     But this approach works only when we don't have roll-up 7 on the system.
After installing the roll-up above 6 , this approach won't works,because security tokens will be added to this link.So it gives error as "INVALID_WRPC_TOKEN"


     so one of the best approach i know to solve this issue is as follows,



public byte[] DownloadAttachment(string AnnotationId)
        {
          
          
            Guid annotationId = new Guid( AnnotationId );

            // 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)crmService.Retrieve(EntityName.annotation.ToString(), annotationId, cols1);

          
            using (FileStream fileStream = new FileStream(annotationAttachment.filename, FileMode.OpenOrCreate))
            {
                byte[] fileContent = new UTF8Encoding(true).GetBytes(annotationAttachment.documentbody);
            //    fileStream.Write(fileContent, 0, fileContent.Length);

                return fileContent;
            }
          
        }


In the Download Button Click Event,


 byte[] fileData = DownloadAttachment(annotationID);

      
            Response.ClearContent();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
            BinaryWriter bw = new BinaryWriter(Response.OutputStream);
            bw.Write(fileData);
            bw.Close();
            Response.ContentType = mimetype;
            Response.End();

No comments:

Post a Comment