none
Constructor for Microsoft.ReportingServices.SharePoint.UI.WebParts.ReportViewerWebPart not working in Powershell? RRS feed

  • Question

  • Hi,

    I'm trying to programmatically provision a ReportViewerWebPart in Powershell and I'm getting an "Object reference not set to an instance of an object".

    My Powershell snippet is as follows:-

     

    $AssemblyName = "Microsoft.ReportingServices.SharePoint.UI.WebParts"
    [System.Reflection.Assembly]::LoadWithPartialName($AssemblyName)
    $WebPart = new-object Microsoft.ReportingServices.SharePoint.UI.WebParts.ReportViewerWebPart
    

     

    The error is as follows:-

     

    New-Object : Exception calling ".ctor" with "0" argument(s): "Object reference not set to an instance of an object."
    At line:1 char:22
    + $WebPart = new-object <<<<  Microsoft.ReportingServices.SharePoint.UI.WebParts.ReportViewerWebPart
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
    


    I don't know what I'm doing wrong, I've pretty much ported the code from C# examples I've seen around.

    Update: I've even tried it in a C# console program and I also get a NullException error on the constructor.

    using (SPSite site = new SPSite(webUrl))
                {
                    SPWeb web = site.RootWeb;
                    SPFile page = web.GetFile(serverRelativePageUrl);
    
                    using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
                    {
                        ReportViewerWebPart wp = new ReportViewerWebPart();  <--- Null Exception thrown here
                        wp.ReportPath = reportPath;
                        wpmgr.AddWebPart(wp, "wpz", 0);
                    }
                }


    Any help would be much appreciated.

    UPDATE: I solved this by providing it with a context. This is in Powershell v2 notation (note the generic Dictionary collection).

    $web = Get-SPWeb $webUrl
    	[Reflection.Assembly]::LoadWithPartialName("Microsoft.ReportingServices.SharePoint.UI.WebParts")
    	[Reflection.Assembly]::LoadWithPartialName("System.Web")
    	[Reflection.Assembly]::LoadWithPartialName("System.IO")
    	[Reflection.Assembly]::LoadWithPartialName("System.Collections.Generic")
    	[System.Web.HttpRequest] $request = new-object System.Web.HttpRequest("",$web.Url,"")
    	$response = new-object System.Web.HttpResponse([System.IO.TextWriter]::Null);
    	[System.Web.HttpContext]::Current = new-object System.Web.HttpContext($request,$response)
    	[System.Web.HttpContext]::Current.Request.Browser = new-object System.Web.HttpBrowserCapabilities
    	[System.Web.HttpContext]::Current.Request.Browser.Capabilities = new-object 'System.Collections.Generic.Dictionary[string,string]'
    	[System.Web.HttpContext]::Current.Request.Browser.Capabilities["type"] = "IE7";
    	[System.Web.HttpContext]::Current.Request.Browser.Capabilities["majorversion"] = "7";
    	[System.Web.HttpContext]::Current.Request.Browser.Capabilities["minorversion"] = "0"
    	[System.Web.HttpContext]::Current.Items["HttpHandlerSPWeb"] = [Microsoft.SharePoint.SPWeb]$web;
    	$wp = new-object Microsoft.ReportingServices.SharePoint.UI.WebParts.ReportViewerWebPart
    	$web.AllowUnsafeUpdates = $true
    	$page = $web.GetFile("$pagePath")
    	$wpm = $page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
    	$wp.ReportPath = "$reportPath"
    	$wpm.AddWebPart($wp,"$wpZoneName",$index)
    	$wpm.SaveChanges($wp)
    	$wpm.Dispose()
    	$web.Dispose()

    Kind Regards

    Chris



    • Edited by chris.sp Monday, April 9, 2012 8:46 AM found a fix
    Tuesday, January 10, 2012 6:36 AM

