Saturday, February 14, 2009

How to get the list of reports from SSRS using VB.NET

Here is the sample code to get the list of reports in a tree structure, before this you need to add the web reference to your reporting server's web service, a sample url will loook like this:

http:///reportserver/reportservice2005.asmx 

Here is the sample code.

Public Shared Function GetReportsNode(ByVal pUserName As String, ByVal pPassword As String, ByVal pDomain As String) As TreeNode
        Dim _reportRoot As TreeNode
        Dim _report As TreeNode
        Dim _folder As TreeNode
        Dim _condition As reportservice2005.SearchCondition
        Dim _conditions(0) As reportservice2005.SearchCondition
        Dim _cReports As reportservice2005.CatalogItem()
        Dim _cItems As reportservice2005.CatalogItem()
        Dim _rp As reportservice2005.ReportingService2005 = New reportservice2005.ReportingService2005()

        'Pass login credentils to SSRS
        _rp.Credentials = New System.Net.NetworkCredential(pUserName, pPassword, pDomain)
        '_rp.Credentials = System.Net.CredentialCache.DefaultCredentials

        'Get the list of all items
        _cItems = _rp.ListChildren("/", True)
        _reportRoot = New TreeNode("Reports")
        _reportRoot.NavigateUrl = "#"

        For Each _cItem As reportservice2005.CatalogItem In _cItems
            If _cItem.Type = reportservice2005.ItemTypeEnum.Folder Then
                _condition = New reportservice2005.SearchCondition

                'Build the condition object to get all the reports from a folder, by setting Report property "Name" to empty
                _condition.Condition = reportservice2005.ConditionEnum.Contains
                _condition.ConditionSpecified = True
                _condition.Name = "Name"
                _condition.Value = ""

                _conditions(0) = _condition

                _folder = New TreeNode(_cItem.Name)
                'Find all the items inside a given folder.
                _cReports = _rp.FindItems(_cItem.Path, reportservice2005.BooleanOperatorEnum.Or, _conditions)

                'loop through each item and create the tree structure
                For Each _cReport As reportservice2005.CatalogItem In _cReports
                    _report = New TreeNode(_cReport.Name)
                    _report.NavigateUrl = "Reports/Reports.aspx?ReportPath=" & _cReport.Path
                    _folder.ChildNodes.Add(_report)
                Next

                _reportRoot.ChildNodes.Add(_folder)
            End If
        Next

        Return _reportRoot
    End Function

No comments: