How to trigger a SSRS Subscription based on an event?

99封情书 提交于 2019-12-09 16:11:17

问题


Is there a way by which I can trigger a SSRS subscription (Time based) whenever there is an event like file created in a shared folder? Can we do it with powershell or C#?

Is there a out of box feature available in SSRS (though I don't think there is any)?

I am using SQL Server 2008 R2.


回答1:


Yes, we do something like this here. You can use the FireSubscription function of the Reporting Services web services to trigger a subscription. Here's a detailed explanation of how to set it up:

Firing a Reporting Services Subscription

You can use the FileSystemWatcher to tell when your file is dropped and then fire the subscription off. It's asynchronous though so you don't get notification if the report was sent successfully... only that it was successfully queued up. Also you first modify the parameters of the subscription before you fire it, so you have to make sure that you don't have more than one program to trigger the subscription or it might end up tripping over itself.

Another slightly more complicated way to do it is to use the Render function to generate a report and then have your program manage the emailing.

Render Function

This way you don't have to create a dummy subscription and you'll know immediately if it was sent successfully with the correct parameters.

One final note... if you have the Enterprise Edition (which you probably don't), it comes with Data Driven Report Subscriptions, which you could use to trigger a subscription:

Creating a Data-Driven Subscription




回答2:


Here i have used timely subscription , i had requirement to generate report on some button click, so i created subscription which will fire after one minute and generate PDF report. And I got all help from this article : http://odetocode.com/articles/114.aspx

You need to add webservice reference of webservice provided by SSRS http://mymachine/ReportServer/ReportService2010.asmx

Here @"\MyMachineName\Share", is path where my pdf was stored (PATH:The folder path or UNC file share path to which to save the report. https://msdn.microsoft.com/en-us/library/ms154020.aspx)

So you can call generate subscription as per your need on file created.

        using Test_WebProject.ReportService2010;
        private static ExtensionSettings GetExtensionSettings()
        {
            ParameterValue[] extensionParams = new ParameterValue[7];

            for (int i = 0; i < extensionParams.Length; i++)
                extensionParams[i] = new ParameterValue();

            extensionParams[0].Name = "FILENAME";
            extensionParams[0].Value = "Test1@TimeStamp";

            extensionParams[1].Name = "FILEEXTN";
            extensionParams[1].Value = "true";

            extensionParams[2].Name = "PATH";
            extensionParams[2].Value = @"\\MyMachineName\Share";

            extensionParams[3].Name = "RENDER_FORMAT";
            extensionParams[3].Value = "PDF";

            extensionParams[4].Name = "WRITEMODE";
            extensionParams[4].Value = "None"; //"Overwrite ";// "AutoIncrement";

            extensionParams[5].Name = "USERNAME";
            extensionParams[5].Value = "gmd";

            extensionParams[6].Name = "PASSWORD";
            extensionParams[6].Value = "password123";

            ExtensionSettings extensionSettings = new ExtensionSettings();
            extensionSettings.Extension = "Report Server FileShare"; // EXTENSION_FILESHARE;
            extensionSettings.ParameterValues = extensionParams;

            return extensionSettings;
        }
        static void generateSubscription()
        {
            string report = @"/MyReports/TestSSRSSubscrptionReport"; 

            string description = "My Test subscription2010";
            string eventType = "TimedSubscription";

            ExtensionSettings extSettings = GetExtensionSettings();


            List<ReportService2010.ParameterValue> parameters = new List<ReportService2010.ParameterValue>();            
            parameters.Add(new ReportService2010.ParameterValue() { Name = "EmployeeKey", Value = "9" });
            parameters.Add(new ReportService2010.ParameterValue() { Name = "SelectedColumn", Value = "EmployeeKey" });

            parameters.Add(new ReportService2010.ParameterValue() { Name = "ParamSelectedColumns", Value = "FirstName" });
            parameters.Add(new ReportService2010.ParameterValue() { Name = "ParamSelectedColumns", Value = "LastName" });

            NetworkCredential credentials = new NetworkCredential("gmd", "password123");
            ReportService2010.ReportingService2010 rs = new ReportService2010.ReportingService2010();
            rs.Credentials = credentials; // System.Net.CredentialCache.DefaultCredentials;

            DateTime topDatetime = DateTime.Now;
            topDatetime = topDatetime.AddMinutes(1);
            string scheduleXml = "<ScheduleDefinition><StartDateTime>";
            scheduleXml += topDatetime.ToShortDateString() + " " + topDatetime.ToShortTimeString();
            scheduleXml += "</StartDateTime></ScheduleDefinition>";

            string sid = rs.CreateSubscription(report, extSettings, description, eventType, scheduleXml, parameters.ToArray());
        }



回答3:


You could create a windows service that uses FileSystemWatcher (https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx) and then just trigger your job on the changed event.



来源:https://stackoverflow.com/questions/30502313/how-to-trigger-a-ssrs-subscription-based-on-an-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!