1. 在命令運行的目錄下新建report目錄, 把要發佈的報表放到此目錄下
2. 建PublishReport.rss文件, 代碼如下
Const userID as String = "MTRCORP/ctsuat"
Const dbServer as String = "hqsdbprd03"
Const dbName as String = "CTSDB"
Const parentFolder as String = "CTS.Report"
Const roleName as String = "Browser"
dim filePath as String
dim cnt as Integer
Dim definition As [Byte]() = Nothing
Dim warnings As Warning() = Nothing
Dim parentPath As String = "/" + parentFolder
Public Sub Main()
rs.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim name As String
filePath = Environment.CurrentDirectory + "/report/"
cnt = 0
CreateFolder()
CreateDataSource()
PublishFromFolder()
CreateUser()
End Sub
'************************************************************
' Create Parent Folder
'************************************************************
Public Sub CreateFolder()
dim alreadyExists as Boolean = False
Dim items As CatalogItem() = Nothing
Dim condition As New SearchCondition()
condition.Condition = ConditionEnum.Equals
condition.ConditionSpecified = True
condition.Name = "Name"
condition.Value = parentFolder
Dim conditions(0) As SearchCondition
conditions(0) = condition
items = rs.FindItems("/", BooleanOperatorEnum.And, conditions)
If Not (items Is Nothing) Then
Dim ci As CatalogItem
For Each ci In items
if ci.Type=ItemTypeEnum.Folder then
alreadyExists=true
end if
Next ci
End If
if alreadyExists=False then
Try
rs.CreateFolder(parentFolder, "/", Nothing)
Console.WriteLine("Parent folder created: {0}", parentFolder)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
end if
End Sub
'************************************************************
' Create DataSource
'************************************************************
Public Sub CreateDataSource()
Dim name As String = dbName
Dim parent As String = "/" + parentFolder
'Define the data source definition.
Dim definition As New DataSourceDefinition
definition.CredentialRetrieval = CredentialRetrievalEnum.Integrated
definition.ConnectString = "data source=" & dbServer & ";initial catalog=" & dbName
definition.Enabled = True
definition.EnabledSpecified = True
definition.Extension = "SQL"
definition.ImpersonateUser = False
definition.ImpersonateUserSpecified = True
'Use the default prompt string.
definition.Prompt = Nothing
definition.WindowsCredentials = False
Try
rs.CreateDataSource(name, parent, True, definition, Nothing)
Console.WriteLine("Report data source created: {0}", name)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
'*************************************************************
' Read Report File from Folder
'*************************************************************
Public Sub PublishFromFolder()
Try
' Get files from specific path
Dim ArrayName As String()
Dim dirs As String() = Directory.GetFiles(filePath + "/", "*.rdl")
Console.WriteLine("{0} reports will be deploy:", dirs.Length)
Dim file As String
Dim reportname as String
For Each file In dirs
ArrayName = file.Split("/")
reportname = ArrayName(ArrayName.GetUpperBound(0))
reportname = reportname.Replace(".rdl","")
PublishReport(reportname)
Next
Console.WriteLine("{0} reports had be deploy!", cnt)
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
'*************************************************************
' Delpoyment of Reports to Server
'*************************************************************
Public Sub PublishReport(ByVal reportName As String)
Console.WriteLine("Deploy: {0}", reportName)
Try
Dim stream As FileStream = File.OpenRead(filePath + "/" + reportName + ".rdl")
definition = New [Byte](stream.Length) {}
stream.Read(definition, 0, CInt(stream.Length))
stream.Close()
Catch e As IOException
Console.WriteLine(e.Message)
End Try
Try
warnings = rs.CreateReport(reportName, parentPath, True, definition, Nothing)
If Not (warnings Is Nothing) Then
Dim warning As Warning
For Each warning In warnings
Console.WriteLine(" warning:" + warning.Message)
Next warning
End If
cnt = cnt + 1
Catch e As Exception
Console.WriteLine(e.Message)
End Try
End Sub
Public Sub CreateUser()
dim pols_old() as Policy
dim pol as Policy
dim hasExists as Boolean = false
pols_old = rs.GetPolicies("/" + parentFolder,false)
For Each pol In pols_old
if pol.GroupUserName=userID then
hasExists = true
end if
Next pol
if hasExists=false then
dim r as new Role
dim roles(0) as Role
dim newPolicy as new Policy
r.Name=roleName
roles(0) = r
newPolicy.GroupUserName=userID
newPolicy.Roles=roles
Dim cnt as Integer
cnt = pols_old.Length
dim pols_new(cnt) as Policy
Dim i As Integer
For i = 0 To cnt - 1
pols_new(i) = pols_old(i)
Next i
pols_new(cnt) = newPolicy
Try
rs.SetPolicies("/" + parentFolder, pols_new)
Console.WriteLine("Grant ""{1}"" role permission on ""{2}"" folder to ""{0}"" user!", userID, roleName, parentFolder)
Catch e As Exception
Console.WriteLine(e.Message)
End Try
end if
End Sub
3. 建cmd文件 CTS_Report_Migration.bat, 代碼如下
@echo off
rem ===== update the parameter BGN =====
set REPORT_SERVER=hqsdbprd03
rem ===== update the parameter END =====
rs -i ./PublishReport.rss -s http://%REPORT_SERVER%/reportserver > report_deploy.log
4. 當然就是運行第三步中的文件羅