Using ReportViewer 10 control in SharePoint Foundation 2010 webpart.
I’m using new ReportViewer control in a SharePoint for rendering reports in local mode. My reports are using data from different SharePoint lists. ReportViewer control is then displayed in SharePoint webpart.
I decided to upgrade my previous solution to new version of Report Viewer control that shipped with new Visual Studio 2010 and is also available as redistribualbe package http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a941c6b2-64dd-4d03-9ca7-4017a0d164fd&displaylang=en
Why I decided to use new versionof reportViewer? Because it allow using reports in new format Reporting services 2008 . I’m using new Visual Studio 2010 with build in report designer from Report Services 2008
To use ReportViewer control in SharePoint webpart, they are following requirements to meet in a SharePoint configuration:
Enabled Session state for a web application:
1. Open IIS 7 manager, and find your web application.
2. Double click "Modules" in the IIS section.
3. Click "Add Managed Module..." on the right hand pane.
4. In the Add Managed Module dialog, enter "SessionState" or something like that for the name, and choose the following item from the dropdown:
System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Enable View State:
In a web.config file change
enableSessionState="true" enableViewState="true"….
Register ReportViewer control:
…
…
Source code of my webpart that use report viewer:
public class GenerateWebpart : System.Web.UI.WebControls.WebParts.WebPart
{
#region Members
private DataSet ds;
private ReportViewer reportViewer = null;
private Microsoft.Reporting.WebForms.ReportDataSource reportDataSource = null;
#region Child controls
//UI elements
private Button cmdGenerate;
private DropDownList cmbSelectReport;
#endregion Child controls
#endregion Members
#region Construction / Finalization
public GenerateWebpart()
{
this.ChromeType = PartChromeType.None;
}
#endregion Construction / Finalization
#region Overrides
///
/// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
///
protected override void CreateChildControls()
{
try
{
// Create and add the controls that compose the
// user interface of the Web Part.
this.SetPersonalizationDirty();
cmbSelectReport = new DropDownList();
cmbSelectReport.Items.Add(new ListItem("Report sales", "sales"));
cmbSelectReport.Items.Add(new ListItem("Report products", "products"));
this.Controls.Add(cmbSelectReport);
cmdGenerate = new System.Web.UI.WebControls.Button();
cmdGenerate.Text = "Generate";
cmdGenerate.Width = new Unit(75);
cmdGenerate.Click += new EventHandler(GenerateButton_Click);
this.Controls.Add(new LiteralControl(" "));
this.Controls.Add(cmdGenerate);
this.Controls.Add(new LiteralControl("
"));
"));
reportDataSource = new Microsoft.Reporting.WebForms.ReportDataSource();
reportViewer = new ReportViewer();
reportViewer.ProcessingMode = ProcessingMode.Local;
reportViewer.ShowRefreshButton = false;
reportViewer.EnableViewState = true;
reportViewer.AsyncRendering = false;
reportViewer.Width = this.Width;
this.Controls.Add(reportViewer);
}
catch (Exception ex)
{
Literal _ErrorMessageLiteral = new Literal();
_ErrorMessageLiteral.Text = ex.Message + "Inner exception: " + ex.InnerException.Message;
this.Controls.Clear();
this.Controls.Add(_ErrorMessageLiteral);
}
}
#endregion Overrides
#region UI event handlers
protected void GenerateButton_Click(object sender, EventArgs e)
{
if (cmbSelectReport != null)
{
GenerateReport();
}
}
#endregion UI event handlers
#region Private methods
private void GenerateReport()
{
switch (cmbSelectReport.SelectedValue)
{
case "sales":
ds = new SalesData();
ds = Helper.PopulateDataSet(ds);
reportDataSource = new Microsoft.Reporting.WebForms.ReportDataSource();
reportDataSource.Name = "MergedFlatData";
reportDataSource.Value = Helper.JoinDataSetByRelation(ds).Tables["MergedFlatData"];
this.reportViewer.LocalReport.ReportEmbeddedResource = "Sales.rdlc";
break;
case "products":
ds = new ProductData();
ds = Helper.PopulateDataSet(ds);
reportDataSource = new Microsoft.Reporting.WebForms.ReportDataSource();
reportDataSource.Name = "MergedFlatData";
reportDataSource.Value = Helper.JoinDataSetByRelation(ds).Tables["MergedFlatData"];
this.reportViewer.LocalReport.ReportEmbeddedResource = "Products.rdlc";
break;
}
this.reportViewer.LocalReport.DataSources.Clear();
this.reportViewer.LocalReport.DataSources.Add(reportDataSource);
reportViewer.LocalReport.ExecuteReportInCurrentAppDomain(System.Reflection.Assembly.GetExecutingAssembly().Evidence);
reportViewer.LocalReport.Refresh();
}
#endregion Private methods
}
To get it working I need to set reportViewer.AsyncRendering = false;
(by default is set to true)
Actually I don’t use asynchronous calls by using this control and I don’t know exactly how to use ReportViewer in asynchronous mode.