Skip to content

Instantly share code, notes, and snippets.

@JRondeau16
Last active August 29, 2015 14:16
Show Gist options
  • Save JRondeau16/2910469fc93a8b68dcfc to your computer and use it in GitHub Desktop.
Save JRondeau16/2910469fc93a8b68dcfc to your computer and use it in GitHub Desktop.
ExportCommand
using System;
using System.IO;
using System.Text;
using System.Web;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Form.Core.Configuration;
using Sitecore.Form.Core.Utility;
using Sitecore.Forms.Core.Commands.Export;
using Sitecore.IO;
using Sitecore.Shell.Framework.Commands;
using Sitecore.Web.UI.Sheer;
using Sitecore.Web.UI.WebControls;
using Sitecore.Web.UI.XamlSharp.Continuations;
using System.Collections.Specialized;
namespace MyProject.Web.Commands
{
public class MyCommand : ExportToXml
{
public override void Execute(CommandContext context)
{
Assert.ArgumentNotNull((object)context, "context");
string str1 = context.Parameters["items"];
string str2 = context.Parameters["mode"];
if (string.IsNullOrEmpty(str1) && (string.IsNullOrEmpty(str2) || str2 != "all") || str2 == "all" && int.Parse(context.Parameters["count"]) == 0)
{
SheerResponse.Alert(ResourceManager.Localize("SELECT_ITEM_FIRST"));
}
else
{
Item obj = context.Items[0];
NameValueCollection parameters = new NameValueCollection();
parameters["id"] = obj.ID.ToString();
parameters["la"] = obj.Language.Name;
parameters["vs"] = obj.Version.Number.ToString();
parameters["db"] = obj.Database.Name;
parameters["items"] = context.Parameters["items"] ?? string.Empty;
parameters["mode"] = context.Parameters["mode"] ?? string.Empty;
parameters["fields"] = context.Parameters["fields"];
parameters["view"] = context.Parameters["view"];
parameters["tempfile"] = WebUtil.GetTempFileName();
//Do necessary work to generate file of your choosing, saving the contents into parameters["tempfile"]
ContinuationManager.Current.Start((ISupportsContinuation)this, "ExportContents", new ClientPipelineArgs(parameters));
}
}
public void ExportContents(ClientPipelineArgs args)
{
Assert.ArgumentNotNull(args, "args");
var filePath = args.Parameters["tempfile"];
HttpContext.Current.Session["exportcontent"] = File.ReadAllBytes(filePath); //may need to change depending on the type of file
HttpContext.Current.Session["filename"] = "myFileName.myExtension"; //fill in with your filename and extension
HttpContext.Current.Session["contentType"] = "myContentType"; //fill in with your content type
var stringBuilder = new StringBuilder();
stringBuilder.Append("var iframe=document.createElement('iframe');");
stringBuilder.Append("iframe.src='/path/to/MyExportControl.aspx';"); //This control will do the work of streaming content back to the browser
stringBuilder.Append("iframe.width='1';");
stringBuilder.Append("iframe.height='1';");
stringBuilder.Append("document.body.appendChild(iframe);");
SheerResponse.Eval(((object)stringBuilder).ToString());
try
{
FileUtil.Delete(filePath); //delete tempfile
}
catch (Exception ex)
{
Log.Error(string.Format("An unexpected error occured when exporting file: {0}", ex.Message)
, ex
, this);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment