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.


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);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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