Exporting portal data to Excel

Using the PerformClick API, CreateDocument API, and IPFExportCollectionGlobal server-side global script, you can give portal users the ability to export a collection of data to MS Excel with one click.

To provide this option, you must add server-side code to the relevant event handler (Click, Alert, etc.) to call the global script, which passes back an IPFDocumentl object. Following the global script call, the document could, for example, be stored in a hidden component. The PerformClick() method could then be called on that component, so the document is downloaded to the user’s computer. One way to implement this is to add code to the applicable server-side script, which places a clickable image in the top header region of a grid and indicates that clicking the image will download the collection.

This is the process for our example:

  1. In the Portal Manager, select Content Development > Layouts and find the layout to which to add the clickable image.
  2. Right-click the Server Script column for this layout and select Edit Server Script. This downloads the script to a temporary folder on your hard drive and opens the script in Visual Studio.
  3. With the script open in Visual Studio, find its location in the temporary folder on your hard drive. This is necessary to set the location for available global scripts in the Visual Studio File Explorer.

    In the Solution Explorer, right-click your project and select Open Folder in File Explorer.This shows the location of all global scripts available to reference.

  4. Add the global script as a reference in this project.
    1. In the Solution Explorer, right-click your project again and select Add > Reference.
    2. In the pop-up window, click Browse and navigate to the location found in step 3.
    3. Select IPFExportCollectionGlobal.dll.
    4. Click Save All.
  5. Use the reference in your code.

    The IPFExportCollectionGlobal.dll resides in the IPFGlobalScripts namespace, so you can call it by writing this:

    
    IPFGlobalScripts.Spreadsheet.CreateCSVSpreadsheet(...)
    

    The code could look like this:

    
    Public Sub OnClick(ByVal context As GridClickContextI, ByVal parms 
    As GridClickParmsI)
    
    If parms.Name = "DownloadButton" Then
    Dim propertiesList As System.Collections.Generic.List(Of String) = 
    New System.Collections.Generic.List(Of String)(New String() 
    {"CoNum", "PoNum", "OrderDate", "SalesTax", "Price", 
    "SiteSiteName"})
    

    (The lines above define which properties in the collection should be exported to CSV.)

    
    Dim labelsDictionary As System.Collections.Generic.Dictionary(Of String, 
    String) = New System.Collections.Generic.Dictionary(Of String, String)()
    labelsDictionary.Add("CoNum", "Order")
    labelsDictionary.Add("PoNum", "Purchase Order")
    labelsDictionary.Add("OrderDate", "Order Date")
    labelsDictionary.Add("SalesTax", "Tax")
    labelsDictionary.Add("SiteSiteName", "Site")
    

    (The lines above provide header labels to identify columns in the CSV file. If a column is not given a label, the name of the property is used.)

    
    Dim document As IPFDocumentI = 
    IPFGlobalScripts.Spreadsheet.CreateCSVSpreadsheet (
    context.SessionContext, _
    context.Items, _
    "OrderStatusCollection.csv", _
    properties:=propertiesList, _
    labels:= labelsDictionary)
    

    (Calling the global script method creates an IPFDocumentl with the value for the given properties of the given collection, with the given labels for each property, all in CSV format.)

    
    context.UnboundComponents("HiddenLink").SetValue(document)
    context.UnboundComponents("HiddenLink").FieldDisplayFormatAttributes.IPFTe
    xt.PerformClick()
    

    (These lines put the document inside a hidden component, with PerformClick() set on the component. This automatically downloads the document to the browser. And in this case, HiddenLink would be the name of a component with the IPFDocument formatter and the ipf-gone style.)

    End If
    End Sub