Sitecore SheerResponse.Download Error with XML

I had  a solution where we need to generate an XML file and download it from the Sitecore client. But when using SheerResponse.Download to download an XML file, it would add 2 elements to the end of the XML document (see the image below). The issue was found in sitecore 8.1 rev. 151207 (Update-1).

extra-elements

Sitecore have registered this a bug and provided a solution where I had to modify the main layout for the Sitecore client and use several support DLL’s, I decided not to do this.

Solution

I therefore did what I consider to be a nasty hack, but better than the alternative of modifying the Sitecore client, so please do not judge me for this!

SheerUI has a Eval function, which allows you to execute JavaScript within the Sitecore client. So I added an A tag, with a link to the file and then called click on the link.

internal void DownloadFile([NotNull] FileInfo file)
  {
    Assert.ArgumentNotNull(file, "file");
    var virtualPath = file.FullName;
    var rootPath = HostingEnvironment.MapPath("/");
    if (rootPath == null)
       return;

    virtualPath = virtualPath.Replace(rootPath, string.Empty);
    virtualPath = $"/{virtualPath.Replace("\\", "/")}";
    string js = $"var link=document.createElement('a');document.body.appendChild(link);link.href = '{virtualPath}';link.download='{file.Name}';link.click();";
    SheerResponse.Eval(js);
  }

2 thoughts on “Sitecore SheerResponse.Download Error with XML

  1. commodore73

    My Sitecore case on this bug is 473183, which contains instructions and a DLL from support (I haven’t tried it yet). The developer next to me said that this defect has been coming and going since Sitecore 7.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s