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?