How to deploy a report (*.rdl) file to SQL Server Reporting Services using PowerShell

If you want to deploy a report (*.rdl) file to SQL Server Reporting Services in PowerShell, use the following script:

 

PowerShell script

"Set execution policy to [Unrestricted]"
Set-ExecutionPolicy Unrestricted

"Load assembly"
[System.Reflection.Assembly]::LoadFrom("C:\Temp\Ada.Cdf.dll")

"Create report"
$report = New-Object Ada.Cdf.Deployment.SSRS.Report
$report.SSRSWebServiceUrl = "http://localhost/ReportServer/ReportService2005.asmx"
$report.SSRSFolder = "ADA Sales Reports"
$report.FileSystemPath = "C:\Reports\AanvragenStats.rdl"
$report.Create()

 

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ada.Cdf.ReportService2005;
namespace Ada.Cdf.Deployment.SSRS
{
public class Report
{
public string FileSystemPath { get; set; }
public string SSRSWebServiceUrl { get; set; }
public string _ssrsFolder = string.Empty;
public string SSRSFolder 
{
get
{
// Use "/" as default value
string firstCharacter = "/";
if (string.IsNullOrEmpty(_ssrsFolder))
{
_ssrsFolder = firstCharacter;
}
// Report folder should start with one "/"
_ssrsFolder = firstCharacter + _ssrsFolder.TrimStart(firstCharacter.ToCharArray());
return _ssrsFolder;
}
set
{
_ssrsFolder = value;
}
}
/// <summary>
/// Create a report
/// </summary>
public void Create()
{
// Validate properties
if (string.IsNullOrEmpty(this.FileSystemPath)) { throw new ArgumentNullException("this.FileSystemPath"); }
if (!File.Exists(this.FileSystemPath)) { throw new ApplicationException(string.Format("The file [{0}] does not exist", this.FileSystemPath)); }
if (string.IsNullOrEmpty(this.SSRSWebServiceUrl)) { throw new ArgumentNullException("this.SSRSWebServiceUrl"); }
if (string.IsNullOrEmpty(this.SSRSFolder)) { throw new ArgumentNullException("this.SSRSFolder"); }
// Initialize webservice proxy
ReportService2005.ReportingService2005 rs = new ReportService2005.ReportingService2005();
rs.Url = this.SSRSWebServiceUrl;
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Determine filename without extension (used as name in SSRS)
FileInfo fileInfo = new FileInfo(this.FileSystemPath);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileInfo.FullName);
// Determine filecontents
byte[] fileContents = File.ReadAllBytes(fileInfo.FullName);
// Publish report
Warning[] warnings = rs.CreateReport(fileNameWithoutExtension, this.SSRSFolder, true, fileContents, null);
// Log warnings
if (warnings != null)
{
foreach (Warning warning in warnings)
{
Global.Logger.Warn(warning);
}
}
}
/// <summary>
/// Delete a report
/// </summary>
public void Delete()
{
// Initialize webservice proxy
ReportService2005.ReportingService2005 rs = new ReportService2005.ReportingService2005();
rs.Url = this.SSRSWebServiceUrl;
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Determine filename without extension (used as name in SSRS)
FileInfo fileInfo = new FileInfo(this.FileSystemPath);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileInfo.FullName);
// Determine full SSRS path
string ssrsPath = string.Format("{0}/{1}", this.SSRSFolder, fileNameWithoutExtension.TrimStart("/".ToCharArray()));
// Delete report
rs.DeleteItem(ssrsPath);
}
}
}

 

Result

image

7 Comments

  1. Dan

    Hi,

    Good job on your article.
    I’m working on deploy reports through TFS, when deploy reports to QA/Test, do I have to build the report sln/rptproj?

    Another question, does your dll deploy all reports or just modified reports? If is all reports, what do I need to change to accommodate to deploy only modified reports.

    Thanks in advance,
    Dan

  2. Roel van Lisdonk

    You can deploy report without building the report sln / proj, saving the reports is enough.
    De script will deploy all dll’s. You can ask the report server if the reports differ.

  3. Melby

    Where can i get the Reporting services 2005 from? using Ada.Cdf.ReportService2005;?

    I am trying to use the same code in a windows application to kinda come up with a installer kinda screen accepting the report server URL n other parameters. Is this possible?

  4. Roel van Lisdonk

    When you want to call the reportingservices2005 from C# code, you should first add a service reference, this services reference generates a dll in my case this dll has the name “Ada.Cdf.ReportService2005”

  5. shyqyri

    Hello.
    please help me!
    i create the report server and i publish the file report dsms.rdl. when i open the link of server report http://servername/reportserver is open the IIS tree,in the tree is and file report dsms.rdl, i wont to open with brosware wich html file and generate raport here.
    please help me .
    my english is bater 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *