1

There are topics that sound the same, but there's no answer there, really.

I'm trying to export report results into a csv file programmatically from a C# app.

Using Analitics API I'm able to get the report ID that I need. Then I build the following url:

"https://na9.salesforce.com/00OE0000001L8cUMAS/?export=1&enc=UTF-8&xf=CSV"

Then tried to do the following:

WebClient wc = new WebClient();               
wc.Headers["Authorization"] = "Bearer " + sForceService.SessionHeaderValue.sessionId;                                
wc.DownloadFile(URL, Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "report.csv"));

But this saves not the report but the following html file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<script>
if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://login.salesforce.com/?ec=302&startURL=%2F00OE0000001L8cUMAS%2F%3Fexport%3D1%26xf%3DCSV%26enc%3DUTF-8'); }  else 
if (window.location.replace){ 
window.location.replace('https://login.salesforce.com/?ec=302&startURL=%2F00OE0000001L8cUMAS%2F%3Fexport%3D1%26xf%3DCSV%26enc%3DUTF-8');
} else {;
window.location.href ='https://login.salesforce.com/?ec=302&startURL=%2F00OE0000001L8cUMAS%2F%3Fexport%3D1%26xf%3DCSV%26enc%3DUTF-8';
} 
</script>

</head>


</html>



<!--
...................................................................................................
...................................................................................................
...................................................................................................
...................................................................................................
-->

Looks like some redirection javascript, so I tried to do WebClient.DownloadFile, passing this redirection url, and this just returns the standard salesforce login screen saying that I need to log in (although, I'm always passing an active sessionId in the header). I don't believe there's no way to programmatically export a report, if I can do it in browser, I should be able to do it from code, I'm just missing something basic... Or is there a way to export the report using Analytics API? I haven't found anything about export in the Analytics API developer guide.

2
  • I have exactly the same need ... and have done the same than you with the same results !!! Did you find any solution ? I've done a lot of search and tests (using curl requests in php) but without success ... I also believe that "there's no way to programmatically export a report, if I can do it in browser" ... but how to proceed ??? Thanks for your help ... Fabrice
    – Fabrice
    Oct 8, 2014 at 2:27
  • Fabrice, please see the accepted answer below, I'm successfully exporting reports from two different platforms using this technique.
    – taralex
    Oct 28, 2014 at 19:03

1 Answer 1

5

If you're building the url to standard SF page (/<reportId>?<some params>- you're not using Analytics API. I had the same question some time ago (maybe we should vote to merge?):

screen scrape Salesforce with REST GET call from Apex

As for real export using Analytics API ( /services/data/<latest API version>/analytics/reports/<report ID>...) - there's not a single mention of word "export" or "CSV" in the PDF so I wouldn't have too high hopes.

You'd need to obtain the "factMap" values and craft the CSV yourself in the code.

5
  • I'm doing this already, however this still limits me to 2000 records per report. The only way to get the complete report is to export it, but I don't see any way to do this in Analytics API. That's why I thought that there should be a way to 'trick' salesforce to believe I'm a browser and download the darn file. I'm just not that good with http to figure that out on my own, so thought someone must have done it already.
    – taralex
    Jan 23, 2014 at 0:26
  • Is this C#? Have you tried setting the cookie as explained in the other question I linked to? I don't know much about C# but seems it's not rocket science to set cookies: stackoverflow.com/questions/1777221/…
    – eyescream
    Jan 23, 2014 at 7:30
  • It actually worked, the solution from your link! I missed it initially, but that's it, the way to pass security token to the sfdc web interface is using the "cookie" value in the header!
    – taralex
    Jan 29, 2014 at 23:40
  • @taralex: I would really like to know how you get it working? I have tried using Cookie header but I am getting the same HTML page as in your question. Any help will be very useful. Dec 3, 2014 at 16:52
  • A couple of things to remember here: 1. When setting up your remote access app in Salesforce you need to enable the "Access my data via Web" permission. 2. Pass the session id in the header as follows: WebClient wc = new WebClient(); wc.Headers["Cookie"] = "sid=" + sessionId; p.s. the example is for C#, add your header info to the http request depending on the language you're using.
    – taralex
    Dec 4, 2014 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .