Search code examples
pythonpython-3.xreporting-services

Python: How to get SSRS Report Subscriptions published by different user via API


I'm using the SSRS API with Python 3 and need to get all reports and their subscriptions.

https://app.swaggerhub.com/apis/microsoft-rs/SSRS/2.0#/info

from requests_negotiate_sspi import HttpNegotiateAuth
import requests

req_url = r'http://<my_server_url>/reports/api/v2.0/Subscriptions'

session = requests.Session()
session.auth = HttpNegotiateAuth()

report_response = session.get(req_url, stream=True)

report_response_json = report_response.json()

session.close()

# do stuff with json response, eventually export to CSV

This works for getting me all the information I need for reports that I created and published. However, there are reports published by other users on the server and I'd like to be able to get their report information too.


Solution

  • Seems like /CatalogItems returns everything, so the new workflow is to use that endpoint and then cycle through each item. Then make another get request using the ID of each item in the response.

    from requests_negotiate_sspi import HttpNegotiateAuth
    import requests
    
    api_url = r'http://<my_server>/reports/api/v2.0/'
    
    # single-sign on
    session = requests.Session()
    session.auth = HttpNegotiateAuth()
    
    # use Odata filtering to only get Reports and LinkedReports
    # https://docs.oasis-open.org/odata/odata/v4.0/odata-v4.0-part1-protocol.html#_Toc445374625
    report_response = session.get(api_url + r"CatalogItems?%24filter=Type%20eq%20'Report'%20or%20Type%20eq%20'LinkedReport'")
    
    report_response_json = report_response.json()
    for report_item in report_response_json['value']:
        try:
            # documentation doesn't list the LinkedReport(Id)/Subscriptions endpoint, 
            # but it exists just like the Report(Id)/Subscriptions one
            # https://app.swaggerhub.com/apis/microsoft-rs/SSRS/2.0#/LinkedReports/GetLinkedReports
            subscription_request_response = session.get(api_url + report_item['Type'] + 's(' + report_item['Id'] + ')/Subscriptions')
            subscription_items = subscription_request_response.json()['value']
    
            if(len(subscription_items) == 0): # no subscriptions
                # do stuff
            else: # report has subscriptions
                for subscription_item in subscription_items:
                    # do other stuff with each returned subscription dict
        except:
            # log error, do other stuff
    
    session.close()