-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAIP_get_SSRS_Report.cs
157 lines (133 loc) · 5.35 KB
/
AIP_get_SSRS_Report.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
// This sample code shows how to:
// - Call the SQL report server from Aras
// - Rename SQL Report
// - Load the report result to the Aras Vault
// - Create a new Document with the attached report
// This sample works direct from a Method
// With some simple modifications it can easy be used in an Action or Form button onClick event.
Innovator inn = this.getInnovator();
string itemID = "1234567891234567897B26F33247F90C"; // Id of Part for testing
// Get credentials from InnovatorServerConfig.xml
XmlDocument xml = CCO.Cache.GetCacheInfo("ApplicationXML");
string targetURL = "";
string reportServerURL = xml.SelectSingleNode("//ReportingServices/ReportServer").InnerText;
string reportUserDomain = xml.SelectSingleNode("//ReportingServices/Domain").InnerText;
string reportUser = xml.SelectSingleNode("//ReportingServices/User").InnerText;
//string reportUserPW = xml.SelectSingleNode("//ReportingServices/Password").InnerText; // works only with unencrypted PWs
// Pitfall: A SecureString object should never be constructed from a String, because the sensitive data is already subject to the memory persistence consequences of the immutable String class.
SecureString securedPW = new SecureString();
foreach (char c in xml.SelectSingleNode("//ReportingServices/Password").InnerText) // <- is this really a good approach??
{
securedPW.AppendChar(c);
}
securedPW.MakeReadOnly();
/* debug - define credentials manually - Use this block for first tests
string reportServerURL = "http://myreportserver/SSRS/";
string reportUserDomain = "";
string reportUser = "myuser";
string reportUserPW = "mypassword";
*/
// Define Report server
string subfolder = "MYSUBFOLDER";
string report = "PCBA_BOM"; // Name of .rdl
string command = "Render";
string format = "EXCEL"; // for xlsx use "EXCELOPENXML"
string fileExtension = "xls"; // xlsx
// Get properties from request item
Item myItem = inn.newItem("Part","get");
myItem.setAttribute("select", "item_number,major_rev");
myItem.setProperty("id",itemID);
myItem = myItem.apply();
if (myItem.isError())
{
return inn.newError("An error occured: " + myItem.getErrorDetail() );
}
string itemType = myItem.getProperty("itemType");
string itemNum = myItem.getProperty("item_number");
string major_rev = myItem.getProperty("major_rev");
string fileString = itemNum + "-" + major_rev + "." + fileExtension; // e.g. A123456-A01-3.xls
// Specify the path for storing the file temporary on the server
string path = @"C:\\temp\\SSRS\\" + fileString;
targetURL = reportServerURL +
"?%2f" + subfolder +
"%2f" + report +
"&rs:Command=" + command +
"&id=" + itemID +
"&rs:Format=" + format ;
try
{
System.Net.HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create( targetURL );
request.Credentials = new System.Net.NetworkCredential(
reportUser,
securedPW,
reportUserDomain );
request.Method = "GET";
System.Net.HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create);
System.IO.Stream stream = response.GetResponseStream();
byte[] buffer = new byte[1024];
int length = stream.Read(buffer, 0, 1024);
while (length > 0)
{
fs.Write(buffer, 0, length);
length = stream.Read(buffer, 0, 1024);
}
stream.Close();
fs.Close();
}
}
catch (WebException e)
{
using (Stream data = e.Response.GetResponseStream())
{
string text = new StreamReader(data).ReadToEnd();
return inn.newError(e.Status + "\n" + e.Message + "\n" + text);
}
}
catch (Exception e)
{
return inn.newError(e.Message);
}
// Check-in File to Aras Vault
Item fileItem = inn.newItem("File", "add");
fileItem.setAttribute("doGetItem", "0");
fileItem.setProperty("filename", fileString);
fileItem.attachPhysicalFile(path);
Item checkin_result = fileItem.apply();
if (checkin_result.isError())
{
inn.newError("Fehler beim File Check-In. Bitte Admin melden." + checkin_result.getErrorDetail());
}
// Delete temporary File from Server
try
{
FileInfo fileInfo = new FileInfo(path);
fileInfo.Delete();
}
catch (Exception e)
{
return inn.newError(e.ToString());
}
// Create new Document
Item docItem = inn.newItem("Document","add");
docItem.setAttribute("doGetItem", "0");
string sequenceVal = inn.getNextSequence("Default Document");
docItem.setAttribute("useInputProperties", "1");
docItem.setProperty("item_number", sequenceVal);
docItem.setProperty("name", "SSRS Report " + report + " " + fileString );
docItem.setProperty("description", "This document was automatically generated.");
// Create new Relationship and attach File
Item relItem = inn.newItem("Document File","add");
relItem.setAttribute("doGetItem", "0");
relItem.setProperty("related_id",fileItem.getID());
docItem.addRelationship(relItem) ;
// Execute query
Item resultItem = docItem.apply();
if (docItem.isError())
{
return inn.newError("An error occured: " + docItem.getErrorDetail() );
}
string docId = resultItem.getID();
return inn.newResult(docId); // id of new Document