Search results

Load Syncfusion Report Server reports

You can render the Syncfusion Report Server hosted reports in the Report Viewer easily without creating a Web API service. Syncfusion Report Server provides the built-in Web API service that helps you to display the server reports.

The Report Viewer requires the ServiceAuthorizationToken to connect and download the items from the Syncfusion Report Server.

To load the Report Server reports, follow these steps:

  • Create a token for users by using the server RESTful API. The following code is used to generate the token.
    public partial class ReportViewerController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.ServiceAuthorizationToken = this.GenerateToken("http://reportserver.syncfusion.com/", "guest", "demo");
            return View();
        }

        public IActionResult Error()
        {
            return View();
        }

        public string GenerateToken(string serverUrl, string userName, string password)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(serverUrl);
                client.DefaultRequestHeaders.Accept.Clear();

                var content = new FormUrlEncodedContent(new[]
                {
                new KeyValuePair<string, string>("grant_type", "password"),
                new KeyValuePair<string, string>("username", userName),
                new KeyValuePair<string, string>("password", password)
                  });

                var result = client.PostAsync("/api/Token", content).Result;
                string resultContent = result.Content.ReadAsStringAsync().Result;
                var token = JsonConvert.DeserializeObject<Token>(resultContent);

                return token.token_type + " " + token.access_token;
            }
        }
    }

    public class Token
    {
        public string access_token { get; set; }

        public string token_type { get; set; }

        public string expires_in { get; set; }

        public string userName { get; set; }

        public string serverUrl { get; set; }

        public string password { get; set; }
    }
  • Set the Syncfusion Report Server built-in service URL to the report-service-url property.

  • Assign the created token to service-authorization-token and report-path of a report deployed on the server. You can use the following complete code in your CSHTML page.

    <sf-report-viewer id="viewer" report-service-url="http://reportserver.syncfusion.com/ReportService/api/viewer" report-path="/Sample Reports/Company Sales" service-authorization-token="@ViewBag.ServiceAuthorizationToken" ></sf-report-viewer>

You can also load the report using GUID instead of report location. Set the GUID of the report in the report-path property as report-path: ‘91f24bf1-e537-4488-b19f-b37f77481d00’.