I am new to Sharepoint. I need to know the best way to add a ReportViewWebPart to a page through C# code behind.

I have tried accessing the web part from WebPartCatalog. It leads me to the NullReferenceException.

Then I tried by creating instance for ReportViewerWebPart and add the properties for ReportPath, Title, etc., it leads me to Object Reference Not Set to an Instance of an Object exception.

But I could add other web parts to a page as normal.

1 Answer 1


try this code:

SPLimitedWebPartManager webParts = SPContext.Current.Web.GetLimitedWebPartManager("pathToPage",System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
ReportViewerWebPart reportWP = new ReportViewerWebPart();
reportWP.Title = "Title";
reportWP.ToolBarMode = ToolBarDisplayMode.Full;
reportWP.ToolBarItemsDisplayMode = ToolBarItem.Export | ToolBarItem.Print | ToolBarItem.Zoom;
reportWP.AutoGenerateTitle = false;
reportWP.AutoGenerateDetailLink = false;
reportWP.KeepSessionAlive = false;
reportWP.ScrollBars = ScrollBars.Vertical;
reportWP.ReportPath = "report.rdl";
webParts.AddWebPart(reportWP, "Body", 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.