2
votes

I have Reporting Services running on SQL 2008 R2 and have a handful of reports that I created. I'm able to go into Report Server and set up a subscription and have any of the reports emailed to an email address. So all of that is configured correctly.

What I want to do is have a web page in my application that shows a list of available reports. The user can choose one, choose a schedule frequency, enter an email address, and click a 'save' button. When clicking save it should create the subscription in SSRS. I may need to pass in a couple report parameters depending on the report.

How can I do this in C#?

2

2 Answers

5
votes

You can dynamically generate a one time subscription in SSRS for the report. You'll have to use the RS webservice as mentioned by Diego.

Your code would look something like this:

static void generateSubscription()
{
    if (SubscriptionRequests.Count < 1) return;

    NetworkCredential credentials = new NetworkCredential("user", "pass");
    reports.ReportingService2005 rs = new reports.ReportingService2005();
    rs.Credentials = credentials;
    DateTime topDatetime = DateTime.Now;
    topDatetime = topDatetime.AddMinutes(2);

    foreach (SubscriptionRequest x in SubscriptionRequests)
    {
        reports.ExtensionSettings extensionSettings = new reports.ExtensionSettings();
        List<reports.ParameterValue> extParameters = new List<reports.ParameterValue>();
        List<reports.ParameterValue> parameters = new List<reports.ParameterValue>();
        string description = "Email: ";
        string eventType = "TimedSubscription";
        extensionSettings.Extension = "Report Server Email";

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

        parameters.Add(new reports.ParameterValue() { Name = "abc", Value = x.id });


        extParameters.Add(new reports.ParameterValue() { Name = "RenderFormat", Value = x.renderFormat });
        extParameters.Add(new reports.ParameterValue() { Name = "TO", Value = x.email });
        extParameters.Add(new reports.ParameterValue() { Name = "ReplyTo", Value = x.replyTo });
        extParameters.Add(new reports.ParameterValue() { Name = "IncludeReport", Value = "True" });
        extParameters.Add(new reports.ParameterValue() { Name = "Subject", Value = "subject - " + " (" + x.id.ToString() + ")" });

        extParameters.Add(new reports.ParameterValue() { Name = "Comment", Value = x.body });
        extensionSettings.ParameterValues = extParameters.ToArray();

        description += topDatetime.ToShortDateString() + " " + topDatetime.ToShortTimeString();
        description += " (" + x.a + " - " + x.b + " - " + x.c + ")";
        string _reportName = "/report";
        rs.CreateSubscription(_reportName, extensionSettings, description, eventType, scheduleXml, parameters.ToArray());
        topDatetime = topDatetime.AddSeconds(30);
    }           
}  
1
votes

Easiest way is give access to the user to the report manager under the "Browser" pre-defined role. This is exactly what this role is about, view folders and reports and subscribe to reports.

If that's not possible you can create your own management tool. To do that you need to access the SSRS web methods Using SOAP and the ReportService2005 endpoint

Examples here