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:
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With