Public Sub Main()
CreateFolder("/", "Data Sources")
CreateFolder("/", "My Reports")
CreateDataSource("/Data Sources", "myDataSource", _
"Data Source=server\instance;Initial Catalog=myDatabase")
CreateReport("/My Reports", _
"MyReport", _
"C:\myreport.rdl", _
True, _
"/Data Sources", _
"myDataSource")
End Sub
Public Sub CreateFolder(parent As String, name As String)
Dim fullpath As String = GetFullPath(parent, name)
Try
RS.CreateFolder(name, parent, GetCommonProperties())
Console.WriteLine("Folder created: {0}", name)
Catch e As SoapException
If e.Detail.Item("ErrorCode").InnerText = "rsItemAlreadyExists" Then
Console.WriteLine("Folder {0} already exists and cannot be overwritten", fullpath)
Else
Console.WriteLine("Error : " + e.Detail.Item("ErrorCode").InnerText + " (" + e.Detail.Item("Message").InnerText + ")")
End If
End Try
End Sub
Public Sub CreateDataSource(parent As String, name As String, connectionString As String)
Try
RS.CreateDataSource(name, parent,False, GetDataSourceDefinition(connectionString), GetCommonProperties())
Console.WriteLine("DataSource {0} created successfully", name)
Catch e As SoapException
Console.WriteLine("Error : " + e.Detail.Item("ErrorCode").InnerText + " (" + e.Detail.Item("Message").InnerText + ")")
End Try
End Sub
Public Sub CreateReport(parent As String, name As String, location As String, overwrite As Boolean, dataSourcePath As String, dataSourceName As String)
Dim reportContents As Byte() = Nothing
Dim warnings As Warning() = Nothing
Dim fullpath As String = GetFullPath(parent, name)
'Read RDL definition from disk
Try
Dim stream As FileStream = File.OpenRead(location)
reportContents = New [Byte](stream.Length-1) {}
stream.Read(reportContents, 0, CInt(stream.Length))
stream.Close()
warnings = RS.CreateReport(name, parent, overwrite, reportContents, GetCommonProperties())
If Not (warnings Is Nothing) Then
Dim warning As Warning
For Each warning In warnings
Console.WriteLine(Warning.Message)
Next warning
Else
Console.WriteLine("Report: {0} published successfully with no warnings", name)
End If
'Set report DataSource references
Dim dataSources(0) As DataSource
Dim dsr0 As New DataSourceReference
dsr0.Reference = dataSourcePath
Dim ds0 As New DataSource
ds0.Item = CType(dsr0, DataSourceDefinitionOrReference)
ds0.Name=dataSourceName
dataSources(0) = ds0
RS.SetItemDataSources(fullpath, dataSources)
Console.Writeline("Report DataSources set successfully")
Catch e As IOException
Console.WriteLine(e.Message)
Catch e As SoapException
Console.WriteLine("Error : " + e.Detail.Item("ErrorCode").InnerText + " (" + e.Detail.Item("Message").InnerText + ")")
End Try
End Sub
Public Function GetCommonProperties() As [Property]()
'Common CatalogItem properties
Dim descprop As New [Property]
descprop.Name = "Description"
descprop.Value = ""
Dim hiddenprop As New [Property]
hiddenprop.Name = "Hidden"
hiddenprop.Value = "False"
Dim props(1) As [Property]
props(0) = descprop
props(1) = hiddenprop
Return props
End Function
Public Function GetDataSourceDefinition(connectionString as String)
Dim definition As New DataSourceDefinition
definition.CredentialRetrieval = CredentialRetrievalEnum.Integrated
definition.ConnectString = connectionString
definition.Enabled = True
definition.EnabledSpecified = True
definition.Extension = "SQL"
definition.ImpersonateUser = False
definition.ImpersonateUserSpecified = True
definition.Prompt = "Enter a user name and password to access the data source:"
definition.WindowsCredentials = False
definition.OriginalConnectStringExpressionBased = False
definition.UseOriginalConnectString = False
Return definition
End Function
Private Function GetFullPath(parent As String, name As String) As String
If parent = "/" Then
Return parent + name
Else
Return parent + "/" + name
End If
End Function