Click here to Skip to main content
15,885,914 members
Articles / Web Development / ASP.NET

SharePoint Report Viewer Page Hacks

Rate me:
Please Sign up or sign in to vote.
4.93/5 (10 votes)
22 Sep 2009CPOL6 min read 63.6K   564   20  
An alternate way to view Microsoft Reporting Services reports in SharePoint.
//
// RSViewer.cs - http://www.dataquadrant.com
// Copyright (c) 2009
// by Data Quadrant, Inc. 
// contact: Marian Dumitrascu md@dataquadrant.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//
//


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using Microsoft.ReportingServices.SharePoint.UI.WebParts;


namespace DataQ.SharePoint {

  public class RSViewer : LayoutsPageBase {

    protected ReportViewerWebPart m_sqlRsWebPart;
    protected Literal LiteralPageTitle;
    protected Label LabelPageTitleInTitleArea;


    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);

        SPWeb Web = SPContext.Current.Web;
        string strUrl =
            Web.ServerRelativeUrl + "/_catalogs/masterpage/default.master";

        this.MasterPageFile = strUrl;
    }


    protected override void OnLoad(EventArgs e) {      
      // add code here

        if (!Page.IsPostBack)
        {
            SPWeb web = SPContext.Current.Web;
            SPList list;
            SPListItem report = null;
            if (Request["ItemId"] != null && Request["ListId"] != null)
            {
                list = web.Lists[new Guid(Request["ListId"].ToString())];
                if (list != null)
                {
                    report = list.Items.GetItemById(Convert.ToInt32(Request["ItemId"])); 
                }
            }
            else if (Request["report"] != null)
            {
                report = web.GetListItem(Request["report"].ToString());
            }

            if (report != null)
            {

                //set the window title
                LiteralPageTitle.Text = report.Title;

                //set the title in the page
                LabelPageTitleInTitleArea.Text = report.Title;
                
                //set the path to the report
                m_sqlRsWebPart.ReportPath = SPContext.Current.Site.MakeFullUrl(report.File.ServerRelativeUrl);
                
                ReportParameterDefaultCollection parameters; // = new ReportParameterDefaultCollection();
                parameters = m_sqlRsWebPart.OverrideParameters;

                foreach (string urlParam in Request.QueryString.AllKeys)
                {
                    //we have to prefix report params with something so we know 
                    //they are report parameters, and they are destined to be passed to the report
                    if (urlParam != null && urlParam.StartsWith("p_"))
                    {
                        string paramName = urlParam.Substring(2);
                        string paramValue = Request[urlParam].ToString();
                        Microsoft.Reporting.WebForms.ReportParameter p = new Microsoft.Reporting.WebForms.ReportParameter(paramName, paramValue);
                        parameters.Add(p);
                    }
                } //foreach (string urlParam in Request.QueryString.AllKeys)
            } //if (report != null)
        } //if (!Page.IsPostBack)
    } //OnLoad



  } //class RSViewer
} //namespace DataQ.SharePoint

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Data Quadrant, Inc.
United States United States
Seek, create and combine solutions in the realm of Information Portals, Business Inteligence and Business Automation. With a focus on SharePoint. (Houston, TX)

Comments and Discussions