How to execute the "Export table" menu item programatically in C#?

Hello, I am trying to figure out if it's possible to programmatically call the "Export table" menu item that appears when the user right-clicks on a Table visualization on the WebPlayer.

The reason I am trying to do that is I am trying to develop a CustomTool C# extension to download the data that is currently shown in a Map chart. To do that, the current implementation converts a Map chart to a Table so I can call its ExportData method. However, that method requires a FileStream which requires a path. If I use the current implementation the logic tries to save the resulting file on the WebPlayer's server and not stream it down to the user's browser. 

So I am thinking if there is a way to execute the "Export table" context menu item that is already build-in to Spotfire WebPlayer from code. The method downloads the resulting Excell file to the user (i.e. normal browser download behaviour). 


Current snippet of code in my CustomTool<VisualContent>:

protected override void ExecuteCore(VisualContent map)

	var doc = map.Context.GetAncestor<Document>();
	AggregatedTransactionHandle transaction = doc.Transactions.BeginAggregatedTransaction();

		var visual = map.Visual;
		visual.TypeId = VisualTypeIdentifiers.Table;
		var table = visual.As<TablePlot>();

		var dm = doc.Data.Markings.DefaultMarkingReference;

		var pathWithEnv = @"%UserProfile%\Desktop\ExportData.xls";
		var filePath = Environment.ExpandEnvironmentVariables(pathWithEnv);

		if (!Directory.Exists(filePath))

		using (FileStream stream = File.Open(filePath, FileMode.Create))
			table.ExportData(DataWriterTypeIdentifiers.ExcelXlsDataWriter, stream);



(1) Answer