Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: Accessing SQL Server Reporting Services from C#

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: Mon, Aug 19, 2013, 12:31 PM


Overview



Procedure

1. Navigate to Project menu > Add Service Reference > Advanced button > Add Web Reference button

2. In the URL field, specify the URL for the SSRS instance's web service (e.g., http://myserver/myinstance/reportservice2010.asmx)

3. In the Web Reference Name field, specify "SsrsService" and click the Add Reference button.

4. Add the ReportProvider class (see below) to your solution.

5. In the ASPX code-behind code for the page that contains the ReportViewer control, add the RenderReport method (see below).

6. Add the following settings to your web.config file (in the appSettings section).

<!-- SSRS Settings -->
<add key="SsrsServerRoot" value="http://localhost" />
<add key="SsrsWebServicePath" value="/ReportServer/ReportService2010.asmx" />
<add key="SsrsReportServerPath" value="/ReportServer" />
<add key="SsrsDefaultCredentials" value="false" />
<add key="SsrsUserName" value="user" />
<add key="SsrsPassword" value="password" />
<add key="SsrsDomain" value="domain" />

Reusable Code

ReportProvider Class

public class ReportProvider
{
    /*- Private Fields ----------------------------------------------------------------------*/
    //ReportingService2010 _webService;

    /*- Constructor -------------------------------------------------------------------------*/
    public ReportProvider()
    {
        bool defaultCredentials = (ConfigurationManager.AppSettings["SsrsDefaultCredentials"] 
                                       == "true");

        UserName = ConfigurationManager.AppSettings["SsrsUserName"];
        Password = ConfigurationManager.AppSettings["SsrsPassword"];
        Domain = ConfigurationManager.AppSettings["SsrsDomain"];
        ReportServerRoot = ConfigurationManager.AppSettings["SsrsServerRoot"];
        ReportServerPath = ConfigurationManager.AppSettings["SsrsReportServerPath"];
        ReportServerUrl = new System.Uri(ReportServerRoot + ReportServerPath);

        //var url = ReportServerRoot + ConfigurationManager.AppSettings["SsrsWebServicePath"];
        //_webService = new ReportingService2010();
        //_webService.Url = url;

        //if (defaultCredentials)
        //    _webService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
        //else
        //    _webService.Credentials = new System.Net.NetworkCredential(username, password, domain);
    }

    /*- Private Properties ------------------------------------------------------------------*/
    private string UserName { get; set; }
    private string Password { get; set; }
    private string Domain { get; set; }
    private string ReportServerRoot { get; set; }
    private string ReportServerPath { get; set; }

    /*- Public Properties -------------------------------------------------------------------*/
    public System.Uri ReportServerUrl { get; set; }

    public ReportCredentials Credentials
    {
        get
        {
            ReportCredentials rc = new ReportCredentials(UserName, Password, Domain);
            return rc;
        }
    }

    /*- Public Methods ----------------------------------------------------------------------*/
    //public List<CatalogItem> GetReportList(string folder, bool includeBlank)
    //{
    //    List<CatalogItem> result = new List<CatalogItem>();

    //    if (folder.Length > 0 && !folder.StartsWith("/"))
    //        folder = "/" + folder;

    //    if (includeBlank)
    //        result.Add(new CatalogItem { Name = "(select)", Path = "" });

    //    List<CatalogItem> items = _webService
    //        .ListChildren(folder, false)
    //        .Where(o => o.TypeName == "Report")
    //        .ToList<CatalogItem>();

    //    result.AddRange(items);

    //    return result;
    //}


    /*- Nested Classes ----------------------------------------------------------------------*/
    [Serializable]
    public class ReportCredentials : Microsoft.Reporting.WebForms.IReportServerCredentials
    {
        string _userName, _password, _domain;

        public ReportCredentials(string userName, string password, string domain)
        {
            _userName = userName;
            _password = password;
            _domain = domain;
        }
        public System.Security.Principal.WindowsIdentity ImpersonationUser
        {
            get
            {
                return null;
            }
        }
        public System.Net.ICredentials NetworkCredentials
        {
            get
            {
                return new System.Net.NetworkCredential(_userName, _password, _domain);
            }
        }
        public bool GetFormsCredentials(out System.Net.Cookie authCoki, out string userName, 
            out string password, out string authority)
        {
            userName = _userName;
            password = _password;
            authority = _domain;
            authCoki = new System.Net.Cookie(".ASPXAUTH", ".ASPXAUTH", "/", "Domain");
            return true;
        }

    }

}

RenderReport Method

Place this code in your ASPX code-behind for the page that has a ReportViewer control with the ID uxReportViewer.

private void RenderReport(string reportName)
{
    var rp = new ReportProvider();
    var rv = uxReportViewer;
    var sr = rv.ServerReport;
    var rc = rp.Credentials;
    rv.ShowCredentialPrompts = false;
    sr.ReportServerCredentials = rc;
    rv.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
    sr.ReportServerUrl = rp.ReportServerUrl;
    sr.ReportPath = reportName;
    sr.Refresh();
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.