Click here to Skip to main content
Click here to Skip to main content

SSRS Production Deployment: Part 3

By , 6 May 2013
 

Introduction

In Part 2, we talked about deploying SSRS data sources. Now we are ready for reports.

Reports are deployed using rs.CreateReport() method. It accepts array of bytes instead of .rdl file name. This is relatively minor, since reading contents of a file is not that hard. Here’s the code:

Dim len As Integer
Dim fileBytes As Byte()
Using stream As FileStream = File.OpenRead(path)
    len = stream.Length
    fileBytes = New [Byte](len - 1) {}
    stream.Read(fileBytes, 0, len)
End Using

rs.CreateReport("MyReport", "Reports Folder", overwrite, fileBytes, Nothing)

It would be even easier if Microsoft did not mess up the VB example at the above link: they don’t read the last byte of the stream and the result is invalid XML. Interestingly, in the SQL 2000 example, they messed up the array initialization instead, so they send an extra byte to the SSRS server instead. The result is the server complaining about illegal 0×00 character at the end of the report.

It seems difficult to comprehend, even for Microsoft people, that in VB.NET, the number of elements in your array is different from the number you specify in Dim.

Updating Data Source References

Another hurdle is that report definitions, as stored by BIDS, contain invalid data source references. When you upload a report as outlined above, you will get a warning similar to this:

The dataset `DataSet1' refers to the shared data source `MyDataSource', 
which is not published on the report server.

HTTP traffic sniffing shows that when BIDS deploys a report, it gets the same warning. The solution is to update the data source references after the report has been uploaded, changing them to existing data sources in the "/Data Sources" folder.

This requires several steps. First, we need to load the report file as XML and locate all data source references.

Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
doc.Load(rdlPath)

Dim nsManager As XmlNamespaceManager = New XmlNamespaceManager(doc.NameTable)
nsManager.AddNamespace("r", "http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition")
Dim nodes As XmlNodeList = doc.SelectNodes("/r:Report/r:DataSources/r:DataSource/r:DataSourceReference", 
			nsManager)

Then we need to convert those nodes to DataSource objects recognized by SSRS:

Dim dataSources As DataSource() = New DataSource(nodes.Count - 1) {}

For i As Integer = 0 To nodes.Count - 1
    dataSources(i) = CreateDataSourceObj(nodes.Item(i))
Next

Private Function CreateDataSourceObj(ByVal refNode As XmlNode) As DataSource

    Dim reference As DataSourceReference = New DataSourceReference
    reference.Reference = "/Data Sources/" + refNode.InnerText

    Dim result As DataSource = New DataSource
    result.Name = CType(refNode.ParentNode, XmlElement).GetAttribute("Name")
    result.Item = reference

    Return result
End Function

And finally, we need to call rs.SetItemDataSources method:

rs.SetItemDataSources(serverReportPath, dataSources)

The complete code for creating a report is here.
The combined file for creating a data source and a report is here.

It was fun, isn’t it? I am not sure why Microsoft did not make it simpler: SSRS reports are typically needed in an enterprise environment, and production deployment is an important part of development for the enterprise. It would also help if the code sample for CreateReport were right: I spent quite some time trying to figure out why I am getting weird exceptions from SSRS before I noticed the size error.

I hope this text will save others hours of boilerplate coding and frustration.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

About the Author

Ivan Krivyakov
Architect Sungard Consulting Services
United States United States
Member
Ivan is a hands-on software architect working for SunGard Consulting, in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.
 
Please visit my web site: www.ikriv.com.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130513.1 | Last Updated 6 May 2013
Article Copyright 2013 by Ivan Krivyakov
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid