An interesting question arose today:  how can I re-use a standard, centrally managed header/footer definition in all Reporting Services reports.  The intention being to reduce management overhead when the definition of a header/footer changes to avoid having to fix all individual reports.

A solution to this is to manipulate the Report Definition Language (RDL) files that Reporting Services uses to produce reports.  Each RDL file has a <PageHeader> and <PageFooter> node, in which the definition for the header/footer exists. By creating a standard reference RDL file that simply contains one of these nodes with the full header/footer definition, this reference node can then be copied over the appropriate node in all the report RDL files using a short piece of code.

The following is the basic C# code that uses the .NET framework's System.Xml namespace to manipulate a report file.  The purpose of the code is to:

  1. Delete an existing <PageHeader> section in the report file in question
  2. Read a standard <PageHeader> definition from the reference RDL file
  3. Insert this standard <PageHeader> definition into the report file, thus updating the report with the standard header definition
  4. The code should then loop back for all the files that are to be modified.  This could be accomplished by looping through all the files in a directory, or perhaps use  UI to get a list of files from a user.  This piece is not reflected in this code, however it could be easily extended to do so.

Here is the code:

  1. // Establish XmlDocument variables for standard header and existing report file to change
  2. XmlDocument xmlDStandardHeader = new XmlDocument();
  3. XmlDocument xmlReport = new XmlDocument();
  4. // Load RDL for existing report and establish the document root
  5. xmlReport.Load(report);
  6. XmlNode root = xmlReport.DocumentElement;
  7. // Remove the existing <PageHeader> element
  8. root.RemoveChild(xmlReport.GetElementsByTagName("PageHeader")[0]);
  9. // Load the RDL for the standard header and retrieve the <PageHeader> node
  10. xmlDStandardHeader.Load(tbStandardHeader.Text);
  11. XmlNode xmlNStandardHeader = xmlDStandardHeader.GetElementsByTagName("PageHeader")[0];
  12. // Import the standard <PageHeader> node into the report RDL file
  13. XmlNode xmlNNewHeader = xmlReport.ImportNode(xmlNStandardHeader, true);
  14. xmlReport.DocumentElement.AppendChild(xmlNNewHeader);
  15. // Save the RDL for the modified report
  16. xmlReport.Save(report);