Navigatie

Contact

Send mail to the author(s) E-mail

View Richard Soeteman's profile on LinkedIn

RSS 2.0 | Atom 1.0 | CDF

Archief

Categorieën

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Sign In

Zoeken

# Tuesday, December 20, 2011
Tuesday, December 20, 2011 1:57:00 PM (GMT Standard Time, UTC+00:00) ( MemberExport | Package | Umbraco )

I’ve just released MemberExport 2.0. Apart from another awesome logo by Arnold Visser.I’ve added some functionality as well.

memberexport100x100

Native Excel export

MemberExport 1.x could only export to a csv file. This is great for most of us but when you select a wrong separator and want to open the csv file in Excel directly it will probably looks a bit messy like the example below.

wrongformat

MemberExport 2.0 Pro let’s you select the export option csv or Excel file. When you select Excel file it will export the data to an Excel file instead of csv and all the data is formatted correctly.

image

Export provider

I don’t like closed systems, all of my packages are open for extension. When creating the Excel export option I wanted the export option to be a provider model. Not only to make this task easier for myself but also so you can use MemberExport to connect to your custom ERP system. Implementing an export provider is pretty straight forward. The example below is the implementation of the CSV Provider.

CSVProviderUI

The CSVProviderUI class is the class which is responsible for the UI part of the provider. Add a reference to the MemberExport.Library dll and derive from ExportProviderUIBase class. Implement to following methods:

  • OnInit, create the UI controls
  • Initialize, initialize the UI options based on the stored export provider settings. In this case exportProvider will hold the text delimiter and text indicator options.
  • GetOptions will return the export provider settings so we use those settings when exporting records
  • The name property will return the name of the provider stored in the Export as dropdownlist when selecting an export provider.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using MemberExport.Resources.Helpers;
using umbraco.uicontrols;
 
namespace MemberExport.Library.ExportProviders.CSV
{
    /// <summary>
    /// Renders the Layout for the CSV Provider
    /// </summary>
    public class CSVProviderUI : ExportProviderUIBase, INamingContainer
    {
        protected DropDownList _csvTextIndicatorDropDownList = new DropDownList();
        protected DropDownList _csvSVSeperatorDropDownList = new DropDownList();
 
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
 
            _csvSVSeperatorDropDownList.Items.Add(new ListItem(";", ";"));
            _csvSVSeperatorDropDownList.Items.Add(new ListItem(",", ","));
 
            _csvTextIndicatorDropDownList.Items.Add(new ListItem(ResourceHelper.Current.GetStringResource("CSVNoTextIndicator"), ""));
            _csvTextIndicatorDropDownList.Items.Add(new ListItem("\"", "\""));
            _csvTextIndicatorDropDownList.Items.Add(new ListItem("\'", "\'"));
 
            PropertyPanel delimiterPropery = new PropertyPanel();
            delimiterPropery.Text = ResourceHelper.Current.GetStringResource("CSVSeperatorProperty");
            delimiterPropery.Controls.Add(_csvSVSeperatorDropDownList);
 
            PropertyPanel stringIndicatorPropery = new PropertyPanel();
            stringIndicatorPropery.Text = ResourceHelper.Current.GetStringResource("CSVTextIndicatorProperty");
            stringIndicatorPropery.Controls.Add(_csvTextIndicatorDropDownList);
 
            Controls.Add(delimiterPropery);
            Controls.Add(stringIndicatorPropery);
        }
 
        /// <summary>
        /// Sets the export options (delimiter and string indicator).
        /// </summary>
        /// <param name="exportProvider">The export provider.</param>
        public override void Initialize(ExportProviderBase exportProvider)
        {
            CSVProvider csvProvider = exportProvider as CSVProvider;
            if (csvProvider != null)
            {
                _csvSVSeperatorDropDownList.SelectedValue = csvProvider.Delimiter;
                _csvTextIndicatorDropDownList.SelectedValue = csvProvider.StringIndicator;
            }
        }
 
        /// <summary>
        /// Returns the selected export options (delimiter and string indicator).
        /// </summary>
        /// <returns></returns>
        public override ExportProviderBase GetOptions()
        {
            CSVProvider csvProvider = new CSVProvider();
            csvProvider.Delimiter = _csvSVSeperatorDropDownList.SelectedValue;
            csvProvider.StringIndicator = _csvTextIndicatorDropDownList.SelectedValue;
            return csvProvider;
        }
 
        /// <summary>
        /// Return the name of the provider. this will be displayed in the selectbox
        /// </summary>
        /// <value>The selected name.</value>
        public override string Name
        {
            get { return "CSV file"; }
        }
    }
}

CsvProvider

The second part we need implement is the provider itself which handles the actual export process. Create a new class and derive from ExportProviderBase.  This will give the following methods/properties:

  • Export, responsible for the export process
  • Icon, will be displayed when opening the saved exports tree

