CRM 2011 Customer Portal Attachment File download

As I work through the customizations for the customer portal.  I am very impressed with the OOB functionality that is already in this version as opposed to what was available with eService.

For eService I had to add features like, displaying both Closed and Active cases and add the filter functionality.  I was very pleased to see this type of thing was already in the Customer Portal.

Another feature that was not available was for documents that were attached as a notes for service cases.  It was fairly easily to add this functionality to eService and I was very excited to see that this functionality appeared available in Customer Portal.  Well not exactly.  I have not verified this completely but the link in the code resolved to _entity/annotation/Guid .  After scouring the code, the pages templates, links, and pretty much everywhere I could not find where this was supposed to be going.  It is possible that something was configured incorrectly, but with time of the essence in an upgrade situation, I decided to rework the code I wrote for eService.

First you will need to change the URL on the editcase.aspx page to point to the page we are going to create.  I commented out the line of code that was there and the second line is the code that points to our new page.

<%--<asp:HyperLink CssClass="note-attachment" NavigateUrl='<%# (Container.DataItem as Entity).GetRewriteUrl() %>' Visible='<%# Eval("FileSize") != null %>' runat="server"><%# Eval("FileName") %>&nbsp;(<%# Eval("FileSize") %>)</asp:HyperLink>--%>
 <asp:HyperLink ID="HyperLink1" CssClass="note-attachment" NavigateUrl='<%# Eval("AnnotationId", "~/pages/eservice/download.aspx?annotationid={0}") %>' Visible='<%# Eval("FileSize") != null %>' runat="server"><%# Eval("FileName") %>&nbsp;(<%# Eval("FileSize") %>)</asp:HyperLink>

Then I added a page to my Customer Portal Project called download.aspx.  Then I removed all code from the page except for the declaration at the top of the page.  In the code we are going to use the Response.OutPutStream.Write to basically convert this page to the document you are downloading.

below is the only line of code necessary for aspx page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="download.aspx.cs" Inherits="Site.Pages.eService.download" %>

then in the code behind:

using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Xrm;
using System.Linq;
using Microsoft.Xrm.Portal.Access;
using Microsoft.Xrm.Portal.Cms;
using Microsoft.Xrm.Portal.Core;
namespace Site.Pages.eService
{
   public partial class download : PortalPage
   {
    private Guid _annotationId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(Request.QueryString["annotationid"]))
           {
             _annotationId = new Guid(Request.QueryString["annotationid"]);
            }
           //create a new context to the Xrm to pull the annotationset back
           var contextnote = new Xrm.XrmServiceContext("Xrm");
           var retrievedAnnotation = (
                 from note in contextnote.AnnotationSet
                 where note.AnnotationId == _annotationId
                 select new
                {
                  note.FileName,
                  note.DocumentBody,
                  note.FileSize,
                  note.MimeType
                 }
            );
      //loop thorugh the return to create the out put stream.
      foreach (var annotation in retrievedAnnotation)
      {
          Response.ContentType = annotation.MimeType;
          byte[] buffer = Convert.FromBase64String(annotation.DocumentBody);
          int filesize = buffer.Length;
          Response.AddHeader("content-disposition", "attachment; filename=" + annotation.FileName);
          Response.OutputStream.Write(Convert.FromBase64String(annotation.DocumentBody), 0, filesize);
      }
    }
  }
}

So in the code you are requesting the GUID from url and then passing that to the linq to sql querry which brings back all the infromation you need to generate the document.

Feel free to comment and let me know what you think.

About dorothyjarry

Super Dots
This entry was posted in CRM, CRM 2011, Customer Portal, Dynamics. Bookmark the permalink.

17 Responses to CRM 2011 Customer Portal Attachment File download

  1. Love the modern appear. I was pleased with this article. Appreciation for a fine blog post. alle werbemittel tasse tasse werbemittel

    Like

  2. Miss America 2011…

    Like

  3. Nice Blog…I love to read your posts.

    Like

  4. Sally says:

    A better magazine theme will make the blog looks nicer:)

    Like

  5. Great article! I loved the insight and advice given. In addition, your blogging style is very fun to read. If you have time please take a look at my brand new blog and tell me what you think.

    Like

  6. What a great article! Thanks and keep up the great work!

    Like

  7. Henri says:

    Have you found a way to do this within CRM 2011 ? I’d like to implement a list of attachments with clickable URLs through a SSRS report (dashboard), but I’haven’t found a way to provide the base64 decoding required to open the attachment directly (that is, not going through opening the note first).

    Like

  8. Ching Kempen says:

    What an awesome article. Thank you and keep up the good work.

    Like

  9. Reddy says:

    It is also downloading the page source along with file content.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s