If you want to use PowerShell to create a folder in SQL Server Reporting Services 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")

$folder = New-Object Ada.Cdf.Deployment.SSRS.Folder
$folder.SSRSWebServiceUrl = "http://localhost/ReportServer/ReportService2005.asmx"
$folder.Name = "ADA Sales Reports"
$folder.Parent = "/"
$folder.Create()

 

 

 

C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Ada.Cdf.Deployment.SSRS
{
    public class Folder
    {
        public string SSRSWebServiceUrl { get; set; }
        public string _parent = "/";
        public string Parent
        {
            get
            {
                // Use "/" as default value
                string firstCharacter = "/";
                if (string.IsNullOrEmpty(_parent))
                {
                    _parent = firstCharacter;
                }

                // Parent should start with one "/"
                _parent = firstCharacter + _parent.TrimStart(firstCharacter.ToCharArray());

                return _parent;
            }
            set
            {
                _parent = value;
            }
        }
        public string _name = string.Empty;
        public string Name
        {
            get
            {
                
                if (string.IsNullOrEmpty(_name))
                {
                    _name = string.Empty;
                }

                // FolderName should not start with"/"
                _name = _name.TrimStart("/".ToCharArray());

                return _name;
            }
            set
            {
                _name = value;
            }
        }

        /// <summary>
        /// Create a report
        /// </summary>
        public void Create()
        {

            // Validate properties
            if (string.IsNullOrEmpty(this.SSRSWebServiceUrl)) { throw new ArgumentNullException("this.SSRSWebServiceUrl"); }
            if (string.IsNullOrEmpty(this.Name)) { throw new ArgumentNullException("this.Name"); }
            if (string.IsNullOrEmpty(this.Parent)) { throw new ArgumentNullException("this.Parent"); }

            // Initialize webservice proxy
            ReportService2005.ReportingService2005 rs = new ReportService2005.ReportingService2005();
            rs.Url = this.SSRSWebServiceUrl;
            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;

            try
            {
                rs.
                // Create report folder, this will throw an exception if the folder already exists
                rs.CreateFolder(this.Name, this.Parent, null);
            }
            catch (Exception ex)
            {
                Global.Logger.Info("Folder already exists", ex);
            }

        }
        /// <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 full SSRS path
            string ssrsPath = string.Format("{0}/{1}", this.Parent.TrimStart("/".ToCharArray()), this.Name);

            // Delete report
            rs.DeleteItem(ssrsPath);

       }
    }
}

Result

image

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.