-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReportingServicesUploader.cs
175 lines (153 loc) · 7.65 KB
/
ReportingServicesUploader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ReportingServicesBatchUpload
{
internal class ReportingServicesUploader
{
public string ReportingServicesWebServiceUrl { get; set; }
private ReportingServices2005WebService.ReportingService2005 _WebService { get; set; }
public ReportingServicesUploader()
{
_WebService = new ReportingServicesBatchUpload.ReportingServices2005WebService.ReportingService2005();
ReportingServicesWebServiceUrl = _WebService.Url;
}
/// <summary>
/// Uploads new .rdl file to SSRS
/// </summary>
/// <param name="reportName"></param>
/// <param name="rdlFilePath"></param>
/// <param name="serverFolder"></param>
/// <returns>List of warnings (null if successful with nor warnings</returns>
public IEnumerable<string> UploadResource(string resourceName, string filePath, string serverFolder, bool overwrite)
{
Byte[] definition = Initialize(filePath);
try {
_WebService.CreateResource(resourceName, serverFolder, overwrite, definition, GetMimeTypeFromFileName(resourceName), null);
}
catch (System.Web.Services.Protocols.SoapException) {
}
catch (NotSupportedException) {
return new string[] { string.Format("File type is not supported") };
}
return null;
}
/// <summary>
/// Uploads new .rdl file to SSRS
/// </summary>
/// <param name="reportName"></param>
/// <param name="rdlFilePath"></param>
/// <param name="serverFolder"></param>
/// <returns>List of warnings (null if successful with nor warnings</returns>
public IEnumerable<string> UploadDataSource(string rdsFilePath, string serverFolder, bool overwrite)
{
_WebService.Url = ReportingServicesWebServiceUrl;
_WebService.Credentials = System.Net.CredentialCache.DefaultCredentials;
string name;
var dataSource = GetDataSourceFromRds(rdsFilePath, out name);
try {
_WebService.CreateDataSource(name, serverFolder, overwrite, dataSource, null);
}
catch (System.Web.Services.Protocols.SoapException e) {
if (e.Message.CompareTo(string.Format("The item '{0}' cannot be found. ---> The item '{0}' cannot be found.", serverFolder)) == 0) {
CreateFolder(serverFolder);
return UploadDataSource(rdsFilePath, serverFolder, overwrite);
}
}
catch (NotSupportedException) {
return new string[] { string.Format("File type is not supported") };
}
return null;
}
private ReportingServicesBatchUpload.ReportingServices2005WebService.DataSourceDefinition GetDataSourceFromRds(string rdsFilePath, out string name)
{
var dataSource = new ReportingServices2005WebService.DataSourceDefinition();
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(rdsFilePath);
int propertyCount = doc.SelectNodes("/RptDataSource/ConnectionProperties/*").Count;
if (propertyCount != 2 && propertyCount != 3)
Console.Error.WriteLine("Please check the rds file. There are an unexpected number of connection properties");
dataSource.Extension = doc.SelectSingleNode("/RptDataSource/ConnectionProperties/Extension").InnerText;
dataSource.ConnectString = doc.SelectSingleNode("/RptDataSource/ConnectionProperties/ConnectString").InnerText;
name = doc.SelectSingleNode("/RptDataSource/Name").InnerText;
return dataSource;
}
private string GetMimeTypeFromFileName(string resourceName)
{
string extension = resourceName.Substring(resourceName.Length - 3);
switch (extension) {
case "bmp":
return "image/bmp";
case "png":
return "image/x-png";
case "jpg":
default:
throw new NotSupportedException(string.Format("Extension {0} not supported"));
}
}
/// <summary>
/// Uploads new .rdl file to SSRS
/// </summary>
/// <param name="reportName"></param>
/// <param name="rdlFilePath"></param>
/// <param name="serverFolder"></param>
/// <returns>List of warnings (null if successful with nor warnings</returns>
public IEnumerable<string> UploadReport(string reportName, string rdlFilePath, string serverFolder, bool overwrite)
{
Byte[] definition = Initialize(rdlFilePath);
ReportingServicesBatchUpload.ReportingServices2005WebService.Warning[] warnings = null;
try {
warnings = _WebService.CreateReport(reportName, serverFolder, overwrite, definition, null);
}
catch (System.Web.Services.Protocols.SoapException e) {
if (e.Message.CompareTo(string.Format("The item '{0}' cannot be found. ---> The item '{0}' cannot be found.", serverFolder)) == 0) {
CreateFolder(serverFolder);
return UploadReport(reportName, rdlFilePath, serverFolder, overwrite);
}
if (overwrite ||
e.Message.CompareTo(string.Format("The item '{0}/{1}' already exists. ---> The item '{0}/{1}' already exists.",serverFolder, reportName)) != 0)
return new string[] { e.Detail.InnerXml.ToString() };
}
IList<string> rc = ProcessWarnings(warnings);
return rc.Count != 0 ? rc : null;
}
private void CreateFolder(string serverFolder)
{
int lastSlash = serverFolder.LastIndexOfAny(new char[] { '\\', '/' });
string folder = serverFolder.Substring(lastSlash + 1);
string parent = serverFolder.Remove(lastSlash).Replace('\\', '/');
parent = string.IsNullOrEmpty(parent) ? "/" : parent;
try {
_WebService.CreateFolder(folder, parent, null);
}
catch (System.Web.Services.Protocols.SoapException e) {
if (e.Message.CompareTo(string.Format("The item '{0}' cannot be found. ---> The item '{0}' cannot be found.", parent)) == 0) {
CreateFolder(parent);
CreateFolder(serverFolder);
}
else
throw;
}
}
private static IList<string> ProcessWarnings(ReportingServicesBatchUpload.ReportingServices2005WebService.Warning[] warnings)
{
IList<string> rc = new List<string>();
if (warnings != null)
foreach (var warning in warnings)
rc.Add(warning.Message);
return rc;
}
private Byte[] Initialize(string rdlFilePath)
{
_WebService.Url = ReportingServicesWebServiceUrl;
_WebService.Credentials = System.Net.CredentialCache.DefaultCredentials;
Byte[] definition = null;
FileStream stream = File.OpenRead(rdlFilePath);
definition = new Byte[stream.Length];
stream.Read(definition, 0, (int)stream.Length);
stream.Close();
return definition;
}
}
}