/
Program.cs
300 lines (256 loc) · 11.7 KB
/
Program.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Description;
using System.Text;
using System.Xml;
using OfficeOpenXml;
namespace ExportData
{
class Program
{
static IOrganizationService orgService;
static void Main(string[] args)
{
Console.WriteLine("Start : " + DateTime.Now);
string url = System.Configuration.ConfigurationManager.AppSettings["url"].ToString();
string username = System.Configuration.ConfigurationManager.AppSettings["username"].ToString();
string Password = System.Configuration.ConfigurationManager.AppSettings["Password"].ToString();
Console.WriteLine("Connection URL: " + url);
Console.WriteLine("Connection UserName : " + username);
Console.WriteLine("Connection Password : " + Password);
Console.WriteLine("Connecting to CRM service : " + DateTime.Now);
//System.Threading.Thread.Sleep(2000);
ConnectToMSCRM(username, Password, url);
//Test connection
Guid userid = ((WhoAmIResponse)orgService.Execute(new WhoAmIRequest())).UserId;
if (userid != Guid.Empty)
{
Console.WriteLine("Connection Established : " + DateTime.Now);
}
EName:
Console.WriteLine("Please specify entity name");
var entityname = Console.ReadLine();
if (entityname.Length == 0) goto EName;
Efolder:
Console.WriteLine("Please select destination file type: \n 1: for .txt \n 2: for .xlsx \n 3: for .csv");
var filepath = Console.ReadLine();
if (filepath.Length == 0) goto Efolder;
else if (filepath != "1" && filepath != "2" && filepath != "3")
{
Console.WriteLine("Incorrect file extension selected");
goto Efolder;
}
Console.WriteLine("Fetching Data : " + DateTime.Now);
// Set the number of records per page to retrieve.
int fetchCount = 5000;
// Initialize the page number.
int pageNumber = 1;
// Initialize the number of records.
int recordCount = 0;
// Specify the current paging cookie. For retrieving the first page,
// pagingCookie should be null.
string pagingCookie = null;
string fetchXML = @"<fetch version='1.0' mapping='logical' output-format='xml - platform'><entity name = '" + entityname + "'><all-attributes/></entity></fetch> ";
DataTable dtData = new DataTable();
while (true)
{
// Build fetchXml string with the placeholders.
string xml = CreateXml(fetchXML, pagingCookie, pageNumber, fetchCount);
// Excute the fetch query and get the xml result.
RetrieveMultipleRequest fetchRequest1 = new RetrieveMultipleRequest
{
Query = new FetchExpression(xml)
};
EntityCollection returnCollection = ((RetrieveMultipleResponse)orgService.Execute(fetchRequest1)).EntityCollection;
if (dtData.Columns.Count == 0)
{
if (returnCollection.Entities.Count > 0)
{
foreach (var item in returnCollection.Entities[0].Attributes)
{
dtData.Columns.Add(item.Key);
}
}
}
foreach (Entity record in returnCollection.Entities)
{
DataRow dr = dtData.NewRow();
foreach (KeyValuePair<string, object> item in returnCollection.Entities[0].Attributes)
{
if (!dtData.Columns.Contains(item.Key)) dtData.Columns.Add(item.Key);
dr[item.Key] = SantizeData(record, item);
}
dtData.Rows.Add(dr);
}
// Check for morerecords, if it returns 1.
if (returnCollection.MoreRecords)
{
Console.WriteLine("pageNumber: " + pageNumber + " Record Count " + dtData.Rows.Count + " : " + DateTime.Now.ToString());
Console.WriteLine("\n********************************");
// Increment the page number to retrieve the next page.
pageNumber++;
// Set the paging cookie to the paging cookie returned from current results.
pagingCookie = returnCollection.PagingCookie;
}
else
{
// If no more records in the result nodes, exit the loop.
break;
}
}
Console.WriteLine("Total Record Fetch :" + dtData.Rows.Count);
Console.WriteLine("End : Data Fetch" + DateTime.Now);
Console.WriteLine("Start : File Creation " + DateTime.Now);
switch (filepath)
{
case "1":
Write(dtData, entityname + ".txt");
break;
case "2":
string xlsxpath = entityname + ".xlsx";
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (ExcelPackage pck = new ExcelPackage())
{
pck.File = new FileInfo(xlsxpath);
ExcelWorksheet ws = pck.Workbook.Worksheets.Add(entityname);
ws.Cells["A1"].LoadFromDataTable(dtData, true);
pck.Save();
}
break;
case "3":
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = dtData.Columns.Cast<DataColumn>().
Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in dtData.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText(entityname + ".csv", sb.ToString());
break;
}
Console.WriteLine("End : File Creation " + DateTime.Now);
Console.ReadLine();
}
static void Write(DataTable dt, string outputFilePath)
{
int[] maxLengths = new int[dt.Columns.Count];
for (int i = 0; i < dt.Columns.Count; i++)
{
maxLengths[i] = dt.Columns[i].ColumnName.Length;
foreach (DataRow row in dt.Rows)
{
if (!row.IsNull(i))
{
int length = row[i].ToString().Length;
if (length > maxLengths[i])
{
maxLengths[i] = length;
}
}
}
}
using (StreamWriter sw = new StreamWriter(outputFilePath, false))
{
for (int i = 0; i < dt.Columns.Count; i++)
{
sw.Write(dt.Columns[i].ColumnName.PadRight(maxLengths[i] + 2));
}
sw.WriteLine();
foreach (DataRow row in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
if (!row.IsNull(i))
{
sw.Write(row[i].ToString().PadRight(maxLengths[i] + 2));
}
else
{
sw.Write(new string(' ', maxLengths[i] + 2));
}
}
sw.WriteLine();
}
sw.Close();
}
}
public static string SantizeData(Entity c, KeyValuePair<string, object> field)
{
string data = "";
if (c.Attributes.ContainsKey(field.Key))
{
if (field.ToString().Contains("OptionSetValue"))
{
data = ((Microsoft.Xrm.Sdk.OptionSetValue)field.Value).Value.ToString();
}
else if (field.ToString().Contains("EntityReference"))
{
data = ((Microsoft.Xrm.Sdk.EntityReference)field.Value).LogicalName + " : " + ((Microsoft.Xrm.Sdk.EntityReference)field.Value).Name + " : " + ((Microsoft.Xrm.Sdk.EntityReference)field.Value).Id;
}
else { data = c.Attributes[field.Key].ToString(); }
}
return data;
}
public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)
{
try
{
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = UserName;
credentials.UserName.Password = Password;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Uri serviceUri = new Uri(SoapOrgServiceUri);
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);
proxy.EnableProxyTypes();
orgService = (IOrganizationService)proxy;
}
catch (Exception ex)
{
Console.WriteLine("Error while connecting to CRM " + ex.Message);
Console.ReadKey();
}
}
public static string CreateXml(string xml, string cookie, int page, int count)
{
StringReader stringReader = new StringReader(xml);
XmlTextReader reader = new XmlTextReader(stringReader);
// Load document
XmlDocument doc = new XmlDocument();
doc.Load(reader);
return CreateXml(doc, cookie, page, count);
}
public static string CreateXml(XmlDocument doc, string cookie, int page, int count)
{
XmlAttributeCollection attrs = doc.DocumentElement.Attributes;
if (cookie != null)
{
XmlAttribute pagingAttr = doc.CreateAttribute("paging-cookie");
pagingAttr.Value = cookie;
attrs.Append(pagingAttr);
}
XmlAttribute pageAttr = doc.CreateAttribute("page");
pageAttr.Value = System.Convert.ToString(page);
attrs.Append(pageAttr);
XmlAttribute countAttr = doc.CreateAttribute("count");
countAttr.Value = System.Convert.ToString(count);
attrs.Append(countAttr);
StringBuilder sb = new StringBuilder(1024);
StringWriter stringWriter = new StringWriter(sb);
XmlTextWriter writer = new XmlTextWriter(stringWriter);
doc.WriteTo(writer);
writer.Close();
return sb.ToString();
}
}
}