Unanswered SPFeatureReceiver: Creating report viewer web part programmatically

  • Monday, February 22, 2010 12:35 AM
     
      Has Code

    I have a feature that adds a report viewer web part (SP integrated mode) into the newly created site page.

    Problem: The feature activates without any problem through the UI, but I cannot activate it through STSADM (which will be the case when going to production).

    Here is my code block that adds the web part to the page. This code block exists in FeatureActivated:

    Assembly currentAssembly = Assembly.GetExecutingAssembly();
    string dwpStr = string.Empty;
    
    try
    {
        using (Stream resourceStream = currentAssembly.GetManifestResourceStream("Report.dwp"))
        {
            using (StreamReader sr = new StreamReader(resourceStream))
            {
                dwpStr = sr.ReadToEnd();
                dwpStr = string.Format(dwpStr, web.Url);
                sr.Close();
            }
            resourceStream.Close();
        }
    
        using (StringReader sr = new StringReader(dwpStr))
        {
            string err = string.Empty;
            System.Web.UI.WebControls.WebParts.WebPart wp = wpMan.ImportWebPart((XmlReader.Create(sr as TextReader)), out err); //Exception thrown from this line
            wpMan.AddWebPart(wp, "HeaderZone", 0);
        }
    }
    catch (Exception ex)
    {
        //Log errors
    }
    Report.dwp is as follows:
    <?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">
      <FrameType>None</FrameType>
      <Description>Use the Report Viewer to view reports running on a Reporting Services report server configured for SharePoint integration.</Description>
      <IsIncluded>true</IsIncluded>
      <ZoneID>HeaderZone</ZoneID>
      <PartOrder>0</PartOrder>
      <FrameState>Normal</FrameState>
      <Height>175px</Height>
      <Width />
      <AllowRemove>false</AllowRemove>
      <AllowZoneChange>false</AllowZoneChange>
      <AllowMinimize>false</AllowMinimize>
      <AllowConnect>false</AllowConnect>
      <AllowEdit>true</AllowEdit>
      <AllowHide>false</AllowHide>
      <IsVisible>true</IsVisible>
      <DetailLink />
      <HelpLink />
      <HelpMode>Modeless</HelpMode>
      <Dir>Default</Dir>
      <PartImageSmall />
      <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
      <PartImageLarge>/_layouts/images/ReportServer/ReportWebPart.gif</PartImageLarge>
      <IsIncludedFilter />
      <_locDefinition>
        <_locTag d3p1:_loc="locData" xmlns:d3p1="http://schemas.microsoft.com/WebPart/v2">Title</_locTag>
        <_locTag d3p1:_loc="locData" xmlns:d3p1="http://schemas.microsoft.com/WebPart/v2">Description</_locTag>
      </_locDefinition>
      <Title d2p1:_locID="WP_ReportViewer_Title" xmlns:d2p1="http://schemas.microsoft.com/WebPart/v2">SQL Server Reporting Services Report Viewer</Title>
      <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">{0}/Rrports/MyReport.rdl</ReportPath>
      <HyperlinkTarget xmlns="urn:schemas-microsoft-com:SqlServerReportViewer">_top</HyperlinkTarget>
      <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">None</ToolBarMode>
      <DocumentMapMode xmlns="urn:schemas-microsoft-com:SqlServerReportViewer">Hidden</DocumentMapMode>
      <DocumentMapAreaWidth xmlns="urn:schemas-microsoft-com:SqlServerReportViewer">200px</DocumentMapAreaWidth>
      <PromptAreaMode xmlns="urn:schemas-microsoft-com:SqlServerReportViewer">Hidden</PromptAreaMode>
      <OverrideParameters xmlns="urn:schemas-microsoft-com:SqlServerReportViewer" />
      <Title xmlns="urn:schemas-microsoft-com:SqlServerReportViewer" />
    </WebPart>

    An exception was thrown when executing the code above, it says:

    "One of the properties of the Web Part has an incorrect format. Windows SharePoint Services cannot deserialize the Web Part. Check the format of the properties and try again"

    What is wrong here?

    • Moved by Mike Walsh FIN Monday, February 22, 2010 7:06 AM ssrs related (From:SharePoint - Development and Programming (pre-SharePoint 2010))
    •  

