Thursday, May 19, 2011

Deploying SSRS reports through rs.exe utility

The rs utility is a script host that can be used to perform scripted operations. This utility is used to run Microsoft Visual Basic scripts that copy data between report server databases, create items in the report server database, publish reports, etc. The report servers that are configured for SharePoint integration mode do not support the rs utility.
rs {-?}
{-i input_file=}
{-s serverURL}
{-u username}
{-p password}
{-e endpoint}
{-l time_out}
{-b batchmode}
{-v globalvars=}
{-t trace}

(Optional) Displays the syntax of rs arguments.
-i input_file
(Required) Specifies the .rss file to execute. This value can be a relative or fully qualified path to the .rss file.
-s serverURL
(Required) Specifies the Web server name and report server virtual directory name to execute the file against. An example of a report server URL is http://examplewebserver/reportserver. The prefix http:// or https:// at the beginning of the server name is optional. If you omit the prefix, the report server script host tries to use https first, and then uses http if https does not work.
-u [domain\]username
(Optional) Specifies a user account used to connect to the report server. If -u and -p are omitted, the current Windows user account is used.

-p password
(Required if -u is specified) Specifies the password to use with the -u argument. This value is case-sensitive.
(Optional) Specifies the SOAP endpoint against which the script should run. Valid values are the following:
• Mgmt2010
• Mgmt2006
• Mgmt2005
• Exec2005
If a value is not specified, the Mgmt2005 endpoint is used. Note that the Mgmt2000 value is discontinued in this release and is no longer a valid value. For more information about the SOAP endpoints and deprecated features
-l time_out
(Optional) Specifies the number of seconds that elapse before the connection to the server times out. The default is 60 seconds. If you do not specify a time-out value, the default is used. A value of 0 specifies that the connection never times out.
(Optional) Specifies that the commands in the script file run in a batch. If any commands fail, the batch is rolled back. Some commands cannot be batched, and those run as usual. Only exceptions that are thrown and are not handled within the script result in a rollback. If the script handles an exception and returns normally from Main, the batch is committed. If you omit this parameter, the commands run without creating a batch.
(Optional) Specifies global variables that are used in the script. If the script uses global variables, you must specify this argument. The value that you specify must be valid for global variable defined in the .rss file. You must specify one global variable for each –v argument.
The -v argument is specified on the command line and is used to set the value for a global variable that is defined in your script at run time. For example, if your script contains a variable named parentFolder, you can specify a name for that folder on the command line:
rs.exe -i myScriptFile.rss -s http://myServer/reportserver -v parentFolder="Financial Reports"
Global variables are created with the names given and set to the values supplied. For example, -v a="1" -v b="2" results in a variable named a with a value of "1" and a variable b with a value of "2".
Global variables are available to any function in the script. A backslash and quotation mark (\") is interpreted as a double quotation mark. The quotation marks are required only if the string contains a space. Variable names must be valid for Microsoft Visual Basic; they must start with alphabetical character or underscore and contain alphabetical characters, digits, or underscores. Reserved words cannot be used as variable names.
(Optional) Outputs error messages to the trace log. This argument does not take a value.
Note: To run the tool, you must have permission to connect to the report server instance you are running the script against. You can run scripts to make changes to the local computer or a remote computer. To make changes to a report server installed on a remote computer, specify the remote computer in the -s argument.
RS.exe is located at \Program Files\Microsoft SQL Server\100\Tools\Bin. You can run the utility from any folder on your file system.
Using the RS.EXE utility:
• It is a Command-line utility.
• Used for performing Deployment and Administrative tasks programmatically.
• Executes the RSS file. [Reporting Services Script file]
• Hope you created sample report here my report name is MyReport.rdl
• Create .rss file
o Open notepad and copy the below code and save it as .rss file
o Inside the code make appropriate changes i.e. DestinationFolder, ReportSource in file.

Dim definition As [Byte]() = Nothing
Dim warnings As Warning() = Nothing
Dim DestinationFolder As String = "MyReports"
Dim parentPath As String = "/" + DestinationFolder
Dim ReportSource As String

Public Sub Main()

'Location of the rdl file
ReportSource = "C:\Reports"

rs.Credentials = System.Net.CredentialCache.DefaultCredentials
'get particular rdl file
If rs.GetItemType("/MyReports") = Microsoft.SqlServer.ReportingServices2005.ItemTypeEnum.Folder Then
End If
'creates folder on report server
rs.CreateFolder("MyReports", "/", Nothing)
Console.WriteLine("Parent folder [MyReports] created successfully.")
'method called to create datasource used in the report
'CreateDataSource(name, extension, connectionstring)
CreateReportDataSource("MyDataSource", "SQL", "Data Source=(local);Initial Catalog=AdventuresWorks")
'method called to deploy the report

Console.WriteLine("Tasks completed successfully.")

Catch ex As Exception
Throw ex
End Try
End Sub

Public Sub CreateReportDataSource(ByVal name As String, ByVal extension As String, ByVal connectionString As String)
'Data source definition.
Dim definition As New DataSourceDefinition()
definition.CredentialRetrieval = CredentialRetrievalEnum.Integrated
definition.ConnectString = connectionString
definition.Enabled = True
definition.Extension = extension

rs.CreateDataSource(name, "/MyReports", False, definition, Nothing)
Console.WriteLine("Data source: {0} created successfully.", name)
Catch e As Exception
Console.WriteLine("ERROR creating data source: " + name)
Throw e
End Try

End Sub

Public Sub PublishReport(ByVal reportName As String)
Dim stream As FileStream = File.OpenRead(ReportSource + "\" + reportName + ".rdl")
definition = New [Byte](stream.Length) {}
stream.Read(definition, 0, CInt(stream.Length))

rs.CreateReport(reportName, parentPath, False, definition, Nothing)
Console.WriteLine("Report: {0} published successfully.", reportName)

Catch e As Exception
Console.WriteLine("ERROR while Publishing report: " + reportName)
Throw e
End Try
End Sub

The sample RSS file creates a data-source, which points to the Northwind database on the (local) sql server instance using Windows authentication. The function used is: CreateDataSource(name, extension, connectionstring)

CreateDataSource("MyDataSource", "SQL", "Data Source= (local);Initial Catalog=Northwind")
It also publishes a sample report using the CreateReport function.
CreateReport(NameofReport, ParentFolder, Overwrite, ReportDefinition, Properties)

CreateReport(MyReport, /MyReports, False, definition, Nothing)
If report contains any external images, then it has to be also deployed as a resource. This is not required if the image is embedded in the report. The function used is: CreateResource(ImageName, ParentFolder, Overwrite, Definition, MIME type, Properties)

CreateResource(ImageName, parentPath, False, definition, "image/jpeg",Nothing)
For executing the rss file, open the command prompt and set the directory to D:\MyReports and run the following command:
rs -i MyRSS.rss -s http://localhost/ReportServer
Now your .rss file is created then placed this file to appropriate folder. i.e. C:/Users/Users_name/your rss file
• Open Command prompt Runcmd
• Run below cmd
rs -i MyRSS.rss -s http://localhost/reportserver

After successful execution, MyReports folder will be created on the report server and Mydatasource will get created and report will be deployed. It can be viewed using the Report Manager.