In the previous post, we created an Azure Function which generates an SSRS report from Dynamics 365. We can now hook that function up to Microsoft Flow as a custom connector, where we can use it as an action in a flow to send the response (i.e. the report file) in an email.
Before we can setup the custom connector, we must create a new App Registration in the Azure Portal. Go back to the Azure Active Directory App Registrations and create a new app. Call it anything you would like and give it a login URL of https://login.windows.net. Once it's created, replace the existing reply URL with https://msmanaged-na.consent.azure-apim.net/redirect. In Required Permissions, grant it delegated access to the Azure Function app registration that was created in the first post. Also, you must grant the Read Directory permission to the Windows Azure Active Directory API. Next, create a new key and save the value somewhere. You will also want to write down the Application ID (i.e. the Client ID).
Now, we will generate the API definition and import it into Flow. To do this, go back to the Function App, click the Platform Features, and API Definition. Change from External URL to Function. Update it to match the definition below (changing the title and host, of course).
swagger: '2.0' info: title: bguidingerfunctions.azurewebsites.net version: 1.0.0 host: bguidingerfunctions.azurewebsites.net basePath: / schemes: - https paths: /api/Report: get: operationId: /api/Report/get summary: Render Report description: Render a report produces:  consumes:  parameters: - in: query name: reportId type: string required: true description: Report ID x-ms-summary: Report ID - in: query name: format type: string required: true description: Format (PDF | Excel) x-ms-summary: Format enum: [PDF, EXCEL] responses: default: description: Default schema: type: string title: Body format: binary
Now, click Export to PowerApps + Flow, give the connector a Custom API Name and click OK. With the connector created, head over to Flow, and under Settings click Custom Connectors. Click the pencil icon to edit the connector. Flip over to the Security tab and click Edit. Change the authentication type to OAuth 2.0 and set the Identity Provider to Azure Active Directory. Paste in the Client ID and Secret from your connector App Registration. Set the resource URL to the URL of your Function App Service. Click Update Connector.
Finally, click the Test tab and create a new connection and test it out! You may get a 404 initially, but if you give it 30 minutes or so, it should start working. You may also get a 401 - if you do, verify you correctly setup all the permissions on the App Registrations, and try logging out and back in.
Once the connector is tested successfully, you can create a flow with it! Here's an example of a basic one.
Notice that we can use the dynamic content feature to set the Body of the Render Report step as the Attachments Content for the send an email step. Once we save it and run it, we can see the status.
And after a few seconds, we see the email in our inbox!
How cool is that?!? You can now schedule an SSRS report to be emailed out on a recurring basis! And because of how awesome Flow is, you could instead save the reports to a SharePoint folder or an on-premises file share.