In this implementation we’ve added the StringIndicator and Delimiter options, which gets set from the UI. The export method will give a list of columns to export and all records using an IRecordReader.  The rest of the code is implementation specific. Two important things to know when exporting records.  When assigning a value always use the ValueParser factory

ValueParser.Parse(reader.GetObject(column.Alias), column));

To write the file to the browser either use the WriteResponseString (for stringvalues ) or WriteResponseBytes (for a byte array) methods. Both methods need the Contenttype (Mimetype) and fileExtension, in this case csv.

using System;
using System.Collections.Generic;
using MemberExport.Library.Csv;
using MemberExport.Library.Members;
using MemberExport.Library.Types;
 
namespace MemberExport.Library.ExportProviders.CSV
{
    /// <summary>
    /// CSV (Default) implementation of MemberExport
    /// </summary>
    [Serializable()]
    public class CSVProvider : ExportProviderBase
    {
        /// <summary>
        /// Exports the data to a csv file
        /// </summary>
        /// <param name="columns">Collection of column info</param>
        /// <param name="reader">The actual data to export</param>
        public override void Export(List<Types.MemberField> columns, umbraco.DataLayer.IRecordsReader reader)
        {
            //Use a csv writer object
            CsvWriter writer = new CsvWriter(Delimiter, StringIndicator);
 
            //Add columns to export
            foreach (MemberField field in columns)
            {
                writer.Columns.Add(field.Text);
            }
 
            //Add data to export
            while (reader.Read())
            {
                //Get values for the current row
                List<object> values = new List<object>();
                foreach (MemberField column in columns)
                {
                    values.Add(ValueParser.Parse(reader.GetObject(column.Alias), column));
                }
 
                //write values to csvwriter;
                writer.Add(values.ToArray());
            }
            //Write response to the browser
            WriteResponseString(writer.Parse(), "application/excel", "csv");
        }
 
        /// <summary>
        /// Gets or sets the string indicator.
        /// </summary>
        /// <value>The string indicator.</value>
        public string StringIndicator { get; set; }
 
        /// <summary>
        /// Gets or sets the delimiter.
        /// </summary>
        /// <value>The delimiter.</value>
        public string Delimiter { get; set; }
 
        /// <summary>
        /// Returns the icon that will be displayed in the saved tree
        /// </summary>
        /// <value>The icon.</value>
        public override string Icon
        {
            get
            {
                return "csvexport.gif";
            }
        }
    }
}

That’s all, compile and add to the bin folder of your Umbraco install and you can use the provider!

ValueParsers

By default MemberExport exports the stored value from  the database. When this value is an Id value you might want to export the text value instead. This can be achieved using a value parser. By default MemberExport comes with value parsers for the following datatypes :

  • Checkbox
  • Dropdownlist
  • MNTP
  • MuiltipleDropdownlist
  • Radiobox
  • Ultimatepicker

In the example below we will write a value parser for the True/false datatype. By default values will be exported as 1/0. We want the values to be exported as yes/no for this example.

Before you ask, this isn’t in the core because some people want 0/1, some true/false, some yes/no etc.

First we need to add a reference to the MemberExport.Library dll. Then we can create a class that implements the IValueParser interface

  public class TrueFalseParser : IValueParser
    {
        /// <summary>
        /// Returns the Datatype GUID of the true/false Render Control.
        /// </summary>
        public Guid DataTypeId
        {
            get { return new Guid("38b352c1-e9f8-4fd8-9324-9a2eab06d97a"); }
        }
 
        /// <summary>
        /// Converts the 0/1 to a normal yes/no string.
        /// </summary>
        /// <param name="memberfieldInfo">The memberfield info.</param>
        /// <param name="value">The value.</param>
        /// <returns>The yes/no string</returns>
        public object Parse(Library.Types.MemberField memberfieldInfo, object value)
        {
            return string.Format("{0}", value) == "1" ? "yes" : "no";
        }
    }

The DataTypeId property needs to return the GUID which is displayed on the datatype edit screen.

image

The Parse method will be called when a true/false property value is exported. In the above example we transform the value to yes/no.
The following information about the exported field is available in the memberFieldInfo variable:

  • DataTypeId. The GUID which is displayed on the datatype edit screen
  • DatatypeNodeId. The node id  of the Datatype.
  • Alias. The Property alias.
  • Text. The Property text.

To use the Value Parser all we need to compile the project and add the dll to the bin folder of the Umbraco install. Then it will be picked up automatically and values will be exported as yes/no. In the example below the Active column is exported as true/false value using the Value Parser.

image

Download and install

You can download the MemberExport package from the Umbraco Deli. Just install the package, also when you want to update your current version. When you are a Pro user make sure the license file is still in your bin folder to unlock the Pro features.

Happy exporting!