Skip to main content

Programmatically Playing With SSRS Subscriptions

SQL Server Reporting Services offers programmatically handling various report subscriptions. You can read the specific subscriptions and change those in code-behind as required. I assume that you are already aware about the subscription mechanism in reporting services. For some of you who are not aware about this, I would like to write that subscription in reporting services is nothing but a automated service (sql server job) defined and set by you to deliver reports at specific time or in specific events. You also define in which format the report will be presented to the user. Now whatever subscription properties you have set, everything will be stored in the ReportServer database. Schedule, Subscriptions and ReportSchedule tables contain alll those informations.

Now create a Windows or Web Application. Add a Web Reference towords
ReportingService2006. If SSRS is isntalled on your system then you can easily find the ReportService2006.asmx in the following url:

http://servername/_vti_bin/ReportServer/ReportService2006.asmx

If report server is not installed on your system then you can find this web service in any remote report server using the above mentioned url (server name has to be replaced).

Create an object of the ReportingService2006 web service. Declare the following variables in order to get the retrieved subscription properties:

ActiveState active = null;
ParameterValueOrFieldReference[] extensionParams = null;
ExtensionSettings extSettings = null;
ParameterValue[] values; a

string desc = string.Empty;
string eventType = string.Empty;
string matchData = string.Empty;


Call the GetSubscriptionProperties method of ReportingService2006 web service by passing the Subscription ID of the Subscription which needs to be changed:

ReportingService2006 rs = new ReportingService2006();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
rs.GetSubscriptionProperties(txtSubscriptionID.Text.Trim(), out extSettings, out desc, out active, out status, out eventType, out matchData, out values);

Different parameter details for this method are as follows:
Subscription ID : It is the unique id of the subscription which needs to be modified.
Extension Settings: It is the report deilvery extension and its configurable settings. It is an output parameter.
Description: It contains some meaningful description which will be displayed to the user. It is an output parameter.
ActiveState: It returns the ActiveState of the specified subscription. It is an output parameter. Status: It is the status of the Subscription and an output parameter.
EventType: It returns the type of event that triggers the subscription. It is an output parameter.
MatchData: It returns xml data specific to the report execution and deilvery scheduling process. It is an output parameter.
ParameterValue[]: It is the collection of different report parameters for the report. It is also an output parameter.

To get all extension settings returned from the web service, you can use the following code:

ParameterValueOrFieldReference[] extensionParams = extSettings.ParameterValues;
foreach (ParameterValueOrFieldReference
extensionParam in extensionParams)
{
if (((ParameterValue)extensionParam).Name.Equals("TO"))
{
txt_TO.Text = ((ParameterValue)extensionParam).Value;
}
if (((ParameterValue)extensionParam).Name.Equals("CC"))
{
txt_CC.Text = ((ParameterValue)extensionParam).Value;
}
if (((ParameterValue)extensionParam).Name.Equals("BCC"))
{
txt_BCC.Text = ((ParameterValue)extensionParam).Value;
}
if (((ParameterValue)extensionParam).Name.Equals("ReplyTo"))
{
txt_ReplyTo.Text = ((ParameterValue)extensionParam).Value;
}
if (((ParameterValue)extensionParam).Name.Equals("Subject"))
{
txt_Sub.Text = ((ParameterValue)extensionParam).Value;
}
if (((ParameterValue)extensionParam).Name.Equals("Comment"))
{
txt_Comment.Text = ((ParameterValue)extensionParam).Value;
}
if (((ParameterValue)extensionParam).Name.Equals("Priority"))
{
txt_Priority.Text = ((ParameterValue)extensionParam).Value;
}
}

As I wrote earlier, matchdata parameter returns xml string which needs to be parsed first in order to change or modify it. You can always fire a select statement in the Subscription table on the ReportServer database to see how matchdata column looks like. But the pre-requisite is that you have to create a report subscription first. Understand the xml definition of the match data with different schedule durations and the parse and bind it to the different control as per your requirement.

After binding all informations retrieved from ReportingService2006, its your turn to change the data as per the requirement. Now to save the subscription, please note that you need to again create a xml string that contains the modified report scheduling defenition. Finally call the
SetSubscriptionProperties method of the ReportingService2006 web service.


rs.SetSubscriptionProperties(subscriptionID, extSettings, desc, eventType, xmlScheduling, values);

It will update the Subscription definiton in the ReportServer database. It is very important to be noted that the matchdata which is generated dynamically should be consistent with the exact defenition. There are five types of schedule durations which are Once, Hourly, Daily, Hourly, Weekly and Monthly. For each different schedule durations, the xml schema are less or more differents. So its my suggestion to you to not to use SetSubscriptionProperties method to change the subscription with out understanding the matchdata format for different schedule durations. Otherwise, report subscription may be corrupted for improper matchdata.

Following link may be useful during your implementation:

http://technet.microsoft.com/en-us/library/reportservice2006.reportingservice2006_members.aspx


All the best.

Comments

Popular posts from this blog

Some facts on Sharepoint 2010

By following Lee Richardson's blog , I came to know about few good and few shocking news about Sharepoint 2010. Lets start with some good news: (i) Sharepoint 2010 will have inline list editing with AJAX Support. (ii) Workflows created using Sharepoint designer can be resued by the same way what Visual Studio does (iii) Sharepoint 2010 will support ASP.NET Data Services API. (iv) BDC will also insert or update the data. (v) You can edit Sharepoint Solutions without touching XML! Whooo!!! To me, shocking news is related to the requirement part: (i) Windows Server 2008 will be needed. So what will happen to me if I have Windows 2003? (ii) 64 bit SQL Server 2005/2008 is needed. (iii) There will be no support for Sharepoint 2010 in IE6. I am not sure whether it is because of AJAX or not.

IIS Manager Showing Blank Screen

I was shocked by one problem that I experienced while developing a SharePoint application. I was trying to open IIS Manager but it was showing me a blank screen. Even if I was trying to connect to localhost, it was denying. After being disturbed for a while, I came to know that OWSTimer.exe wants to access IIS ADSI (Active Directory Service Interface). OWSTime.exe has multiple threads and those are trying to access the IIS at the same time. That is why IIS Server Manager suddenly goes blank. OWSTimer.exe is a Windows Sharepoint Services timer process which is responsible to execute defined tasks in specific time interval in background. I restarted the OWSTimer.exe service and believe me it worked like a magic. Restarting IIS also forces all running threads to release their access. So that also would have solved my problem. By the way, this may be a small issue but when I experienced it, felt quite surprised.