共用方式為


Push Reports to Server

The VB script below is designed to push SQL Reporting Services reports from local directory and put them all into a RS folder This script works in conjunction with GetReportsFromServer, which can be found here. This script was designed to work for getting Team Foundation Server Reports from the server and pushing to another 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 delivering, then running the command while in the parent direcotry of the folder created from GetReportsFromServer

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

for example if the path used in GetReportsFromServer was in the folder C:\TFS_DefaultColleciton, then while in the C:\ directory execute:

rs -i PushReportsToServer.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. This script copies all of
' the RDL files from a local folder to the server and is designed to be used
' with GetReportsFromServer.rss
'
' 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 PushReportsToServer.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 your Reports are stored in a different directory then
''''' .\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"

'This function recursively navigates through the local hard drive uploading files and creating folders
'in the process
public Sub OnNodeArrival(myParentServerPath As String, myServerFolder As String, myHDFolder As String)
 
 'Create the parent folder on Reporting Server
 Try
         rs.CreateFolder(myServerFolder, myParentServerPath, Nothing)
         Console.WriteLine("Folder {0} created successfully", myServerFolder)
     Catch e As Exception
  Console.WriteLine("Failed to Create Folder")
         Console.WriteLine(e.Message)
     End Try

 

 '' We need to handle the possible base cases "C:\" and "/" differntly because the end in a slash
 Dim parentHDPath As String
 Dim parentRepPath As String
 If (myHDFolder = "C:\")
  parentHDPath = myHDFolder
 Else
  parentHDPath = myHDFolder+"\"
 End If

 If (myParentServerPath = "/")
  parentRepPath = myParentServerPath
 Else
  parentRepPath = myParentServerPath +"/"
 End If

 ' Retrieve a list of all items from the local and upload them.
    Dim returnValues As String()
 Dim fileWithPath As String
 Dim file As String

 'Publish reports in myHDFolder
 returnValues = Directory.GetFiles(myHDFolder)
 For Each fileWithPath in returnValues
  If (Path.GetExtension(fileWithPath) = ".rdl")
   file = Path.GetFileNameWithoutExtension(fileWithPath)
   PublishReport(fileWithPath, parentRepPath+myServerFolder, file)
  End If
 Next

 ''Recursively navigate through all sub directories
 Dim subFolderWithPath As String
 Dim subFolder As String

 returnValues = Directory.GetDirectories(myHDFolder)
 For Each subFolderWithPath in returnValues
  'the below line is nescssary because subFolderWithPath contains "C:\blah-blah\...\reportfolder"
  'and we just need "reportfolder"
  subFolder = subFolderWithPath.Replace(parentHDPath, "")

  OnNodeArrival(parentRepPath+myServerFolder, subFolder, subFolderWithPath)
  
 Next
End Sub

'This function actually publishes the report
Public Sub PublishReport(filePath As String, reportPath As String,  ByVal reportName As String)

 Dim definition As [Byte]() = Nothing
 Dim warnings As Warning() = Nothing

 Dim combinedString As String
 If (reportPath="/") Then
  combinedString = reportPath+reportName
 Else
  combinedString = reportPath+"/"+reportName
 End If

 Try
  Dim stream As FileStream = File.OpenRead(filePath)
         definition = New [Byte](stream.Length-1) {}
         stream.Read(definition, 0, CInt(stream.Length))
         stream.Close()

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

     Try
         warnings = rs.CreateReport(reportName, reportPath, False, definition, Nothing)

         If Not (warnings Is Nothing) Then
       
   'We ignore the warnings where the DataSources are not hooked up because
   'we fix this up after the report is uploaded

              Dim warning As Warning
              For Each warning In warnings
    If (warning.Code <> "rsDataSourceReferenceNotPublished") Then
     Console.WriteLine("--Warning For :" + combinedString)
     Console.WriteLine("Warning Code:" + warning.Code)
     Console.WriteLine(warning.Message)
     Console.WriteLine("")
    End If
   Next warning
         Else
             'Console.WriteLine("Report: {0} published successfully with no warnings", reportName)
         End If

     Catch e As Exception
  Console.WriteLine("--Error For :" + combinedString)
         Console.WriteLine(e.Message)
  Console.WriteLine("")
 End Try
 
 '''''
 'Modify the data source of uploaded reports
 '''''
 'In the TFS suplied reports, only two datasources are used:
  ' 1)"TFSOlapReportDS" and 2) "TfsReportDS". These values are located at respectively,
 ' 1) "/Tfs2010OlapReportDS" and 2) "/Tfs2010ReportDS".
 '
 'If your custom reports have other datasources that need to be changed, you
 'can add these datasources here or you can modifiy them by manual after upload
 'by navigating to their report at the Report Manager URL (https://myserver/reports)
 'and viewing thier datasources under the properties tab.

       Dim dataSources() As DataSource
 dataSources = rs.GetItemDataSources(combinedString)
 Dim oldDS as New DataSource()
 
 Dim count As Integer
 count = 0
 For Each oldDS In dataSources
  If (oldDS.Name = "TfsOlapReportDS")
   Dim referenceDS As New DataSourceReference() 
   referenceDS.Reference = "/Tfs2010OlapReportDS"
   oldDS.Item = referenceDS
  Else If (oldDS.Name = "TfsReportDS")
   Dim referenceDS As New DataSourceReference() 
   referenceDS.Reference = "/Tfs2010ReportDS"
   oldDS.Item = referenceDS
  End If

  dataSources(count) = oldDS
  count = count + 1
 Next oldDS

       Try
          rs.SetItemDataSources(combinedString, dataSources)
       Catch e As SoapException
  Console.WriteLine("Failed to Write Reference for :" + combinedString) 
         Console.WriteLine(e.Detail.InnerXml.ToString())
       End Try

 '''''
 'Modify the paramaters of uploaded reports
 '''''
 'In the TFS suplied reports, only one paramater is modified "ExplicitProject"
 'For the ExcplicitProject paramater, the default value is set to empty (i.e., "")
 'and PromptUser is set to False.
 '
 'If your custom reports have other paramaters that need to be changed, you
 'can add these paramaters here or you can modifiy them by manual after upload
 'by navigating to their report at the Report Manager URL (https://myserver/reports)
 'and viewing thier parameters under the properties tab.

       Dim forRendering As Boolean = False
       Dim historyID As String = Nothing
       Dim values As ParameterValue() = Nothing
       Dim credentials As DataSourceCredentials() = Nothing
       Dim parameters As ReportParameter() = Nothing
 

 parameters = rs.GetReportParameters(combinedString, historyID, forRendering, values, credentials )
 Dim par as New ReportParameter()
 
 count = 0
 For Each par In parameters
  If (par.Name = "ExplicitProject")
   Dim defVals(0) As String
   Dim emptyString As String = ""
   defVals(0) = emptyString
   par.DefaultValues = defVals
   par.PromptUser = False
  End If

  parameters(count) = par
  count = count + 1
 Next par

 Try
          rs.SetReportParameters(combinedString, parameters)
       Catch e As SoapException
  Console.WriteLine("Failed to Write Parameters for :" + combinedString) 
         Console.WriteLine(e.Detail.InnerXml.ToString())
       End Try
End Sub

Public Sub Main()

     rs.Credentials = System.Net.CredentialCache.DefaultCredentials
    Console.WriteLine("Starting")
     OnNodeArrival(gReportsLocation, gMovingCollection, gHDLocation )
 Console.WriteLine("Files from " + gHDLocation + " were published in " + gReportsLocation+"/"+gMovingCollection)
End Sub

 

PushReportsToServer.rss