RDL Object Model on the Horizon

As I mentioned in a previous blog, SQL Server 2008 will probably include an RDL Object Model. This is great news for developers who generate report definitions programmatically. No more custom RDL object models as the one I talked about during my TechEd presentation. The early incarnation of the promised object model is included in the July CTP (CTP4) and resides in the \Microsoft SQL Server\MSSQL.3\Reporting Services\ReportServer\bin\Microsoft.ReportingServices.RdlObjectModel.dll assembly.

DISCLAIMER Before rejoicing too much, recall the usual disclaimer that everything is in a flux and a subject to change. Although here, the RDL Object Model may very well disappear in the final bits.

Note that I disclaimed myself let me introduce you to the RDL Object Model (don’t try to find it in BOL; long live .NET Reflector!).

using System;

using System.IO;

using Microsoft.ReportingServices.RdlObjectModel;

using Microsoft.ReportingServices.RdlObjectModel.Serialization;

namespace RDL

{

class Program

{

static void Main(string[] args)

{

string idef = @”C:\Reports\Sales by Product.rdl”; // input report in RDL 2008 format

string odef = @”C:\Reports\Sales by Product1.rdl”; // output report in RDL 2008 format

Report report = null;

RdlSerializer serializer;

if (!File.Exists(idef)) return;

// deserialize from disk

using (FileStream fs = File.OpenRead(idef))

{

serializer = new RdlSerializer();

report = serializer.Deserialize(fs);

}

report.Author = “Teo Lachev”;

report.Description = “RDL Object Demo”;

// TODO: use and abuse RDL as you wish

// serialize to disk

using (FileStream os = new FileStream(odef, FileMode.Create))

{

serializer.Serialize(os, report);

}

}

}

}

As you can see, using the RDL Object Model is simple. Once you add the reference to (\Microsoft SQL Server\MSRS10.MSSQLSERVER\Reporting Services\ReportServer\bin\Microsoft.ReportingServices.RdlObjectModel.dll, you are work with RDL in object-oriented way. You use the RDLSerializer class to load a report definition (Deserialize method) or get RDL from a report object (Serialize method). Both methods support various overloads to read/write from/to stream, XmlReader, and more. I load an existing Sales by Product report into the Report object using the RDLSerializer.Deserialize() method. Note that it must be saved in the SSRS 2008 format. You need to use the stand-alone Report Designer to do so. If you use the SSRS 2005 format you will get an exception because you will need to upgrade to RDL 2008 (see next paragraph about how to upgrade RDL). From there, I write the report object back to disk as a Sales by Product1.rdl file. What could be easier? Compare this with XmlDom programming and you will start seeing how the RDL Object Model can make your life easier.

There are also methods for upgrading RDL to SSRS 2008 (Microsoft.ReportingServices.ReportProcessing.RDLUpgrader .UpgradeToCurrent) and SSRS 2005 (Microsoft.ReportingServices.ReportProcessing.RDLUpgrader .UpgradeTo2005) formats.

An RDL object model has been long due on my wish list. Kudos to the SSRS team for materializing it.