Get Reports From Server RS Script

The VB script below is designed to get SQL Reporting Services reports from the server and put them all in a local folder. This script works in conjunction with PushReportsToServer, which can be found here. This script was designed to work for getting Team Foundation Server Reports from the server, but can be adapated for any set of Reporting services reports.

This script is executed by modifying it so that gMovingCollection variable is the name of the project collection that you are moving , then running the command:

rs -i GetReportsFromServer.rss -s https://<tfsServer>/reportserver

for example

rs -i GetReportsFromServer.rss -s https://contoso/reportserver

The code in this script is given here and can be found in the attached file.

 

'=============================================================================
'  File:      GetReportsFromServer.rss
'
'  Summary:  Dowloads all reports from a TFS Reports Server to a specified directory
'     
'
'---------------------------------------------------------------------
'
'  Copyright (C) Microsoft Corporation.  All rights reserved.
'
' This source code is intended only as a supplement to Microsoft
' Development Tools and/or on-line documentation.  See these other
' materials for detailed information regarding Microsoft code samples.
'
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'=============================================================================
'
' 1.0 Documentation
'
' Read the following in order to familiarize yourself with the sample script.
'
' 1.1 Overview
'
' This sample script uses a script file (.rss) and the script environment to run
' Web service operations on a specified report server. The script creates coppies
' all files from a TFS collection and stores them on the specified directory.
'
' To correcly run this script you will need to modify the global variable:
' "gMovingCollection"
'
'
' 1.2 Sample Command Lines
'
'
' 1.2.1 Use the script to publish the sample reports to an AdventureWorks Sample Reports folder.
'
'       rs -i GetReportsFromServer.rss -s https://myserver/reportserver
'

''''' Modify gMovingCollection to be the collection name you want to move.

Dim gMovingCollection As String = "DefaultCollection"

''''' Only change the variable gHDLocation, if you don't want the report files stored in the
''''' directory ".\TFS_gMovingCollection"

Dim gHDLocation As String = ".\TFS_"+gMovingCollection

''''' Only change the variable gReportsLocation, if some reason your reports are stored in a direcotry other thean
''''' "/TFSReports" (i.e., never change this).

Dim gReportsLocation As String = "/TFSReports"

 

public Sub OnNodeArrival(MyServerFolder as String, MyHDFolder as String)
 
 'Create Folder on HD
 If(Not System.IO.Directory.Exists(MyHDFolder)) Then
     System.IO.Directory.CreateDirectory(MyHDFolder)
 End If

 ' Retrieve a list of all items from the report server database.
       Dim items As CatalogItem() = Nothing
 Try
         items = rs.ListChildren(MyServerFolder, False)     
       Catch e As SoapException
          Console.WriteLine(e.Detail.InnerXml.ToString())
       End Try
     
       
 Dim catalogItem As CatalogItem
 Dim reportDefinition As Byte() = Nothing
 Dim doc As New System.Xml.XmlDocument
 Dim serverFolderString As String
 Dim hdFolderString As String

 '' Get list of all children reports and seralize it locally.  
 For Each catalogItem In items
  If (catalogItem.Type=ItemTypeEnum.Report)
        Try
   
   '' We need to handle the appended string name differently if
   '' 1) the base report path is \
   '' 2) or the base hard drive path is C:\ (if you want to store to a differnt drive change this)

   If (MyServerFolder = "/")
    serverFolderString = "/"+catalogItem.Name
   Else
    serverFolderString = MyServerFolder + "/"+ catalogItem.Name
   End If

   If (MyHDFolder = "C:\")
    hdFolderString = "C:\"+catalogItem.Name +".rdl"
   Else
    hdFolderString = MyHDFolder+"\" + catalogItem.Name +".rdl"
   End If

   'Console.WriteLine(serverFolderString)
   'Console.WriteLine(hdFolderString)

   ' Serialize the contents as an XML document and write the contents to a file.
          reportDefinition = rs.GetReportDefinition(serverFolderString)
          Dim stream As New MemoryStream(reportDefinition)
          doc.Load(stream)
          doc.Save(hdFolderString)
   Console.WriteLine("Coppied " + serverFolderString + " to " + hdFolderString)

        Catch e As SoapException
           Console.WriteLine(e.Detail.InnerXml.ToString())

         Catch e As IOException
            Console.WriteLine(e.Message)
        End Try  
  End If
  Next catalogItem 

 ''Go through and get all reports and rescursively navigate them
 For Each catalogItem In items
  If (catalogItem.Type=ItemTypeEnum.Folder)
    If (MyServerFolder = "/")
    serverFolderString = "/"+catalogItem.Name
    Else
    serverFolderString = MyServerFolder + "/"+ catalogItem.Name
    End If

    If (MyHDFolder = "C:\")
    hdFolderString = "C:\"+catalogItem.Name
    Else
    hdFolderString = MyHDFolder+"\" + catalogItem.Name
    End If

    OnNodeArrival(serverFolderString, hdFolderString)
  End If
  Next catalogItem 
End sub

 

Public Sub Main()
 Dim baseReportFolderName As String
 Dim baseHDFolderName As String

 baseReportFolderName = gReportsLocation+"/"+gMovingCollection
 baseHDFolderName = gHDLocation

 rs.Credentials = System.Net.CredentialCache.DefaultCredentials
  
 Console.WriteLine("Starting")

 OnNodeArrival(baseReportFolderName, baseHDFolderName)
      
 Console.WriteLine("")
 Console.WriteLine("Reports downloaded into  " +baseHDFolderName)
End Sub 'Main

 

 

GetReportsFromServer.rss

Comments

  • Anonymous
    July 29, 2010
    Thanks for the code.I have changed Dim gHDLocation As String = "C:TFS_"+gMovingCollection.which means i will saving the file to my C drive ...when i run the code there is nothing inside my destination folder. Please help
  • Anonymous
    January 10, 2012
    This is a fantastic script - thank you!
  • Anonymous
    May 07, 2015
    Thank you so much. The code is working fine for collection level, but i want only project level please help me. thank you