5

I'm new to SSRS deployment, and I've been searching around but can't quite find the exact scenario that applies to my situation. I have several reports deployed across different subfolders in SSRS 2012. For example:

  • Sales/salesReport1
  • Sales/salesReport2
  • Finance/finReport1
  • Finance/finReport2
  • Misc/miscReport1
  • Misc/miscReport2

Due to the fact that sometimes SSRS doesn't like an overwritten report file, I would like to delete all the report files in the instance before deploying any new reports. I can successfully remove the folders and their contents by calling this script using rs.exe:

Public Sub Main()  

  rs.Credentials = System.Net.CredentialCache.DefaultCredentials  

  Dim bh As New BatchHeader()  

  bh.BatchID = rs.CreateBatch()  

  rs.BatchHeaderValue = bh 

   'Delete all reports from Main Folder and sub folders.Note:The Folders will be deleted also.  
   'If do not want to delete the folder, we could use rs.CreateFolder(“Folder name”, “/Main     Folder”, “nothing”) to create a new one with nothing.  

 rs.DeleteItem("/Sales")
 rs.DeleteItem("/Finance")
 rs.DeleteItem("/Misc") 

   'Delete all reports from a sub folder, and delete the sub folder  
   'rs.DeleteItem("/Main Folder/Sub Folder ")  

  rs.BatchHeaderValue = bh       

  ' Delete folders using batch header.  

  Try  

     rs.ExecuteBatch()  
     Console.WriteLine("Folders deleted successfully.")  

  Catch e As SoapException  
     Console.WriteLine(e.Detail.InnerXml.ToString())  

  Finally  
      rs.BatchHeaderValue = Nothing 

  End Try  

End Sub 'Main 

The issue lies in the fact that if any of those folders doesn't exist, the script will stop. I'm trying to determine the best way to handle this. Would the best way be to empty the folders and leave them there, or to find a way to ignore the errors and continue?

Depending on the answer, how would I invoke that in this script?

edgesrazor
  • 108
  • 6

2 Answers2

3

You can check if the folder exists by running through the items in the parent folder and looking for any child folders with the expected name. Then, only if this exists, try deleting the folder.

This should solve your issue, i.e. you only delete when you know it exists.

Your code might look something like:

Public Sub Main()

    rs.Credentials = System.Net.CredentialCache.DefaultCredentials

    DeleteFolder("Sales")
    DeleteFolder("Finance")

End Sub

Public Sub DeleteFolder(ByVal folderName As String)

    Dim items() as CatalogItem  
    Dim item As CatalogItem
    Dim folderExists As Boolean = False

    items = rs.ListChildren("/", False)

    For Each item In items

        If item.TypeName = "Folder" And item.Name.Equals(folderName)

            folderExists = True

        End If

    Next

    If folderExists

        Try

            rs.DeleteItem("/" + folderName)
            Console.WriteLine("Deleted folder {0}", folderName)

        Catch e As Exception

            Console.WriteLine(e.Message)

        End Try     

    Else

        Console.WriteLine("Folder {0} does not exist", folderName)

    End If

End Sub

This checks the / folder only - you may have to tweak to match your setup.

Ian Preston
  • 38,816
  • 8
  • 95
  • 92
  • 1
    This is exactly what I was looking for - thank you. One small edit - you'll get an error using `Item.TypeName = "Folder"`, so you'll want to change it to: `If item.Type = ItemTypeEnum.Folder`. – edgesrazor Sep 24 '14 at 13:28
1

I completed the accepted answer to support:

  • Deleting a folder which is not in root
  • Deleting contents of a folder only
  • Supporting SSRS 2017
  • Can be used as a rss file to be used in rs.exe tool too

So the code would be :

Dim TargetRSFolder As String = vTARGETRSFOLDER 
Dim TargetRSRoot As String = vTARGETROOT
Dim DeleteContentOnly As Boolean = vDeleteContentOnly 

Public Sub Main()

    rs.Credentials = System.Net.CredentialCache.DefaultCredentials

    DeleteFolder(TargetRSFolder)

End Sub

Public Sub DeleteFolder(ByVal folderName As String)

    Dim items() as CatalogItem  
    Dim item As CatalogItem
    Dim folderExists As Boolean = False
    Dim path As String = TargetRSRoot

    IF (path = "")
         path = "/"
    END IF


    items = rs.ListChildren(path, False)

    For Each item In items

        If item.Type.ToString() = "Folder" And item.Name.ToLower() = folderName.ToLower()

            folderExists = True

        End If

    Next

    If folderExists

        Try

            IF (DeleteContentOnly = True)   
                    items = rs.ListChildren(TargetRSRoot + "/" + folderName, False)
                     For Each item In items
                          rs.DeleteItem(item.Path)   
                     Next
                     Console.WriteLine("Deleted folder's contetnts : {0}", folderName)
            ELSE
                   rs.DeleteItem(TargetRSRoot + "/" + folderName)
                   Console.WriteLine("Deleted folder :{0}", folderName)
            END IF

        Catch e As Exception

            Console.WriteLine(e.Message)

        End Try     

    Else

        Console.WriteLine("Folder {0} does not exist", folderName)

    End If

End Sub

Assume we have the above codes in DeleteServerReportFolder.rss file, and we have "/bb/cc" folder.

  1. to delete cc folder's contents only:

rs.exe -i DeleteServerReportFolder.rss -s http://server2/ReportServer2017 -v vTARGETRSFOLDER=cc -v vTARGETROOT=/bb -v vDeleteContentOnly=true

  1. To delete bb folder completely:

rs.exe -i DeleteServerReportFolder.rss -s http://server2/ReportServer2017 -v vTARGETRSFOLDER=bb -v vTARGETROOT= -v vDeleteContentOnly=false

Mahmoud Moravej
  • 8,705
  • 6
  • 46
  • 65