Pro SQL Server 2012 Reporting Services

(sharon) #1

CHAPTER 10  MANAGING REPORTS


Delivering the Report


In the example, you’ve used PDF to deliver the report to the subscription user. You’ve also hard-coded
the e-mail address, which isn’t practical in the real world. One other issue of concern, especially in the
health care setting, is complying with HIPAA and protecting patient information.
You could give the user a textbox with which to enter the e-mail address to which the user wants the
report delivered. However, the user could possibly type in an incorrect e-mail address and deliver the
report to the wrong person. It would be great if the user’s e-mail address could be filled in automatically,
to make sure it is the correct address. You can do this by pulling the address from a field in a table in the
database similar to the data driven subscription example given earlier in the chapter where you pulled
the e-mail address from the Employee table. However, in this case, the user pulling the report may not
be in the database table, and you want the report delivered automatically to the user scheduling it.
Fortunately, the .NET Framework and Active Directory offer an easy way to do this. For many
organizations using Microsoft Exchange Server, e-mail addresses are integrated with Active Directory. If
you aren’t using Exchange Server, e-mail addresses aren’t integrated with Active Directory, but you can
still enter them into Active Directory manually.
Let’s create a method that determines the e-mail of the currently logged-in user. Then you can use it
to provide the To e-mail address for the subscription. Start by adding a new reference to the project for
System.DirectoryServices. Select References under the SSRS Viewer RVC project in your solution, and
then select Add Reference. In the Add Reference dialog box under the .NET tab, select
System.DirectoryServices from the list of component names. Next, add using statements to simplify your
typing, as follows:

using System.DirectoryServices;
using System.Security.Principal;

To find the current user’s e-mail address, use DirectorySearcher, which allows you to perform
queries against Active Directory, as shown in Listing 10-10. You’ll start at the root level of the directory
and look for the user by name. When you find the user’s name, you return the first e-mail address you
find for the user.

Listing 10-10. Code to Query Active Directory for E-mail Addresses

private string GetEmailFromAD()
{
DirectoryEntry rootEntry;
DirectoryEntry contextEntry;
DirectorySearcher searcher;
SearchResult result;
string currentUserName;
string contextPath;
WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
currentUserName = wp.Identity.Name.Split('\\')[1];
rootEntry = new DirectoryEntry("LDAP://RootDSE");
contextPath = rootEntry.Properties["defaultNamingContext"].Value.ToString();
rootEntry.Dispose();
contextEntry = new DirectoryEntry("LDAP://" + contextPath);
searcher = new DirectorySearcher();
searcher.SearchRoot = contextEntry;
searcher.Filter = String.Format("(&(objectCategory=person)(samAccountName={0}))",
currentUserName);
searcher.PropertiesToLoad.Add("mail");
Free download pdf