Removing multiple SSRS reports in subfolders using command line?
I am new to SSRS deployment and I have searched for it but cannot find the exact scenario that applies to my situation. I have multiple reports deployed in different subfolders in SSRS 2012. For example:
- Sale / salesReport 1
- Sale / salesReport 2
- Finance / finReport 1
- Finance / finReport 2
- Miscellaneous / miscReport 1
- Miscellaneous / miscReport 2
Due to the fact that sometimes SSRS doesn't like a rewritable report file, I would like to delete all report files in the instance before deploying any new reports. I can successfully delete folders and their contents by calling this script with 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 problem is, if any of those folders don't exist, the script will stop. I am trying to figure out the best way to handle this. Is it best to clear the folders and leave them there, or find a way to ignore the errors and continue?
Depending on the answer, how would I call this in a script?
source to share
You can check if a folder exists by running items in the parent folder and find all child folders with the expected name. Then, only if it exists, try deleting the folder.
This should fix your problem i.e. you only delete when you know it exists.
Your code might look something like this:
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 only checks the folder /
- you may need to tweak it according to your settings.
source to share