Download a file to the user’s browser via CRM

To download a custom file in CRM using VB you need to create a class that inherits Blackbaud.AppFx.Server.AppCatalog.AppCustomFileDownloadProcess like so.

Imports Blackbaud.AppFx.Server
Imports System.IO

Public Class TestFileDownloadHandler
    Inherits AppCatalog.AppCustomFileDownLoadProcessor

    Public Overrides Function CheckSecurity() As SecurityCheckResult
        Return SecurityCheckResult.SecurityCheckPassed
    End Function

    Public Overrides Sub ValidateArgs()
        If String.IsNullOrEmpty(Me.RequestArgs.Parameters("Text")) Then
            Throw New Exception("Text parameter not provided")
        End If
    End Sub

    Public Overrides Function PopulateStream(stream As Stream) As AppCatalog.AppCustomFileDownloadResult
        Dim result = New AppCatalog.AppCustomFileDownloadResult
        result.DataLoaded = False

        If stream Is Nothing Then
            Return result
        End If

        Me.RequestContext.HTTPContext.Response.AddHeader("content-disposition", "attachment; filename=""test.txt""")

        Dim text = Me.RequestArgs.Parameters("text")

        Using streamWriter As New StreamWriter(stream)
            streamWriter.Write(text)
        End Using

        result.DataLoaded = True

        Return result
    End Function
End Class

As you can see it’s pretty straight forward. You can pass in any number of arguments you want, like a record ID, etc. Then you would just build out the stream.

Now to actually get the file is a little different. It is actually served up by a file handler.

http://localhost/CRM/download/customfiledownload.ashx?dbname=bbinfinity&assemblyname=CustomFx.Common.Catalog&classname=CustomFx.Common.Catalog.TestFileDownloadHandler&text=yes

The customfiledownload.ashx takes in a few parameters itself.

DBName = BBInfinity (is almost always this)
AssemblyName = This is the name of the actual dll and can have .dll at the end
ClassName = This is the fully qualified class name
Text = My custom parameter that tells my custom handler what to put in the file

There is a more detailed blog on this here: http://www.bbdevnetwork.com/blogs/creating-a-custom-file-downloader-using-the-infinity-sdk/

Happy coding!
🙂

Posted in CRM

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