1

On my SharePoint 2010 I want to automate Website-Provisioning with customized homepage. The homepage should show some SSRS ReportViewerWebparts. I get error messages, I am very surprised of and cannot figure out, what is not working. See my code:

# Script settings
$webUrl = "https://sharepoint.de/websites/projekte"
$pageUrl = "default.aspx"

# Open web
$web = Get-SPWeb $webUrl

$webPartManager = $web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$webpart = New-Object Microsoft.ReportingServices.SharePoint.UI.WebParts.ReportViewerWebPart
$webpart.ChromeType=[System.Web.UI.WebControls.WebParts.PartChromeType]::TitleOnly;
$webpart.Title="Projekt-Metadaten"
$webpart.ReportPath = "https://sharepoint.de/websites/projekte/Reports/Projektdaten.rdl";

$webpartmanager.AddWebPart($webpart, "Left", 1);

#Dispose web
$web.Dispose()

I get the following Error messages: In the line where I want to set the ReportPath property:

Exception setting "ReportPath": "Object reference not set to an instance of an object."

The created Webpart just gives some information. It also has a Property "ReportPath". Why does it say it is a null object?

In the line where I want to add the Webpart to the Page:

Exception calling "AddWebPart" with "3" argument(s): "There was an error generating XML document."

This error messsage is so general that I can't extract any meaning out of it.

Does anyone know how to add an ReportViewer Webpart to a page in SP2010? Thank you!

Update:

I tried it another way: export an webpart and import this via PowerShell by following this example script:

# Script settings
$webUrl = "https://sharepoint.de/websites/projekte"
$pageUrl = "default.aspx"
$webpartPath = 'C:\tmp\testpart.dwp'
$errorMsg = 'Test error msg'

# Open web
$web = Get-SPWeb $webUrl

$webPartManager = $web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$xmlReaderSettings = New-Object System.Xml.XmlReaderSettings
$xmlReader = [System.Xml.XmlReader]::Create($webpartPath ,$xmlReaderSettings)
$oWebPartDefinition = $webPartManager.ImportWebPart($xmlReader,[ref]$errorMsg)

$webpartmanager.AddWebPart($oWebPartDefinition , "Right", 1);

#Dispose web
$web.Dispose()

It is really weird. Now I get this error message in the $webPartManager.ImportWebPart()-Call:

Exception calling "ImportWebPart" with "2" argument(s): "One of the properties of the Web Part has an incorrect format. Microsoft SharePoint Foundation cannot deserialize the Web Part. Check the format of the properties and try again."

The exported WebpartXML looks like this:

<?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>Default</FrameType>
  <Description>Verwenden Sie den Berichts-Viewer, um Berichte von SQL Server Reporting Services anzuzeigen.</Description>
  <IsIncluded>true</IsIncluded>
  <ZoneID>Left</ZoneID>
  <PartOrder>0</PartOrder>
  <FrameState>Normal</FrameState>
  <Height>400px</Height>
  <Width />
  <AllowRemove>true</AllowRemove>
  <AllowZoneChange>true</AllowZoneChange>
  <AllowMinimize>true</AllowMinimize>
  <AllowConnect>true</AllowConnect>
  <AllowEdit>true</AllowEdit>
  <AllowHide>true</AllowHide>
  <IsVisible>true</IsVisible>
  <DetailLink />
  <HelpLink />
  <HelpMode>Modeless</HelpMode>
  <Dir>Default</Dir>
  <PartImageSmall />
  <MissingAssembly>Dieses Webpart kann nicht importiert werden.</MissingAssembly>
  <PartImageLarge>/_layouts/images/ReportServer/ReportWebPart.gif</PartImageLarge>
  <IsIncludedFilter />
  <Title>Berichts-Viewer für SQL Server Reporting Services</Title>
  <Assembly>Microsoft.ReportingServices.SharePoint.UI.WebParts, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91</Assembly>
  <TypeName>Microsoft.ReportingServices.SharePoint.UI.WebParts.ReportViewerWebPart</TypeName>
  <ReportPath xmlns="urn:schemas-microsoft-com:SqlServerReportViewer">/reporting/Reports_TEST/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">true</AutoGenerateTitle>
  <AutoGenerateDetailLink xmlns="urn:schemas-microsoft-com:SqlServerReportViewer">true</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 ReportBuilder Subscription NewDataAlert</ToolBarItemsDisplayMode>
  <DocumentMapMode xmlns="urn:schemas-microsoft-com:SqlServerReportViewer">Displayed</DocumentMapMode>
  <DocumentMapAreaWidth xmlns="urn:schemas-microsoft-com:SqlServerReportViewer">200px</DocumentMapAreaWidth>
  <PromptAreaMode xmlns="urn:schemas-microsoft-com:SqlServerReportViewer">Displayed</PromptAreaMode>
  <OverrideParameters xmlns="urn:schemas-microsoft-com:SqlServerReportViewer" />
  <Title xmlns="urn:schemas-microsoft-com:SqlServerReportViewer">Some sample Report Viewer</Title>
</WebPart>

The weird part about this is, if I import this exported webpart via SharePoint UI everything runs smoothly without error. I only get an error while importing via PowerShell. I tested another webpart (ContentEditorWebPart): Export it and import via PowerShell. This is working, too. So the problem (as I can see it) is exactly the ReportViewerWebpart combined with automated PowerShell Script. Any hints?

  • The last time I used the report viewer web part was in 2007, and I vaguely recall that it had 2 fields that needed supplied, one was a path to the SSRS server, and the other was the relative path to the report, Is there another web part property that looks like it wants the location to the SSRS server? – Eric Alexander Jun 8 '15 at 17:03
  • Hi, thank you for your suggestion. Unfortunately the report server is the SP instance itself and there is no property which would allow such an information. MSDN Class Information does not specify that, too. – skorzinetzki Jun 9 '15 at 6:00
1

I found a working solution. The ReportViewerWebPart needs an additional context. Can anyone tell me, where this is documented? Whatever, here is my working code:

$webUrl = "https://sharepoint.de/websites/projekte"
$pageUrl = "default.aspx"
$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("$pageUrl")
$wpm = $page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$wp.ReportPath = "https://sharepoint.de/websites/projekte/Reports_TEST/Ausl%20CUx.rdl&CU=BI"
$wpm.AddWebPart($wp,"Left",1)
$wpm.SaveChanges($wp)
$wpm.Dispose()
$web.Dispose()

I found this solution right here on MSDN forum.

Though, the next problem is to set the parameters of the report...

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.