Skip to content

Commit 9327078

Browse files
authored
Add files via upload
1 parent c828f58 commit 9327078

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
Public Sub Main()
2+
'--------------------------------------------------------------------------------------------------------------------
3+
' Purpose: Script to apply custom header and footer from a template report to all reports on ReportServer
4+
' Notes: uses Mgmt2010 endpoint / executable against stand alone or SharePoint integrated instance
5+
' Save file as .rss extension and run using rs.exe from command line.
6+
' Reference: https://jaredzagelbaum.wordpress.com/2014/11/14/update-all-ssrs-reportheaders/
7+
' https://docs.microsoft.com/en-us/sql/reporting-services/tools/rs-exe-utility-ssrs?view=sql-server-2017
8+
' Example: rs -s http://localhost/reportserver -i D:\Scripts\Apply_Header_Footer.rss -e Mgmt2010 -v parentFolder="Salesforce_Test"
9+
'--------------------------------------------------------------------------------------------------------------------
10+
Dim reportDefinition As Byte() = Nothing
11+
Dim templateDoc As New System.Xml.XmlDocument
12+
Dim reportDoc As New System.Xml.XmlDocument
13+
Dim nsmanager As New XmlNamespaceManager(templateDoc.NameTable)
14+
Dim templatePath As String = "/_Custom/Template"
15+
Dim templateHeader As System.Xml.XmlElement = Nothing
16+
Dim templateFooter As System.Xml.XmlElement = Nothing
17+
Dim reportHeader As System.Xml.XmlElement = Nothing
18+
Dim reportFooter As System.Xml.XmlElement = Nothing
19+
Dim reportPage As System.Xml.XmlElement = Nothing
20+
nsmanager.AddNamespace("rd", "http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition")
21+
Dim items As CatalogItem() = rs.ListChildren("/", True)
22+
23+
parentFolder = "/" + parentFolder
24+
25+
Try
26+
For Each item As CatalogItem In items
27+
If item.TypeName = "Report" And item.Path = templatePath Then
28+
reportDefinition = rs.GetItemDefinition(item.Path)
29+
Dim stream1 As New MemoryStream(reportDefinition)
30+
templateDoc.Load(stream1)
31+
stream1.Dispose()
32+
Exit For
33+
End If
34+
Next
35+
36+
templateHeader = templateDoc.SelectSingleNode("/rd:Report/rd:ReportSections/rd:ReportSection/rd:Page/rd:PageHeader", nsmanager)
37+
templateFooter = templateDoc.SelectSingleNode("/rd:Report/rd:ReportSections/rd:ReportSection/rd:Page/rd:PageFooter", nsmanager)
38+
39+
For Each item As CatalogItem In items
40+
If item.TypeName = "Report" And (item.Path.StartsWith(parentFolder)) And Not (item.Name.StartsWith("SubRep")) Then
41+
reportDefinition = rs.GetItemDefinition(item.Path)
42+
Dim stream2 As New MemoryStream(reportDefinition)
43+
Dim outstream2 As New MemoryStream()
44+
reportDoc.Load(stream2)
45+
Console.WriteLine("Report: " + item.Name)
46+
Console.WriteLine("Report Path: " + item.Path)
47+
reportHeader = reportDoc.SelectSingleNode("/rd:Report/rd:ReportSections/rd:ReportSection/rd:Page/rd:PageHeader", nsmanager)
48+
reportFooter = reportDoc.SelectSingleNode("/rd:Report/rd:ReportSections/rd:ReportSection/rd:Page/rd:PageFooter", nsmanager)
49+
reportPage = reportDoc.SelectSingleNode("/rd:Report/rd:ReportSections/rd:ReportSection/rd:Page", nsmanager)
50+
Try
51+
reportHeader.InnerXml = templateHeader.InnerXml
52+
Console.WriteLine("Update Header: " + item.Path)
53+
Catch ex As NullReferenceException
54+
Console.WriteLine("Add Header: " + item.Path)
55+
End Try
56+
Try
57+
reportFooter.InnerXml = templateFooter.InnerXml
58+
Console.WriteLine("Update Footer: " + item.Path)
59+
Catch ex As NullReferenceException
60+
Console.WriteLine("Add Footer: " + item.Path)
61+
End Try
62+
reportDoc.Save(outstream2)
63+
reportDefinition = outstream2.ToArray()
64+
rs.SetItemDefinition(item.Path, reportDefinition, Nothing)
65+
stream2.Dispose()
66+
outstream2.Dispose()
67+
End If
68+
Next
69+
70+
Catch ex As Exception
71+
Console.WriteLine(ex.ToString())
72+
73+
End Try
74+
75+
End Sub

0 commit comments

Comments
 (0)