Sunday, February 19, 2012

Deploy Excel file that already exists on server - file isn't replaced

Dear all,

I am deploying programatically an Excel 2007 file to a SQL Server 2005 Reporting Server. The problem is that if a file with the same name already exists, that file isn't replaced. I would like the opposite to happen. I'm using the following code:

--Executable

set svr=http://w3sdwsqld1/reportserver
set src_fld="\\w3sdwsqld1\\deploy\\SAD\\ECRANS\\UPDATES_20061127_190000\\Ecrans\\AM\\Associados\\"
set dest_fld="Associados"
set script="\\w3sdwsqld1\\deploy\\SAD\ECRANS\\UPDATES_20061127_190000\\Ecrans\\AM\\Associados\\PublishReports.rss"
REM Sample: deploy.bat http://w3sdwsqld1/reportserver "\\w3sdwsqld1\\deploy\\SAD\\ECRANS\\UPDATES_20061127_190000\\Ecrans\\AM\\Associados\\" "Associados" "\\w3sdwsqld1\\deploy\\SAD\ECRANS\\UPDATES_20061127_190000\\Ecrans\\AM\\Associados\\PublishReports.rss"
for /R %src_fld% %%f in (*.xlsx) do rs -i %script% -s %svr% -v ParentFolder=%dest_fld% -v reportP="%%~nf" -v path=%src_fld%
PAUSE

--rss Code


'
' Script Variables
'
' Variables that are passed on the command line with the -v switch:
'
' (a) parentFolder - corresponds to the folder that the script creates and uses
' to contain your published reports

' (b) reportP - corresponds to the report to publish


Dim ROOT As String = "/SAD/Ecrans/Ecrans/AM"

Dim definition As [Byte]() = Nothing
Dim warnings As Warning() = Nothing
Dim parentPath As String = ROOT + "/"+ parentFolder
Dim filePath As String = path
Dim report As String = reportP


Public Sub Main()

rs.Credentials = System.Net.CredentialCache.DefaultCredentials

'Create the parent folder
Try
rs.CreateFolder(parentFolder, ROOT,Nothing)
Console.WriteLine("Parent folder {0} created successfully", parentFolder)
Catch e As Exception

Console.WriteLine(e.Message)

End Try

'Create shared data source
'CreateSampleDataSource("Solucao_Integrada", "OLEDB-MD", "Data Source=dwareas1;Initial Catalog=SAD_Solucao_Integrada")

'Publish the sample reports
PublishReport(report)


End Sub

Public Sub CreateSampleDataSource(name As String, extension As String, connectionString As String)
'Define the data source definition.
Dim definition As New DataSourceDefinition()
definition.CredentialRetrieval = CredentialRetrievalEnum.Integrated
definition.ConnectString = connectionString
definition.Enabled = True
definition.EnabledSpecified = True
definition.Extension = extension
definition.ImpersonateUser = False
definition.ImpersonateUserSpecified = True
'Use the default prompt string.
definition.Prompt = Nothing
definition.WindowsCredentials = False

Try
rs.CreateDataSource(name, parentPath, False, definition, Nothing)
Console.WriteLine("Data source {0} created successfully", name)

Catch e As Exception
Console.WriteLine(e.Message)
End Try

End Sub

Public Sub PublishReport(ByVal reportName As String)
Try
Dim stream As FileStream = File.OpenRead(filePath + reportName + ".xlsx")
Console.WriteLine(reportName)

definition = New [Byte](stream.Length) {}
stream.Read(definition, 0, CInt(stream.Length))
stream.Close()

Catch e As IOException
Console.WriteLine(e.Message)
End Try

Try
rs.CreateResource(reportName + ".xlsx", parentPath, True, definition, "application/x-excel", Nothing)

Catch e As Exception
Console.WriteLine(e.Message)
Console.WriteLine("Failed to publish report")
End Try
End Sub
--

Any thoughts? Many thanks,

Pedro Martins

Portugal

Pedro,

Try

It looks like the paths may not be correct, use the "filePath" that you use to open the file in the below statement.

rs.CreateResource(reportName + ".xlsx", filePath, True, definition, "application/x-excel", Nothing)

I normally created my Rss script insert console write message to return values so that I can make sure I getting the corrent values.

Let me know if this helps.

Ham

|||

Ham,

The path is correct, if I deploy the file and there is no other file in the destination the file is uploaded successfully. However, If a file with the same name already exists, the new file doesn't replace the old one.

Pedro

|||

Pedro

Is the file you are trying to replace - "Open" by another application? You could use the System.IO to check on the status of this file before you try and replace.

Ham

No comments:

Post a Comment