Deploying PDFs, and more [SSRS]

One of my presentations last year covered the topic of automating SSRS deployments through the RS Utility

In that presentation I covered how to create data sources and how to deploy shared datasets and reports.  What I didn’t cover is how you’re supposed to deploy other items, such as images or PDFs.  Luckily I got inspired once more through a forum question, so here’s the script for exactly that!

For the interested reader: the scripts from my session can be downloaded through this follow-up post: Automating SSRS Deployment: Download.

The script below demonstrates how to deploy a PDF but the same technique can be used for any file type.

    'The following global variables need to be passed in through the rs command:
    'Dim name As String = "CatalogItem.pdf"
    'Dim parent As String = "/Test"

    Dim reportFile As String = "scripts\resources\" & Name

    Public Sub Main()
        Console.WriteLine("Running script DeployPDF.rss")

        Dim overwrite As Boolean = True
        Dim fileContent As Byte() = Nothing
        Dim warnings As Warning() = Nothing

        'Common CatalogItem properties
        Dim descprop As New [Property]
        descprop.Name = "Description"
        descprop.Value = ""
        Dim hiddenprop As New [Property]
        hiddenprop.Name = "Hidden"
        hiddenprop.Value = "False"

        'PDF-specific property
        Dim mimeTypeProp As New [Property]
        mimeTypeProp.Name = "MimeType"
        mimeTypeProp.Value = "application/pdf"

        Dim props(2) As [Property]
        props(0) = descprop
        props(1) = hiddenprop
        props(2) = mimeTypeProp

        Try
            'Read file from disk
            Dim stream As FileStream = File.OpenRead(reportFile)
            fileContent = New [Byte](stream.Length - 1) {}
            stream.Read(fileContent, 0, CInt(stream.Length))
            stream.Close()

            Dim item As CatalogItem
            item = RS.CreateCatalogItem("Resource", Name, Parent, overwrite, _
                                        fileContent, props, warnings)

            If Not (warnings Is Nothing) Then
                Dim warning As Warning
                For Each warning In warnings
                    Console.WriteLine("Warning: {0}", Warning.Message)
                Next warning
            Else
                Console.WriteLine("CatalogItem: {0} published successfully with no warnings" _
                                  , Name)
            End If

        Catch e As IOException
            Console.WriteLine(e.Message)
        Catch e As SoapException
            Console.WriteLine("Error : " + e.Detail.Item("ErrorCode").InnerText _
                              + " (" + e.Detail.Item("Message").InnerText + ")")
        End Try
        Console.WriteLine()
    End Sub

The most important line in all of the above is the one that calls the CreateCatalogItem method.

The first parameter of that function is called ItemType.  To find out what the possibilities are we need to look at the ListItemTypes method:

Item Type Description
Component A report part.
DataSource A data source.
Folder A folder.
Model A model.
LinkedReport A linked report.
Report A report.
Resource A resource.
DataSet A shared dataset.
Site A SharePoint site.
Unknown An item not associated with any known type.

 

A PDF is a resource so that’s the one we need!  When we specify Resource as value for the ItemType parameter the function expects an additional property through the Properties array parameter.  This differs from the deployment of a report or dataset.  The additional property is called MimeType and for PDFs the MIME type is application/pdf.

The script above can be called from a batch (.cmd) file, here’s a possibility:

@echo off

::Script Variables
SET REPORTSERVER=http://YourServer/ReportServer

::default location on 32-bit machines -> SET RS="C:\Program Files\Microsoft SQL Server\100\Tools\Binn\RS.EXE"
::default location on 64-bit machines -> SET RS="C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\RS.EXE"
SET RS="C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\RS.EXE"

SET LOGFILE="SSRS_DeployLog.txt"
SET SCRIPTLOCATION=scripts
SET TIMEOUT=60
SET ENDPOINT="Mgmt2010"

::Clear Log file
IF EXIST %logfile% DEL %logfile%

ECHO Starting deployment to %REPORTSERVER%

::Write Log Header
ECHO Starting deployment at %DATE% %TIME% >>%LOGFILE%
ECHO SCRIPTLOCATION = %SCRIPTLOCATION% >>%LOGFILE%
ECHO REPORTSERVER   = %REPORTSERVER% >>%LOGFILE%
ECHO TIMEOUT        = %TIMEOUT% >>%LOGFILE%
ECHO ENDPOINT        = %ENDPOINT% >>%LOGFILE%
ECHO COMPUTERNAME    = %COMPUTERNAME% >>%LOGFILE%
ECHO RS             = %RS% >>%LOGFILE%
ECHO. >>%LOGFILE%

::Run Scripts

ECHO ...deploying catalog items...

%RS% -i "%SCRIPTLOCATION%\DeployPDF.rss" -s %REPORTSERVER% -l %TIMEOUT% -e %ENDPOINT% -v name="SomePDF.pdf" -v parent="/Test" >>%LOGFILE% 2>&1

::Finish
ECHO. >>%LOGFILE%
ECHO Finished deployment at %DATE% %TIME% >>%LOGFILE%
ECHO. >>%LOGFILE%

ECHO Deployment finished!  See %LOGFILE% for details.
PAUSE

That’s it for today, have fun!

Valentino.

Share

Tags: , ,

  1. Josh’s avatar

    The variable for reportFile indicates the source file is already located in the report server catalog. Can this be modified to point at a source file located on a drive folder such as c:/reports? It’d be nice to automate those kinds of uploads as opposed to doing it manually or creating url links within pagination reports. Any suggestions?

    Reply

© 2008-2017 BI: Beer Intelligence? All Rights Reserved