Tuesday, July 24, 2012
FileName Action

Actions Export to Spreadsheet, Download, and Report will produce a file when executed on the server. The file name is a concatenation of the data controller and the view.

Customers generated report file named after the controller and view.

Let’s create a business rule that will assign custom names to the output files.

First, enable shared business rules and regenerate the project. Start the Project Designer and click Develop on the toolbar to open the project in Visual Studio.

In the Solution Explorer, double-click on ~\App_Code\Rules\SharedBusinessRules.cs(vb) file.

SharedBusinessRules file in the Solution Explorer.

Replace the default code with the following:

C#:

using System;
using MyCompany.Data;
using System.IO;

namespace MyCompany.Rules
{
    public partial class SharedBusinessRules : MyCompany.Data.BusinessRules
    {

        public SharedBusinessRules()
        {
        }

        // 1. "Actions | Export to Spreadsheet" in any controller
        [ControllerAction(".", "FileName", "ExportRowset")]
        public void AssignFileNameToExportSpreadsheet(string fileName)
        {
            UpdateFieldValue("FileName", String.Format("Live {0} Data Link.iqy", 
                ControllerName));
        }

        // 2. "Actions | Download" in 'Suppliers' controller
        [ControllerAction("Suppliers", "FileName", "ExportCsv")]
        public void AssignFileNameToDownloadCsv(string fileName)
        {
            UpdateFieldValue("FileName", String.Format("{0:yyyy-MM-dd} Supplier List.csv", 
                DateTime.Now));
        }

        // 3. "Report |.." in any data controller
        [ControllerAction(".", "FileName", "Report")]
        [ControllerAction(".", "FileName", "ReportAsPdf")]
        [ControllerAction(".", "FileName", "ReportAsExcel")]
        [ControllerAction(".", "FileName", "ReportAsWord")]
        [ControllerAction(".", "FileName", "ReportAsImage")]
        public void FormatReportFileName(string fileName)
        {
            UpdateFieldValue("FileName", String.Format("{0:yyyy-MM-dd} {1}{2}",
                DateTime.Now,
                Arguments.Controller,
                Path.GetExtension(fileName)));
        }
    }
}

Visual Basic:

Imports MyCompany.Data
Imports System
Imports System.IO

Namespace MyCompany.Rules

    Partial Public Class SharedBusinessRules
        Inherits MyCompany.Data.BusinessRules

        Public Sub New()
            MyBase.New()
        End Sub

        ' 1. "Actions | Export to Spreadsheet" in any controller
        <ControllerAction(".", "FileName", "ExportRowset")>
        Public Sub AssignFileNameToExportSpreadsheet(fileName As String)
            UpdateFieldValue("FileName", String.Format("Live {0} Data Link.iqy",
                                                       ControllerName))
        End Sub

        ' 2. "Actions | Download" in 'Suppliers' controller
        <ControllerAction("Suppliers", "FileName", "ExportCsv")>
        Public Sub AssignFileNameToDownloadCsv(fileName As String)
            UpdateFieldValue("FileName", String.Format("{0:yyyy-MM-dd} SupplierList.csv",
                                                       DateTime.Now))
        End Sub

        ' 3. "Report |.." in any data controller
        <ControllerAction(".", "FileName", "Report")>
        <ControllerAction(".", "FileName", "ReportAsPdf")>
        <ControllerAction(".", "FileName", "ReportAsExcel")>
        <ControllerAction(".", "FileName", "ReportAsWord")>
        <ControllerAction(".", "FileName", "ReportAsImage")>
        Public Sub FormatReportFileName(fileName As String)
            UpdateFieldValue("FileName", String.Format("{0:yyyy-MM-dd} {1}{2}",
                                                       DateTime.Now,
                                                       Arguments.Controller,
                                                       Path.GetExtension(fileName)))
        End Sub
    End Class
End Namespace

Save the file.

The implementation handles FileName action with arguments equal to the action command names that require an output file. For example, Export to Spreadsheet action has it’s command set to “ExportRowset”. The application framework will try to obtain a custom filename from the business rules by raising an internal action with command name of “FileName” and argument of “ExportRowset”. If the business rules are updating the filename, then the new name will be assigned to the output file.

Press Ctrl+F5 keyboard shortcut to run the web application. Navigate to the Suppliers page. On the action bar, click Actions | Download.

Download action in the suppliers grid view.

The name of the file will be the current date followed by “Supplier List”.

Download action produced a file with a custom name.

Report action file output will be named with the date and controller name.

PDF Report produced a file with a custom name.

The Export to Spreadsheet action will name the file “Live Suppliers Data Link”.

Export action also produces a file with a custom name.