All replies

  • two quick guesses:

    1. The webpart needs to have additional context and can't be created with the default constructor.

    2. Reporting Services isn't configured correctly in your environment and the webpart can't initialize.

     

    Let's eliminate #2 - can you manually create a reportviewerwebpart in SharePoint via the browser?

    If you can, save it as an .webpart file, then use 

    SPLimitedWebPartManager.ImportWebPart to create the webpart from your .webpart definition file.

    After that, AddWebPart


    jliu - johnliu.net - sharepointgurus.net
    Wednesday, January 11, 2012 1:36 AM
  • Thanks John,

    I also tried that as well and got an exception like the xml has properties incorrectly set, which seems to also boil down to the constructor according to the stack.

    My sample console app:-

    using (SPSite site = new SPSite(webUrl))
                {
                    SPWeb web = site.RootWeb;
                    SPFile page = web.GetFile(serverRelativePageUrl);
    
                    //using (SPLimitedWebPartManager wpmgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
                    //{
                    //    ReportViewerWebPart wp = new ReportViewerWebPart();
                    //    wp.ReportPath = reportPath;
                    //    wpmgr.AddWebPart(wp, "wpz", 0);
                    //}
                    try
                    {
                        using (
                            SPLimitedWebPartManager wpMgr = web.GetLimitedWebPartManager(serverRelativePageUrl,
                                                                                         System.Web.UI.WebControls.WebParts.
                                                                                             PersonalizationScope.Shared))
                        {
                            //SPWebPartCollection coll = web.GetWebPartCollection(pageUrl, Storage.Shared);
    
                            string wpx =
                                @"<?xml version=""1.0"" encoding=""utf-8""?>
    <WebPart xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""http://schemas.microsoft.com/WebPart/v2"">
      <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
      <PartImageLarge>/_layouts/images/ReportServer/ReportWebPart.gif</PartImageLarge>
      <Assembly>Microsoft.ReportingServices.SharePoint.UI.WebParts, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91</Assembly>
      <TypeName>Microsoft.ReportingServices.SharePoint.UI.WebParts.ReportViewerWebPart</TypeName>
      <ReportPath xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">/Reports/Monthly Report.rdl</ReportPath>
      <DockToolBar xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">Top</DockToolBar>
      <HyperlinkTarget xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">_top</HyperlinkTarget>
      <KeepSessionAlive xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">true</KeepSessionAlive>
      <AutoGenerateTitle xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">false</AutoGenerateTitle>
      <AutoGenerateDetailLink xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">false</AutoGenerateDetailLink>
      <AsyncRendering xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">true</AsyncRendering>
      <ToolBarMode xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">Full</ToolBarMode>
      <ToolBarItemsDisplayMode xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">Back Find PageNavigation Refresh Zoom AtomFeed Print Export</ToolBarItemsDisplayMode>
      <DocumentMapMode xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">Displayed</DocumentMapMode>
      <DocumentMapAreaWidth xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">250px</DocumentMapAreaWidth>
      <PromptAreaMode xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">Displayed</PromptAreaMode>
      <OverrideParameters xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"" />
      <Title xmlns=""urn:schemas-microsoft-com:SqlServerReportViewer"">Monthly Reports</Title>
    </WebPart>";
    
    
                            string errors = String.Empty;
                            string webPartXml = wpx;
                            XmlReader xr = XmlReader.Create(new StringReader(webPartXml));
                            System.Web.UI.WebControls.WebParts.WebPart wp = wpMgr.ImportWebPart(xr, out errors);
                            string zoneId = "wpz";
                            int zoneIndex = 1;
                            wpMgr.AddWebPart(wp, zoneId, zoneIndex);
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
    



    Kind Regards

    Chris

    Wednesday, January 11, 2012 2:16 AM
  •  

    I'm now swinging towards the 1st scenario, are you able to manually create this webpart?  Does it run properly in your site?

     


    jliu - johnliu.net - sharepointgurus.net
    Wednesday, January 11, 2012 2:47 AM
  • Yep it works if I add it manually and the report works too.
    Wednesday, January 11, 2012 3:14 AM