Salesforce : How to fetch report as json using C#

We recently got a need to fetch a report from Salesforce and display the same as interactive calendar. Our existing work on integration Salesforce REST API proved fruitful to fulfill the need. The solution involves leveraging Force.com SOAP API to make connection in order to get access token followed by making reports and dashboards API. Salesforce send report in JSON which can be easily parsed and rendered using interactive front end code.

STEP 1: AUTHENTICATE AND CREATE SALESFORCE API SESSION

You can now use objects of classes “SforceService” and “LoginResult” to connect to Salesforce API.

In order to access the Sforce API, we need to provide our login email, password, and the security token. Additionally, we need to generate and download appropriate WSDL which would be later used to make appropriate API calls from our code. Check existing blog Integrate Salesforce with .NET application for detail information.

Once connection with established with Sforce, we can fetch SessionId (or Access Token) to make further calls.

STEP 2: MAKE WEB REQUEST FOR THE REPORT

Provided we have access token, we can make the final request for the report we want to execute and fetch from Force.com. Sforce exposes below API Url that can be used to send the report request:

https://c.na48.visual.force.com/services/data/v29.0/analytics/reports/[ReportId]?includeDetails=true
string ReportUrl = "https://c.na48.visual.force.com/services/data/v29.0/analytics/reports/" + YOUR_REPORT_ID + "?includeDetails=true";

To make the web request we also need to push the generated access token (or session id) onto the header and now we can make the request to receive the report data.

public string GetReportAsJsonData(string ReportUrl)
{
	OpenConnection();
	WebClient wClient = new WebClient();
	wClient.Headers.Add("Authorization", "Bearer " + SessionId);
	return wClient.DownloadString(ReportUrl);
}

Refer blog Integrate Salesforce with .NET application for details on OpenConnection() and related code.

(Visited 883 times, 1 visits today)