/// <summary> /// Runs the profile. /// </summary> /// <param name="profile">The profile.</param> public void RunProfile(MSCRMDataExportProfile profile) { LogManager.WriteLog("Running Data Export Profile: " + profile.ProfileName); DateTime now = DateTime.Now; ReportFileName = Folder + "\\" + profile.ProfileName + "\\ExecutionReports\\DataExportReport" + now.Year + "-" + now.Month + "-" + now.Day + "-" + now.Hour + "-" + now.Minute + "-" + now.Second + ".xml"; //Initialize Execution Reports folder string executionReportsFolder = Folder + "\\" + profile.ProfileName + "\\ExecutionReports"; if (!Directory.Exists(executionReportsFolder)) { Directory.CreateDirectory(executionReportsFolder); } ExportedRecordsNumber = 0; //Create Data Export Report DataExportReport der = new DataExportReport(profile.ProfileName); WriteReport(der, ReportFileName); //Export data Export(profile, ReportFileName); DataExportReport report = ReadReport(ReportFileName); report.DataExportFinishedAt = DateTime.Now.ToString(); report.DataExportCompleted = true; TimeSpan ExportTimeSpan = DateTime.Now - Convert.ToDateTime(report.DataExportStartedAt); report.DataExportedIn = ExportTimeSpan.ToString().Substring(0, 10); WriteReport(report, ReportFileName); }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string[] filePath = openFileDialog1.FileNames; foreach (string fileLoc in filePath) { // Code to read the contents of the text file if (File.Exists(fileLoc)) { this.reportFileName = fileLoc; try { report = dem.ReadReport(reportFileName); } catch (Exception ex) { MessageBox.Show("Invalid Report File: \n" + ex.Message); } labelDataExportProfileName.Text = report.DataExportProfileName; labelExportCompleted.Text = (report.DataExportCompleted) ? "Yes" : "No"; labelExportStartedAt.Text = report.DataExportStartedAt; labelExportFinishedAt.Text = report.DataExportFinishedAt; labelExportedIn.Text = report.DataExportedIn; labelExportedRecords.Text = report.TotalExportedRecords.ToString(); } } } }
private void Form1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop)); foreach (string fileLoc in filePaths) { // Code to read the contents of the text file if (File.Exists(fileLoc)) { this.reportFileName = fileLoc; try { report = dem.ReadReport(reportFileName); } catch (Exception ex) { MessageBox.Show("Invalid Report File: \n" + ex.Message); } labelDataExportProfileName.Text = report.DataExportProfileName; labelExportCompleted.Text = (report.DataExportCompleted) ? "Yes" : "No"; labelExportStartedAt.Text = report.DataExportStartedAt; labelExportFinishedAt.Text = report.DataExportFinishedAt; labelExportedIn.Text = report.DataExportedIn; labelExportedRecords.Text = report.TotalExportedRecords.ToString(); } } } }
private void DataExportReport_Load(object sender, EventArgs e) { this.DragEnter += Form1_DragEnter; this.DragDrop += Form1_DragDrop; if (reportFileName == "") return; report = dem.ReadReport(reportFileName); labelDataExportProfileName.Text = report.DataExportProfileName; labelExportCompleted.Text = (report.DataExportCompleted) ? "Yes" : "No"; labelExportStartedAt.Text = report.DataExportStartedAt; labelExportFinishedAt.Text = report.DataExportFinishedAt; labelExportedIn.Text = report.DataExportedIn; labelExportedRecords.Text = report.TotalExportedRecords.ToString(); }
private void DataExportReport_Load(object sender, EventArgs e) { this.DragEnter += Form1_DragEnter; this.DragDrop += Form1_DragDrop; if (reportFileName == "") { return; } report = dem.ReadReport(reportFileName); labelDataExportProfileName.Text = report.DataExportProfileName; labelExportCompleted.Text = (report.DataExportCompleted) ? "Yes" : "No"; labelExportStartedAt.Text = report.DataExportStartedAt; labelExportFinishedAt.Text = report.DataExportFinishedAt; labelExportedIn.Text = report.DataExportedIn; labelExportedRecords.Text = report.TotalExportedRecords.ToString(); }
/// <summary> /// Exports the specified profile. /// </summary> /// <param name="profile">The profile.</param> /// <param name="DataExportReportFileName">Name of the data export report file.</param> private void Export(MSCRMDataExportProfile profile, string DataExportReportFileName) { try { DataExportReport report = new DataExportReport(DataExportReportFileName); //Get Data Export Report if (File.Exists(DataExportReportFileName)) { report = ReadReport(DataExportReportFileName); } //Set Data export folder string dataExportFolder = Folder + "\\" + profile.ProfileName + "\\Data"; if (!Directory.Exists(dataExportFolder)) { Directory.CreateDirectory(dataExportFolder); } MSCRMConnection connection = profile.getSourceConneciton(); _serviceProxy = cm.connect(connection); IOrganizationService service = (IOrganizationService)_serviceProxy; profile.TotalExportedRecords = 0; //Mesure export time DateTime exportStartDT = DateTime.Now; LogManager.WriteLog("Start exporting data from " + connection.ConnectionName); //Set the number of records per page to retrieve. //This value should not be bigger than 5000 as this is the limit of records provided by the CRM int fetchCount = 5000; // Initialize the file number. int fileNumber = 1; // Initialize the number of records. int recordsCount = 0; // Specify the current paging cookie. For retrieving the first page, pagingCookie should be null. string pagingCookie = null; string entityName = ""; DateTime now = DateTime.Now; string fileName = Folder + "\\" + profile.ProfileName + "\\Data\\ExportedData"; string fileExtension = profile.ExportFormat.ToLower(); if (profile.ExportFormat.ToLower() == "xml spreadsheet 2003") { fileExtension = "xml"; } fileName += now.Year + "-" + now.Month + "-" + now.Day + "-" + now.Hour + "-" + now.Minute + "-" + now.Second + "." + fileExtension; this.ExportedDataFileName = fileName; while (true) { // Build fetchXml string with the placeholders. string xml = CreateXml(profile.FetchXMLQuery, pagingCookie, fileNumber, fetchCount); StringReader stringReader = new StringReader(profile.FetchXMLQuery); XmlTextReader reader = new XmlTextReader(stringReader); // Load document XmlDocument doc = new XmlDocument(); doc.Load(reader); XmlNodeList xnl = doc.ChildNodes[0].ChildNodes[0].ChildNodes; List <string> columns = new List <string>(); List <string> DisplayedColumns = new List <string>(); foreach (XmlNode sm in xnl) { if (sm.Name == "attribute") { columns.Add(sm.Attributes[0].Value); if (profile.ExportFormat.ToLower() == "csv") { DisplayedColumns.Add(profile.DataSeparator + sm.Attributes[0].Value + profile.DataSeparator); } else if (profile.ExportFormat.ToLower() == "xml") { DisplayedColumns.Add(sm.Attributes[0].Value); } else if (profile.ExportFormat.ToLower() == "xml spreadsheet 2003") { DisplayedColumns.Add(sm.Attributes[0].Value); } } else if (sm.Name == "link-entity") { //Linked entity string linkedEntityAlias = sm.Attributes.GetNamedItem("alias").Value; string linkedAttributeyName = sm.Attributes.GetNamedItem("to").Value; XmlNodeList xnlLinkedEntity = sm.ChildNodes; foreach (XmlNode linkedAttribute in xnlLinkedEntity) { //Check if this is not a filter if (linkedAttribute.Name == "filter") { continue; } columns.Add(linkedEntityAlias + "." + linkedAttribute.Attributes[0].Value); if (profile.ExportFormat.ToLower() == "csv") { DisplayedColumns.Add(profile.DataSeparator + linkedAttributeyName + "_" + linkedAttribute.Attributes[0].Value + profile.DataSeparator); } else if (profile.ExportFormat.ToLower() == "xml") { DisplayedColumns.Add(linkedAttributeyName + "_" + linkedAttribute.Attributes[0].Value); } else if (profile.ExportFormat.ToLower() == "xml spreadsheet 2003") { DisplayedColumns.Add(linkedAttributeyName + "_" + linkedAttribute.Attributes[0].Value); } } } } // Execute the fetch query and get the xml result. RetrieveMultipleRequest fetchRequest = new RetrieveMultipleRequest { Query = new FetchExpression(xml) }; EntityCollection returnCollection = ((RetrieveMultipleResponse)_serviceProxy.Execute(fetchRequest)).EntityCollection; recordsCount += returnCollection.Entities.Count; if (recordsCount > 0) { if (profile.ExportFormat.ToLower() == "csv") { WriteCSV(returnCollection, fileName, columns, DisplayedColumns, profile); } else if (profile.ExportFormat.ToLower() == "xml") { WriteXML(returnCollection, fileName, columns, DisplayedColumns, profile); } else if (profile.ExportFormat.ToLower() == "xml spreadsheet 2003") { WriteXMLSpreadsheet2003(returnCollection, fileName, columns, DisplayedColumns, profile); } } // Check for more records, if it returns 1. if (returnCollection.MoreRecords) { // Increment the page number to retrieve the next page. fileNumber++; pagingCookie = returnCollection.PagingCookie; } else { // If no more records in the result nodes, exit the loop. break; } } Encoding encoding = GetEncoding(profile.Encoding); if (profile.ExportFormat.ToLower() == "xml") { using (var writer = new StreamWriter(fileName, true, encoding)) { writer.WriteLine("</Records>"); writer.Flush(); } } else if (profile.ExportFormat.ToLower() == "xml spreadsheet 2003") { using (var writer = new StreamWriter(fileName, true, encoding)) { writer.WriteLine("</Table></Worksheet></Workbook>\n"); writer.Flush(); } } LogManager.WriteLog("Exported " + recordsCount + " " + entityName + " records."); report.TotalExportedRecords = recordsCount; ExportedRecordsNumber = recordsCount; //Delete file if no record found if (recordsCount < 1) { File.Delete(fileName); } WriteReport(report, DataExportReportFileName); TimeSpan exportTimeSpan = DateTime.Now - exportStartDT; LogManager.WriteLog("Export finished for " + profile.ProfileName + ". Exported " + recordsCount + " records in " + exportTimeSpan.ToString().Substring(0, 10)); } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ex) { LogManager.WriteLog("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText); throw; } catch (Exception ex) { if (ex.InnerException != null) { LogManager.WriteLog("Error:" + ex.Message + "\n" + ex.InnerException.Message); } else { LogManager.WriteLog("Error:" + ex.Message); } throw; } }
private void Form1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop)); foreach (string fileLoc in filePaths) { // Code to read the contents of the text file if (File.Exists(fileLoc)) { this.reportFileName = fileLoc; try { report = dem.ReadReport(reportFileName); } catch (Exception ex) { MessageBox.Show("Invalid Report File: \n" + ex.Message); } labelDataExportProfileName.Text = report.DataExportProfileName; labelExportCompleted.Text = (report.DataExportCompleted) ? "Yes" : "No"; labelExportStartedAt.Text = report.DataExportStartedAt; labelExportFinishedAt.Text = report.DataExportFinishedAt; labelExportedIn.Text = report.DataExportedIn; labelExportedRecords.Text = report.TotalExportedRecords.ToString(); } } } }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string[] filePath = openFileDialog1.FileNames; foreach (string fileLoc in filePath) { // Code to read the contents of the text file if (File.Exists(fileLoc)) { this.reportFileName = fileLoc; try { report = dem.ReadReport(reportFileName); } catch (Exception ex) { MessageBox.Show("Invalid Report File: \n" + ex.Message); } labelDataExportProfileName.Text = report.DataExportProfileName; labelExportCompleted.Text = (report.DataExportCompleted) ? "Yes" : "No"; labelExportStartedAt.Text = report.DataExportStartedAt; labelExportFinishedAt.Text = report.DataExportFinishedAt; labelExportedIn.Text = report.DataExportedIn; labelExportedRecords.Text = report.TotalExportedRecords.ToString(); } } } }
/// <summary> /// Exports the specified profile. /// </summary> /// <param name="profile">The profile.</param> /// <param name="DataExportReportFileName">Name of the data export report file.</param> private void Export(MSCRMDataExportProfile profile, string DataExportReportFileName) { try { DataExportReport report = new DataExportReport(DataExportReportFileName); //Get Data Export Report if (File.Exists(DataExportReportFileName)) { report = ReadReport(DataExportReportFileName); } //Set Data export folder string dataExportFolder = Folder + "\\" + profile.ProfileName + "\\Data"; if (!Directory.Exists(dataExportFolder)) { Directory.CreateDirectory(dataExportFolder); } MSCRMConnection connection = profile.getSourceConneciton(); _serviceProxy = cm.connect(connection); IOrganizationService service = (IOrganizationService)_serviceProxy; profile.TotalExportedRecords = 0; //Mesure export time DateTime exportStartDT = DateTime.Now; LogManager.WriteLog("Start exporting data from " + connection.ConnectionName); //Set the number of records per page to retrieve. //This value should not be bigger than 5000 as this is the limit of records provided by the CRM int fetchCount = 5000; // Initialize the file number. int fileNumber = 1; // Initialize the number of records. int recordsCount = 0; // Specify the current paging cookie. For retrieving the first page, pagingCookie should be null. string pagingCookie = null; string entityName = ""; DateTime now = DateTime.Now; string fileName = Folder + "\\" + profile.ProfileName + "\\Data\\ExportedData"; string fileExtension = profile.ExportFormat.ToLower(); if (profile.ExportFormat.ToLower() == "xml spreadsheet 2003") fileExtension = "xml"; fileName += now.Year + "-" + now.Month + "-" + now.Day + "-" + now.Hour + "-" + now.Minute + "-" + now.Second + "." + fileExtension; this.ExportedDataFileName = fileName; while (true) { // Build fetchXml string with the placeholders. string xml = CreateXml(profile.FetchXMLQuery, pagingCookie, fileNumber, fetchCount); StringReader stringReader = new StringReader(profile.FetchXMLQuery); XmlTextReader reader = new XmlTextReader(stringReader); // Load document XmlDocument doc = new XmlDocument(); doc.Load(reader); XmlNodeList xnl = doc.ChildNodes[0].ChildNodes[0].ChildNodes; List<string> columns = new List<string>(); List<string> DisplayedColumns = new List<string>(); foreach (XmlNode sm in xnl) { if (sm.Name == "attribute") { columns.Add(sm.Attributes[0].Value); if (profile.ExportFormat.ToLower() == "csv") DisplayedColumns.Add(profile.DataSeparator + sm.Attributes[0].Value + profile.DataSeparator); else if (profile.ExportFormat.ToLower() == "xml") DisplayedColumns.Add(sm.Attributes[0].Value); else if (profile.ExportFormat.ToLower() == "xml spreadsheet 2003") DisplayedColumns.Add(sm.Attributes[0].Value); } else if (sm.Name == "link-entity") { //Linked entity string linkedEntityAlias = sm.Attributes.GetNamedItem("alias").Value; string linkedAttributeyName = sm.Attributes.GetNamedItem("to").Value; XmlNodeList xnlLinkedEntity = sm.ChildNodes; foreach (XmlNode linkedAttribute in xnlLinkedEntity) { //Check if this is not a filter if (linkedAttribute.Name == "filter") continue; columns.Add(linkedEntityAlias + "." + linkedAttribute.Attributes[0].Value); if (profile.ExportFormat.ToLower() == "csv") DisplayedColumns.Add(profile.DataSeparator + linkedAttributeyName + "_" + linkedAttribute.Attributes[0].Value + profile.DataSeparator); else if (profile.ExportFormat.ToLower() == "xml") DisplayedColumns.Add(linkedAttributeyName + "_" + linkedAttribute.Attributes[0].Value); else if (profile.ExportFormat.ToLower() == "xml spreadsheet 2003") DisplayedColumns.Add(linkedAttributeyName + "_" + linkedAttribute.Attributes[0].Value); } } } // Execute the fetch query and get the xml result. RetrieveMultipleRequest fetchRequest = new RetrieveMultipleRequest { Query = new FetchExpression(xml) }; EntityCollection returnCollection = ((RetrieveMultipleResponse)_serviceProxy.Execute(fetchRequest)).EntityCollection; recordsCount += returnCollection.Entities.Count; if (recordsCount > 0) { if (profile.ExportFormat.ToLower() == "csv") WriteCSV(returnCollection, fileName, columns, DisplayedColumns, profile); else if (profile.ExportFormat.ToLower() == "xml") WriteXML(returnCollection, fileName, columns, DisplayedColumns, profile); else if (profile.ExportFormat.ToLower() == "xml spreadsheet 2003") WriteXMLSpreadsheet2003(returnCollection, fileName, columns, DisplayedColumns, profile); } // Check for more records, if it returns 1. if (returnCollection.MoreRecords) { // Increment the page number to retrieve the next page. fileNumber++; pagingCookie = returnCollection.PagingCookie; } else { // If no more records in the result nodes, exit the loop. break; } } Encoding encoding = GetEncoding(profile.Encoding); if (profile.ExportFormat.ToLower() == "xml") { using (var writer = new StreamWriter(fileName, true, encoding)) { writer.WriteLine("</Records>"); writer.Flush(); } } else if (profile.ExportFormat.ToLower() == "xml spreadsheet 2003") { using (var writer = new StreamWriter(fileName, true, encoding)) { writer.WriteLine("</Table></Worksheet></Workbook>\n"); writer.Flush(); } } LogManager.WriteLog("Exported " + recordsCount + " " + entityName + " records."); report.TotalExportedRecords = recordsCount; ExportedRecordsNumber = recordsCount; //Delete file if no record found if (recordsCount < 1) File.Delete(fileName); WriteReport(report, DataExportReportFileName); TimeSpan exportTimeSpan = DateTime.Now - exportStartDT; LogManager.WriteLog("Export finished for " + profile.ProfileName + ". Exported " + recordsCount + " records in " + exportTimeSpan.ToString().Substring(0, 10)); } catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex) { LogManager.WriteLog("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText); throw; } catch (Exception ex) { if (ex.InnerException != null) LogManager.WriteLog("Error:" + ex.Message + "\n" + ex.InnerException.Message); else LogManager.WriteLog("Error:" + ex.Message); throw; } }
/// <summary> /// Writes the report. /// </summary> /// <param name="der">The der.</param> /// <param name="DataExportReportFileName">Name of the data export report file.</param> public void WriteReport(DataExportReport der, string DataExportReportFileName) { FileStream writer = new FileStream(DataExportReportFileName, FileMode.Create); DataContractSerializer ser = new DataContractSerializer(typeof(DataExportReport)); ser.WriteObject(writer, der); writer.Close(); }
/// <summary> /// Runs the profile. /// </summary> /// <param name="profile">The profile.</param> public void RunProfile(MSCRMDataExportProfile profile) { LogManager.WriteLog("Running Data Export Profile: " + profile.ProfileName); DateTime now = DateTime.Now; ReportFileName = Folder + "\\" + profile.ProfileName + "\\ExecutionReports\\DataExportReport" + now.Year + "-" + now.Month + "-" + now.Day + "-" + now.Hour + "-" + now.Minute + "-" + now.Second + ".xml"; //Initialize Execution Reports folder string executionReportsFolder = Folder + "\\" + profile.ProfileName + "\\ExecutionReports"; if (!Directory.Exists(executionReportsFolder)) { Directory.CreateDirectory(executionReportsFolder); } ExportedRecordsNumber = 0; //Create Data Export Report DataExportReport der = new DataExportReport(profile.ProfileName); WriteReport(der, ReportFileName); //Export data Export(profile, ReportFileName); DataExportReport report = ReadReport(ReportFileName); report.DataExportFinishedAt = DateTime.Now.ToString(); report.DataExportCompleted = true; TimeSpan ExportTimeSpan = DateTime.Now - Convert.ToDateTime(report.DataExportStartedAt); report.DataExportedIn = ExportTimeSpan.ToString().Substring(0, 10); WriteReport(report, ReportFileName); }