Monday, 4 January 2010

ReportViewer in WebPart (SharePoint)

Using ReportViewer control inside SharePoint Webpart is bit tricky and challenging.
Below are the steps to make it happen.

1. web.config (of SharePoint) changes (Extra space has been included after > and < to make it appear in blog)

add below line to < httpHandlers > section

< add verb="*" path="Reserved.ReportViewerWebControl.axd" type = "Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" / >


and below line to < assemblies >

< add assembly="Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" / >

2. Add Report (.rdlc) and DataSet (.xsd) files to Solution


Sample Code

------------------------------------------------------------
using System;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

namespace ReportWebpart
{
public class ReportsWP : WebPart
{
Label lblTime;
ReportViewer ReportViewer1;
TextBox tb1;
Button btn;

protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
lblTime = new Label();
lblTime.Text = "Enter Plant No.";

tb1 = new TextBox();

btn = new Button();
btn.Text = "Refresh Report";
btn.Click += new EventHandler(btn_Click);

ReportViewer1 = new ReportViewer();
ReportViewer1.Width = Unit.Percentage(80);
ReportViewer1.EnableViewState = true;
ReportViewer1.LocalReport.ReportPath = @".\Report1.rdlc"; // Copy the rdlc file to root of IIS
ReportViewer1.ProcessingMode = ProcessingMode.Local;
ReportViewer1.ID = "rprtViewer1";

this.Controls.Add(lblTime);
this.Controls.Add(tb1);
this.Controls.Add(btn);
this.Controls.Add(ReportViewer1);
}
catch (Exception ex)
{
string k = ex.Message;
}
}

void btn_Click(object sender, EventArgs e)
{
DataTable newDT = GetTable();
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1_Employee", newDT));//Name of Dataset (.xsd file)+ '_' + table name
ReportViewer1.ID = "rprtViewer1";
ReportViewer1.DocumentMapCollapsed = true;
ReportViewer1.LocalReport.Refresh();
}

private DataTable GetTable()
{
//FRAME THE QUERY AS NEEDED. THE FIELDS RETURN SHOULD MATCH WITH REPORT'S DESIGN TIME FIELDS
}
}
}
----------------------------------------------------------

NOTE: If you are getting following error while accessing the WebPart page containing ReportViewer

Microsoft.SharePoint.Portal.Analytics.UI.ReportViewerMessages, Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c does not implement IReportViewerMessages or could not be found


Try either of following to fix it

1In web.config of SharePoint, comment the following line under
1. <appsettings >
< ! -- < add key="ReportViewerMessages" value="Microsoft.SharePoint.Portal.Analytics...... / > -- >

or

2.  Add  following to < appsettings >
< remove key="ReportViewerMessages" >


------------------------------------------------------

7 comments:

Anonymous said...

Thank you for the valuable information here.
I've followed your instruction and got this error "The Report Viewer Web Control HTTP Handler has not been registered in the application's web.config file. Add to the system.web/httpHandlers section of the web.config file.

at Microsoft.Reporting.WebForms.ReportViewer.OnInit(EventArgs e) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.AddedControl(Control control, Int32 index) at Microsoft.PRC.SharePoint.QuickPart.CreateChildControls()
"
I've edited the web.config under c:\inetpub\wwwroot and C:\inetpub\wwwroot\wss\VirtualDirectories\80. still got this error, any idea what's wrong here?

Anonymous said...

Thank you for the post. Can you expand on how to ‘Add Report (.rdlc) and DataSet (.xsd) files to Solution’?

Manu said...

Thanks.Saved my day...

Unknown said...

had same Problem. The listed solution had no effekt. As I tried to comment out the existing entries in web.config:




then it worked. There was a conflict with the handlers of the Sharepoint ReportViewer Webpart. Hope that helps.

Unknown said...

Here The Tags i meant

<add name="ReportViewerWebPart" verb="*" .....

<add name="ReportViewerWebControl" verb="*" ....

SAV said...

i am getting a dll conflict error when i use Reportviewer in sharepoint webpart. it's conflicting with ReportViewerWebpart class in Microsoft.ReportingServices.SharePoint.UI.WebParts. how can i solve this?

Vivek said...

@SAV i have the same problem. Did you find any solution then please post.
thanks a lot.