External SSRS images with dynamic file extensions
I have a report that needs to show images from a Windows folder on a server that is running (see here ). Now I am wondering how to get a report for generating images of different file types like jpg and tif. (I am using png by default). Is there a relatively simple way to do this? Image names with file extension are not in the SQL database.
EDIT: I entered this in a custom code block, from Daniel's help below.
Public Function GetImage(ByRef Filename As String) As String
' Full image path used for testing if it exists
Dim ImagePath As String
ImagePath = "\\GVSSERVER1\GVSServerD\Acclamare_Images\" + Filename
' Test to see if the file exists as a gif
Try
If System.IO.File.Exists(ImagePath + ".png")
Return "file://" + ImagePath + ".png"
ElseIf System.IO.File.Exists(ImagePath + ".jpg")
Else Return "file://" + ImagePath + ".jpg"
End If
Catch ex As Exception
Return "Hit an Error"
End Try
Return "Hit the end"
End Function
When I run the report, it extracts the .jpg extension even though the image file is png and there is no jpg file for that item. Any idea on how to fix this?
EDIT 2: I had no success with the updated custom code, but I might be missing something since I'm not an expert with custom code. I found this question (see here) that is for a function. I tried and it works, except the .tif files are not showing up in the report. I installed Microsoft Picture Manager (from Shareex exe download) but it still doesn't display .tif files.
source to share
The good news is this is definitely possible, however it requires a bit of custom code and server-side customization to get it ready.
General idea . Create the code behind the function that takes the name of our image and then runs tests for the existence of the file to determine what type of extension actually exists on the network share.
If you right click outside of the report area and go to properties, you will see a custom code window where you can paste the following function code.
Custom code:
Public Function GetImage(ByRef Filename As String) As String
' Full image path used for testing if the image exists
Dim ImagePath As String
ImagePath = "\\EM-SSRS\ImageTest\" + Filename
' Test to see if the file exists as a gif
If System.IO.File.Exists(ImagePath + ".gif") THEN
Return "file://" + ImagePath + ".gif"
ElseIf System.IO.File.Exists(ImagePath + ".png") THEN
Return "file://" + ImagePath + ".png"
ElseIf System.IO.File.Exists(ImagePath + ".jpg") THEN
Return "file://" + ImagePath + ".jpg"
ElseIf System.IO.File.Exists(ImagePath + ".jpeg") THEN
Return "file://" + ImagePath + ".jpeg"
End If
Return "No Image Exists"
End Function
You will need to edit the ImagePath variable to contain the network shared path for your script, or even add another parameter to the function to make it more general if you like.
Once the code function is created, I would suggest creating a dummy textbox in the report and using the following value:
=Code.GetImage("Filenmame")
This will allow you to view the output of the function and change settings as needed. Note that the "file: // ..." syntax may not work from Report Builder or Visual Studio, and may be deployed to a report server for testing.
Of course, once the function works, add an image, make sure the source is set to external , and use the same expressions as for the textbox.
Server side changes
Upon further testing in my own environment, I had to make two additional changes to get this to work:
-
Make sure that the account is automatic control for the domain user account that has permission to share files
-
edit the SSRS configuration in rssrvpolicy.config to trust the custom code and allow the File.Exists function to execute with "FullTrust"
<CodeGroup
class="UnionCodeGroup"
version="1'
PermissionSetName="FullTrust"
Name="Report_Expressions_Default_Permissions"
Description="This code group grants default permissions for code in report expressions and code element."
...
</CodeGroup>
I restarted SSRS after making these changes (I assume this is necessary, but did it as a precaution)
Please note that I am not an SSRS server administrator and someone else might add more information about custom policy changes. Taking a look at the MS documentation you can recommend using custom assemblies, so take this part with a grain of salt.
source to share