All Replies

  • Wednesday, March 10, 2010 1:46 PM
     
      Has Code
     you can directly use ReportViewerWebPart class instead of trying to get "Report.dwp".


    make sure to add a reference to the DLL:
    Microsoft.ReportingServices.SharePoint.UI.WebParts

    Then use the following two lines to add the ReportViewer webpart:
    Microsoft.ReportingServices.SharePoint.UI.WebParts.ReportViewerWebPart reportviewer = new ReportViewerWebPart();
    
    wpMan.AddWebPart(reportviewer , "HeaderZone", 0);

     if you have any concerns, let me know.

     

     

     

    • Proposed As Answer by Basyonic Wednesday, March 10, 2010 1:47 PM
    • Unproposed As Answer by Mike Walsh FIN Wednesday, March 10, 2010 2:19 PM
    • Marked As Answer by Chengyi Wu Friday, April 09, 2010 4:00 AM
    • Unmarked As Answer by apsi Monday, April 19, 2010 12:15 AM
    •  
  • Wednesday, March 10, 2010 2:21 PM
     
     
    Basyonic:

    Please do not propose your own posts as answers. It is not helpful.

    Propose the good answers of other people, but always wait for someone else to propose your posts.


    (Moderator)
    FAQ sites: (SP 2010) http://wssv4faq.mindsharp.com; (v3) http://wssv3faq.mindsharp.com and (WSS 2.0) http://wssv2faq.mindsharp.com
    Complete Book Lists (incl. foreign language) on each site.
  • Tuesday, March 16, 2010 8:46 PM
     
     
    I've tried using the object model properties to add these parts to pages with mixed success.  Sometimes when I instantiate a reference to a new ReportViewerWebPart() or try to construct this object from an existing part on a page, I receive an "error web part" instead.  I have not found good information on how to resolve this issue though.

    I wanted to comment and see if anyone had experienced and overcome this issue.
    Dan Luciano - Sogeti USA - MCP, MCTS WSS 3.0 and MOSS 2007 Configuration WSS 3.0 Applicaton Development
  • Monday, March 29, 2010 8:07 PM
     
     
    I ran into this exact problem before.  It didn't work through STSADM because SPContext.Current is null when you're not using the UI.  Try this : http://crouchingrabbit.blogspot.com/2010/02/programmatically-accessing-reportviewer.html
    • Edited by iSeeBugs Friday, December 03, 2010 11:04 PM
    •  
  • Thursday, April 01, 2010 9:33 PM
     
     
    I followed the advice of the wanderingprogrammer (URL in the previous post) and it definitely ridded me of the dreaded "ErrorWebPart".  I can now update the reportviewerwebpart via code.
    • Edited by Mike Walsh FIN Friday, April 02, 2010 3:34 AM URL added for clarity. (I couldn't find a previous post from wandering programmer)
    •  
  • Monday, April 19, 2010 12:14 AM
     
      Has Code
    I ran into this exact problem before.  It didn't work through STSADM because SPContext.Current is null when you're not using the UI.  Try this: http://wanderingprogrammer.blogspot.com/2010/02/programmatically-accessing-reportviewer.html

    Although the method provides a workaround to null HttpContext by instantiating our own HttpContext, I am not so sure about hardcoding "IE7" explained in the article

     

    HttpContext.Current.Request.Browser.Capabilities["type"] = "IE7";
    HttpContext.Current.Request.Browser.Capabilities["majorversion"] = "7";
    HttpContext.Current.Request.Browser.Capabilities["minorversion"] = "0"

     

    I prefer not to use this method as I need to support IE6, IE7 and IE8.

    I also tried the approach using ReportViewer web part directly and it gave me the same error. Looks like it is something internal to the web part itself. This web part is quite a pain to work with if any extension or automation is to be done. I hope Microsoft does make improvements to this in SharePoint 2010. AJAX is great but so far, I haven't seen much extensibility for this web part, even in SharePoint 2010.

    I can't seem to find other ways, therefore at the moment, this feature can only be activated successfully from SharePoint UI.

  • Monday, April 19, 2010 9:32 PM
     
     
    As far as I can tell, the browser version is only taken into account when the webpart loads and isn't saved anywhere.  You can  hard-code in a version to instantiate the webpart, but when a user actually loads it from their browser, they'll be using their own HttpContext.
  • Thursday, June 21, 2012 9:20 AM
     
     
    Where to get the dll from?