13

How to add a button to the sitecore content editor to download data as a CSV fil...

 3 years ago
source link: https://blog.coates.dk/2015/01/06/how-to-add-a-button-to-the-sitecore-content-editor-to-download-data-as-a-csv-file/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to add a button to the sitecore content editor to download data as a CSV file

In this blog I will explain how to add a button to the content editor and download data as an CSV file. For this example I will assume that we want to download the data for a given web form for marketers form as a CSV file.

Adding the button

the button

Contextual Ribbons

You may not have noticed but this is a contextual ribbon that is it is only shown when the item is based on the forms template, as it gives no meaning to have the download data as CSV added to all items.

First you need to swap to the core database, and navigate to the Contextual Ribbons item for the content editor i.e. /sitecore/content/Applications/Content Editor/Ribbons/Contextual Ribbons.

This is where all the ribbons that are only shown for specific item types are placed; within this folder there is a forms toolbar, which contains a form strip, which contains a forms chunk, which in turn contains the “Export Data as CSV” button item.

button item

The most important part of the button item is the click field, which defines the command name that will be raised when the button is clicked (I will explain how to hook this up in more detail later).

But how is a contextual ribbon associated with a given template – well that part is easy on the template item under the appearance section there is a ribbon field which can define a contextual ribbon or toolbar to be shown.

co0ntextual form

How to hook the button click up to the code that is to executed? 

We have to add a command item to the /app_config/commands.config file to bind the command name to the class that is going to provide the functionality.

<command name="forms:exportformdata" type="WFFM.SQLServer.SaveToDatabase.Infrastructure.Commands.ExportFormDataCommand, WFFM.SQLServer.SaveToDatabase" />

The class must inherit from the Sitecore Command class, and override the Execute function.

internal class ExportFormDataCommand : Command
{
public override void Execute(CommandContext context)
{
Assert.ArgumentNotNull(context, "context");
Assert.IsNotNull(context.Items, "context items are null");
Assert.IsTrue(context.Items.Length > 0, "context items length is 0");
Item contextItem = context.Items[0];
Assert.IsNotNull(contextItem, "First context item is null");
OpenNewWindow(contextItem.ID, contextItem.Name);
}
private void OpenNewWindow(ID id, string name)
{
Assert.ArgumentNotNull(id, "id");
UrlString url = new UrlString(Constants.Url.ExportFromDataPage);
url.Append(Constants.QueryString.Name.ItemId, HttpContext.Current.Server.UrlEncode(id.ToString()));
url.Append(Constants.QueryString.Name.ItemName, HttpContext.Current.Server.UrlEncode(name));
SheerResponse.Eval(string.Format("window.open('{0}');", url));
}
}

Its not very elegant, but the code generates a URL which points to a aspx page that will stream the data. The Eval method of the sheer response, allows me to execute JavaScript on the client which in this case opens a tab/window.

The aspx page doesn’t do that much it just sets the ContentType to “text/csv” and streams the data. If you want to see the complete implementation, you can download it from github

Hope this was helpful, Alan


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK