/// <summary> /// Deletes the profile. /// </summary> /// <param name="profile">The profile.</param> /// <exception cref="System.Exception">Data Export Profile deletion failed. The Data Export Profile + profile.ProfileName + was not found in the configuration file.</exception> public void DeleteProfile(MSCRMDataExportProfile profile) { int index = Profiles.FindIndex(d => d.ProfileName == profile.ProfileName); if (index > -1) { Profiles.RemoveAt(index); } else { LogManager.WriteLog("Data Export Profile deletion failed. The Data Export Profile " + profile.ProfileName + " was not found in the configuration file."); throw new Exception("Data Export Profile deletion failed. The Data Export Profile " + profile.ProfileName + " was not found in the configuration file."); } //Delete Profile folder try { if (Directory.Exists(Folder + "\\" + profile.ProfileName)) { Directory.Delete(Folder + "\\" + profile.ProfileName, true); } } catch (Exception) { throw; } //Save Profiles WriteProfiles(); }
/// <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); }
/// <summary> /// Creates the profile. /// </summary> /// <param name="profile">The profile.</param> public void CreateProfile(MSCRMDataExportProfile profile) { if (!Directory.Exists(Folder + "\\" + profile.ProfileName)) { Directory.CreateDirectory(Folder + "\\" + profile.ProfileName); } //Creating new Profile Profiles.Add(profile); WriteProfiles(); }
/// <summary> /// Updates the profile. /// </summary> /// <param name="profile">The profile.</param> /// <exception cref="System.Exception">Data Export Profile Update failed. The Data Export Profile + profile.ProfileName + was not found in the configuration file.</exception> public void UpdateProfile(MSCRMDataExportProfile profile) { if (!Directory.Exists(Folder + "\\" + profile.ProfileName)) { Directory.CreateDirectory(Folder + "\\" + profile.ProfileName); } int index = Profiles.FindIndex(d => d.ProfileName == profile.ProfileName); if (index > -1) { Profiles[index] = profile; } else { LogManager.WriteLog("Data Export Profile Update failed. The Data Export Profile " + profile.ProfileName + " was not found in the configuration file."); throw new Exception("Data Export Profile Update failed. The Data Export Profile " + profile.ProfileName + " was not found in the configuration file."); } WriteProfiles(); }
private void deleteProfileToolStripMenuItem_Click(object sender, EventArgs e) { string currentProfileName = currentProfile.ProfileName; DialogResult dResTest; dResTest = MessageBox.Show("Are you sure you want to delete this Profile ?", "Confirm Profile Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dResTest == DialogResult.No) { return; } else { comboBoxProfiles.Items.Remove(currentProfile.ProfileName); comboBoxProfiles.SelectedItem = null; man.DeleteProfile(currentProfile); currentProfile = null; textBoxProfileName.Text = ""; xmlEditor1.Text = ""; textBoxProfileName.Enabled = true; comboBoxConnectionSource.SelectedItem = null; toolStripStatusLabel1.Text = "Profile " + currentProfileName + " deleted"; } }
/// <summary> /// Writes the XML spreadsheet 2003. /// </summary> /// <param name="items">The items.</param> /// <param name="path">The path.</param> /// <param name="columns">The columns.</param> /// <param name="DisplayedColumns">The displayed columns.</param> /// <param name="profile">The profile.</param> public void WriteXMLSpreadsheet2003(EntityCollection items, string path, List<string> columns, List<string> DisplayedColumns, MSCRMDataExportProfile profile) { bool writeColumns = false; if (!File.Exists(path)) writeColumns = true; Encoding encoding = GetEncoding(profile.Encoding); using (var writer = new StreamWriter(path, true, encoding)) { if (writeColumns) { writer.WriteLine("<?xml version=\"1.0\"?>\n"); writer.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>\n"); writer.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" "); writer.WriteLine("xmlns:o=\"urn:schemas-microsoft-com:office:office\" "); writer.WriteLine("xmlns:x=\"urn:schemas-microsoft-com:office:excel\" "); writer.WriteLine("xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" "); writer.WriteLine("xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n"); writer.WriteLine("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">"); writer.WriteLine("<Author>MSCRMToolkit</Author>"); writer.WriteLine("</DocumentProperties>"); writer.WriteLine("<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n"); writer.WriteLine("<ProtectStructure>False</ProtectStructure>\n"); writer.WriteLine("<ProtectWindows>False</ProtectWindows>\n"); writer.WriteLine("</ExcelWorkbook>\n"); writer.WriteLine("<Worksheet ss:Name=\"WorkSheet1\">"); writer.WriteLine("<Table>"); string header = "<Row>"; foreach (string displayedColumn in DisplayedColumns) { header += "<Cell><Data ss:Type=\"String\">" + SecurityElement.Escape(displayedColumn) + "</Data></Cell>"; } header += "</Row>"; writer.WriteLine(header); } foreach (Entity e in items.Entities) { string row = "<Row>"; int cCpt = 0; List<string> values = new List<string>(); foreach (string column in columns) { string value = getFormattedValue(e, column); row += "<Cell><Data ss:Type=\"String\">" + SecurityElement.Escape(value) + "</Data></Cell>"; cCpt++; } row += "</Row>"; writer.WriteLine(row); } writer.Flush(); } }
private bool SaveProfile() { bool result = true; //Check that all fields are provided if (string.IsNullOrEmpty(textBoxProfileName.Text)) { MessageBox.Show("Profile Name is mandatory!"); return false; } //Check that the name of the connection is valid if (textBoxProfileName.Text.Contains(" ") || textBoxProfileName.Text.Contains("\\") || textBoxProfileName.Text.Contains("/") || textBoxProfileName.Text.Contains(">") || textBoxProfileName.Text.Contains("<") || textBoxProfileName.Text.Contains("?") || textBoxProfileName.Text.Contains("*") || textBoxProfileName.Text.Contains(":") || textBoxProfileName.Text.Contains("|") || textBoxProfileName.Text.Contains("\"") || textBoxProfileName.Text.Contains("'") ) { MessageBox.Show("You shouldn't use spaces nor the following characters (\\/<>?*:|\"') in the Profile Name as it will be used to create folders and files."); return false; } if (comboBoxConnectionSource.SelectedItem == null) { MessageBox.Show("You must select a Source for the Profile"); return false; } if (comboBoxFormat.SelectedItem == null) { MessageBox.Show("You must select an Export Format for the Profile"); return false; } //Check if this is a creation if (currentProfile == null) { //Check if a Data Export Profile having the same name exist already MSCRMDataExportProfile existingProfile = man.Profiles.Find(d => d.ProfileName.ToLower() == textBoxProfileName.Text.ToLower()); if (existingProfile != null) { MessageBox.Show("Profile with the name " + textBoxProfileName.Text + " exist already. Please select another name"); return false; } MSCRMDataExportProfile newProfile = new MSCRMDataExportProfile(); newProfile.ProfileName = textBoxProfileName.Text; newProfile.SourceConnectionName = comboBoxConnectionSource.SelectedItem.ToString(); newProfile.setSourceConneciton(); newProfile.ExportFormat = comboBoxFormat.SelectedItem.ToString(); if (comboBoxFieldSeparator.SelectedIndex == 0) newProfile.FieldSeparator = ","; else if (comboBoxFieldSeparator.SelectedIndex == 1) newProfile.FieldSeparator = ":"; else if (comboBoxFieldSeparator.SelectedIndex == 2) newProfile.FieldSeparator = ";"; else if (comboBoxFieldSeparator.SelectedIndex == 3) newProfile.FieldSeparator = " "; if (comboBoxDataSeparator.SelectedIndex == 0) newProfile.DataSeparator = "'"; else if (comboBoxDataSeparator.SelectedIndex == 1) newProfile.DataSeparator = "\""; else if (comboBoxDataSeparator.SelectedIndex == 2) newProfile.DataSeparator = ""; newProfile.Encoding = "Default"; if (comboBoxEncoding.SelectedIndex == 1) newProfile.Encoding = "UTF8"; else if (comboBoxEncoding.SelectedIndex == 1) newProfile.Encoding = "Unicode"; else if (comboBoxEncoding.SelectedIndex == 2) newProfile.Encoding = "ASCII"; else if (comboBoxEncoding.SelectedIndex == 3) newProfile.Encoding = "BigEndianUnicode"; newProfile.FetchXMLQuery = xmlEditor1.Text; man.CreateProfile(newProfile); comboBoxProfiles.Items.AddRange(new object[] { newProfile.ProfileName }); comboBoxProfiles.SelectedItem = newProfile.ProfileName; currentProfile = newProfile; } else { currentProfile.ProfileName = textBoxProfileName.Text; currentProfile.SourceConnectionName = comboBoxConnectionSource.SelectedItem.ToString(); currentProfile.ExportFormat = comboBoxFormat.SelectedItem.ToString(); if (comboBoxFieldSeparator.SelectedIndex == 0) currentProfile.FieldSeparator = ","; else if (comboBoxFieldSeparator.SelectedIndex == 1) currentProfile.FieldSeparator = ":"; else if (comboBoxFieldSeparator.SelectedIndex == 2) currentProfile.FieldSeparator = ";"; else if (comboBoxFieldSeparator.SelectedIndex == 3) currentProfile.FieldSeparator = " "; if (comboBoxDataSeparator.SelectedIndex == 0) currentProfile.DataSeparator = "'"; else if (comboBoxDataSeparator.SelectedIndex == 1) currentProfile.DataSeparator = "\""; else if (comboBoxDataSeparator.SelectedIndex == 2) currentProfile.DataSeparator = ""; currentProfile.Encoding = "Default"; if (comboBoxEncoding.SelectedIndex == 1) currentProfile.Encoding = "UTF8"; else if (comboBoxEncoding.SelectedIndex == 1) currentProfile.Encoding = "Unicode"; else if (comboBoxEncoding.SelectedIndex == 2) currentProfile.Encoding = "ASCII"; else if (comboBoxEncoding.SelectedIndex == 3) currentProfile.Encoding = "BigEndianUnicode"; currentProfile.FetchXMLQuery = xmlEditor1.Text; currentProfile.setSourceConneciton(); MSCRMDataExportProfile oldDEP = man.GetProfile(currentProfile.ProfileName); man.UpdateProfile(currentProfile); } runProfileToolStripMenuItem.Enabled = true; toolStripStatusLabel1.Text = "Profile " + currentProfile.ProfileName + " saved."; LogManager.WriteLog("Profile " + currentProfile.ProfileName + " saved."); return result; }
//private static MSCRMDataExportManager man; private static void Main(string[] args) { //Set the application directory as the current directory string appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); appPath = appPath.Replace("file:\\", ""); Directory.SetCurrentDirectory(appPath); MSCRMDataExportManager man = new MSCRMDataExportManager(); string selectedProfileName = ""; if (args.Length == 0) { if (man.Profiles.Count == 0) { Console.WriteLine("\nNo profiles found."); return; } //Display all profiles for selection Console.WriteLine("\nSpecify the Profile to run (1-{0}) [1] : ", man.Profiles.Count); int tpCpt = 1; foreach (MSCRMDataExportProfile profile in man.Profiles) { Console.WriteLine(tpCpt + ". " + profile.ProfileName); tpCpt++; } String input = Console.ReadLine(); if (input == String.Empty) { input = "1"; } int depNumber; Int32.TryParse(input, out depNumber); if (depNumber > 0 && depNumber <= man.Profiles.Count) { selectedProfileName = man.Profiles[depNumber - 1].ProfileName; } else { Console.WriteLine("The specified Profile does not exist."); return; } } else { //Check that the Profile name is provided if (string.IsNullOrEmpty(args[0])) { return; } selectedProfileName = args[0]; } MSCRMDataExportProfile p = man.GetProfile(selectedProfileName); if (p == null) { Console.WriteLine("The specified Profile does not exist."); return; } man.RunProfile(p); }
private void comboBoxDataExportProfiles_SelectedIndexChanged(object sender, EventArgs e) { comboBoxConnectionSource.SelectedItem = null; if (comboBoxProfiles.SelectedItem != null) { currentProfile = man.Profiles[comboBoxProfiles.SelectedIndex]; textBoxProfileName.Text = currentProfile.ProfileName; comboBoxConnectionSource.SelectedItem = currentProfile.SourceConnectionName; comboBoxFormat.SelectedItem = currentProfile.ExportFormat; xmlEditor1.Text = currentProfile.FetchXMLQuery; if (currentProfile.FieldSeparator == ",") { comboBoxFieldSeparator.SelectedItem = "Comma (,)"; } else if (currentProfile.FieldSeparator == ";") { comboBoxFieldSeparator.SelectedItem = "Semi-colon (;)"; } else if (currentProfile.FieldSeparator == @"\t") { comboBoxFieldSeparator.SelectedItem = "Tab (\t)"; } if (currentProfile.DataSeparator == "'") { comboBoxDataSeparator.SelectedItem = "Signle Quote (')"; } else if (currentProfile.DataSeparator == "\"") { comboBoxDataSeparator.SelectedItem = "Double Quote (\")"; } else if (currentProfile.DataSeparator == "") { comboBoxDataSeparator.SelectedItem = "None"; } comboBoxEncoding.SelectedItem = "Default"; if (currentProfile.Encoding == "UTF8") { comboBoxEncoding.SelectedItem = "UTF8"; } else if (currentProfile.Encoding == "Unicode") { comboBoxEncoding.SelectedItem = "Unicode"; } else if (currentProfile.Encoding == "ASCII") { comboBoxEncoding.SelectedItem = "ASCII"; } else if (currentProfile.Encoding == "BigEndianUnicode") { comboBoxEncoding.SelectedItem = "BigEndianUnicode"; } deleteProfileToolStripMenuItem.Enabled = true; newToolStripMenuItem.Enabled = true; saveToolStripMenuItem.Enabled = true; textBoxProfileName.Enabled = false; runProfileToolStripMenuItem.Enabled = true; } else { currentProfile = null; textBoxProfileName.Text = ""; comboBoxFormat.SelectedItem = null; deleteProfileToolStripMenuItem.Enabled = false; newToolStripMenuItem.Enabled = false; saveToolStripMenuItem.Enabled = false; textBoxProfileName.Enabled = true; runProfileToolStripMenuItem.Enabled = false; } buttonOpenInExcel.Visible = false; dataExportReportToolStripMenuItem.Visible = false; }
private void deleteProfileToolStripMenuItem_Click(object sender, EventArgs e) { string currentProfileName = currentProfile.ProfileName; DialogResult dResTest; dResTest = MessageBox.Show("Are you sure you want to delete this Profile ?", "Confirm Profile Deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dResTest == DialogResult.No) { return; } else { comboBoxProfiles.Items.Remove(currentProfile.ProfileName); comboBoxProfiles.SelectedItem = null; man.DeleteProfile(currentProfile); currentProfile = null; textBoxProfileName.Text = ""; xmlEditor1.Text = ""; textBoxProfileName.Enabled = true; comboBoxConnectionSource.SelectedItem = null; toolStripStatusLabel1.Text = "Profile " + currentProfileName + " deleted"; } }
/// <summary> /// Writes the XML. /// </summary> /// <param name="items">The items.</param> /// <param name="path">The path.</param> /// <param name="columns">The columns.</param> /// <param name="DisplayedColumns">The displayed columns.</param> /// <param name="profile">The profile.</param> public void WriteXML(EntityCollection items, string path, List <string> columns, List <string> DisplayedColumns, MSCRMDataExportProfile profile) { bool writeColumns = false; if (!File.Exists(path)) { writeColumns = true; } Encoding encoding = GetEncoding(profile.Encoding); using (var writer = new StreamWriter(path, true, encoding)) { if (writeColumns) { writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); writer.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>"); writer.WriteLine("<Records>"); } foreach (Entity e in items.Entities) { string row = "<row>"; int cCpt = 0; List <string> values = new List <string>(); foreach (string column in columns) { string value = getFormattedValue(e, column); row += "<" + DisplayedColumns[cCpt] + ">" + SecurityElement.Escape(value) + "</" + DisplayedColumns[cCpt] + ">"; cCpt++; } row += "</row>"; writer.WriteLine(row); } writer.Flush(); } }
/// <summary> /// Deletes the profile. /// </summary> /// <param name="profile">The profile.</param> /// <exception cref="System.Exception">Data Export Profile deletion failed. The Data Export Profile + profile.ProfileName + was not found in the configuration file.</exception> public void DeleteProfile(MSCRMDataExportProfile profile) { int index = Profiles.FindIndex(d => d.ProfileName == profile.ProfileName); if (index > -1) { Profiles.RemoveAt(index); } else { LogManager.WriteLog("Data Export Profile deletion failed. The Data Export Profile " + profile.ProfileName + " was not found in the configuration file."); throw new Exception("Data Export Profile deletion failed. The Data Export Profile " + profile.ProfileName + " was not found in the configuration file."); } //Delete Profile folder try { if (Directory.Exists(Folder + "\\" + profile.ProfileName)) Directory.Delete(Folder + "\\" + profile.ProfileName, true); } catch (Exception) { throw; } //Save Profiles WriteProfiles(); }
/// <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 XML. /// </summary> /// <param name="items">The items.</param> /// <param name="path">The path.</param> /// <param name="columns">The columns.</param> /// <param name="DisplayedColumns">The displayed columns.</param> /// <param name="profile">The profile.</param> public void WriteXML(EntityCollection items, string path, List<string> columns, List<string> DisplayedColumns, MSCRMDataExportProfile profile) { bool writeColumns = false; if (!File.Exists(path)) writeColumns = true; Encoding encoding = GetEncoding(profile.Encoding); using (var writer = new StreamWriter(path, true, encoding)) { if (writeColumns) { writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); writer.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>"); writer.WriteLine("<Records>"); } foreach (Entity e in items.Entities) { string row = "<row>"; int cCpt = 0; List<string> values = new List<string>(); foreach (string column in columns) { string value = getFormattedValue(e, column); row += "<" + DisplayedColumns[cCpt] + ">" + SecurityElement.Escape(value) + "</" + DisplayedColumns[cCpt] + ">"; cCpt++; } row += "</row>"; writer.WriteLine(row); } writer.Flush(); } }
/// <summary> /// Writes the CSV. /// </summary> /// <param name="items">The items.</param> /// <param name="path">The path.</param> /// <param name="columns">The columns.</param> /// <param name="DisplayedColumns">The displayed columns.</param> /// <param name="profile">The profile.</param> public void WriteCSV(EntityCollection items, string path, List<string> columns, List<string> DisplayedColumns, MSCRMDataExportProfile profile) { bool writeColumns = false; if (!File.Exists(path)) writeColumns = true; Encoding encoding = System.Text.Encoding.Default; if (profile.Encoding == "UTF8") encoding = System.Text.Encoding.UTF8; else if (profile.Encoding == "Unicode") encoding = System.Text.Encoding.Unicode; else if (profile.Encoding == "ASCII") encoding = System.Text.Encoding.ASCII; else if (profile.Encoding == "BigEndianUnicode") encoding = System.Text.Encoding.BigEndianUnicode; using (var writer = new StreamWriter(path, true, encoding)) { if (writeColumns) writer.WriteLine(string.Join(profile.FieldSeparator, DisplayedColumns)); foreach (Entity e in items.Entities) { List<string> values = new List<string>(); foreach (string column in columns) { string value = getFormattedValue(e, column); if (value != "") { //Escape Data if (profile.DataSeparator != "") value = value.Replace(profile.DataSeparator, profile.DataSeparator + profile.DataSeparator); else if (value.Contains(profile.FieldSeparator)) value = "\"" + value.Replace("\"", "\"\"") + "\""; values.Add(profile.DataSeparator + value + profile.DataSeparator); } else { values.Add(""); } } writer.WriteLine(string.Join(profile.FieldSeparator, values)); } writer.Flush(); } }
/// <summary> /// Updates the profile. /// </summary> /// <param name="profile">The profile.</param> /// <exception cref="System.Exception">Data Export Profile Update failed. The Data Export Profile + profile.ProfileName + was not found in the configuration file.</exception> public void UpdateProfile(MSCRMDataExportProfile profile) { if (!Directory.Exists(Folder + "\\" + profile.ProfileName)) Directory.CreateDirectory(Folder + "\\" + profile.ProfileName); int index = Profiles.FindIndex(d => d.ProfileName == profile.ProfileName); if (index > -1) { Profiles[index] = profile; } else { LogManager.WriteLog("Data Export Profile Update failed. The Data Export Profile " + profile.ProfileName + " was not found in the configuration file."); throw new Exception("Data Export Profile Update failed. The Data Export Profile " + profile.ProfileName + " was not found in the configuration file."); } WriteProfiles(); }
/// <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); }
/// <summary> /// Writes the CSV. /// </summary> /// <param name="items">The items.</param> /// <param name="path">The path.</param> /// <param name="columns">The columns.</param> /// <param name="DisplayedColumns">The displayed columns.</param> /// <param name="profile">The profile.</param> public void WriteCSV(EntityCollection items, string path, List <string> columns, List <string> DisplayedColumns, MSCRMDataExportProfile profile) { bool writeColumns = false; if (!File.Exists(path)) { writeColumns = true; } Encoding encoding = System.Text.Encoding.Default; if (profile.Encoding == "UTF8") { encoding = System.Text.Encoding.UTF8; } else if (profile.Encoding == "Unicode") { encoding = System.Text.Encoding.Unicode; } else if (profile.Encoding == "ASCII") { encoding = System.Text.Encoding.ASCII; } else if (profile.Encoding == "BigEndianUnicode") { encoding = System.Text.Encoding.BigEndianUnicode; } using (var writer = new StreamWriter(path, true, encoding)) { if (writeColumns) { writer.WriteLine(string.Join(profile.FieldSeparator, DisplayedColumns)); } foreach (Entity e in items.Entities) { List <string> values = new List <string>(); foreach (string column in columns) { string value = getFormattedValue(e, column); if (value != null && value != "") { //Escape Data if (profile.DataSeparator != "") { value = value.Replace(profile.DataSeparator, profile.DataSeparator + profile.DataSeparator); } else if (value.Contains(profile.FieldSeparator)) { value = "\"" + value.Replace("\"", "\"\"") + "\""; } values.Add(profile.DataSeparator + value + profile.DataSeparator); } else { values.Add(""); } } writer.WriteLine(string.Join(profile.FieldSeparator, values)); } writer.Flush(); } }
/// <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 XML spreadsheet 2003. /// </summary> /// <param name="items">The items.</param> /// <param name="path">The path.</param> /// <param name="columns">The columns.</param> /// <param name="DisplayedColumns">The displayed columns.</param> /// <param name="profile">The profile.</param> public void WriteXMLSpreadsheet2003(EntityCollection items, string path, List <string> columns, List <string> DisplayedColumns, MSCRMDataExportProfile profile) { bool writeColumns = false; if (!File.Exists(path)) { writeColumns = true; } Encoding encoding = GetEncoding(profile.Encoding); using (var writer = new StreamWriter(path, true, encoding)) { if (writeColumns) { writer.WriteLine("<?xml version=\"1.0\"?>\n"); writer.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>\n"); writer.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" "); writer.WriteLine("xmlns:o=\"urn:schemas-microsoft-com:office:office\" "); writer.WriteLine("xmlns:x=\"urn:schemas-microsoft-com:office:excel\" "); writer.WriteLine("xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" "); writer.WriteLine("xmlns:html=\"http://www.w3.org/TR/REC-html40\">\n"); writer.WriteLine("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">"); writer.WriteLine("<Author>MSCRMToolkit</Author>"); writer.WriteLine("</DocumentProperties>"); writer.WriteLine("<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">\n"); writer.WriteLine("<ProtectStructure>False</ProtectStructure>\n"); writer.WriteLine("<ProtectWindows>False</ProtectWindows>\n"); writer.WriteLine("</ExcelWorkbook>\n"); writer.WriteLine("<Worksheet ss:Name=\"WorkSheet1\">"); writer.WriteLine("<Table>"); string header = "<Row>"; foreach (string displayedColumn in DisplayedColumns) { header += "<Cell><Data ss:Type=\"String\">" + SecurityElement.Escape(displayedColumn) + "</Data></Cell>"; } header += "</Row>"; writer.WriteLine(header); } foreach (Entity e in items.Entities) { string row = "<Row>"; int cCpt = 0; List <string> values = new List <string>(); foreach (string column in columns) { string value = getFormattedValue(e, column); row += "<Cell><Data ss:Type=\"String\">" + SecurityElement.Escape(value) + "</Data></Cell>"; cCpt++; } row += "</Row>"; writer.WriteLine(row); } writer.Flush(); } }
/// <summary> /// Creates the profile. /// </summary> /// <param name="profile">The profile.</param> public void CreateProfile(MSCRMDataExportProfile profile) { if (!Directory.Exists(Folder + "\\" + profile.ProfileName)) Directory.CreateDirectory(Folder + "\\" + profile.ProfileName); //Creating new Profile Profiles.Add(profile); WriteProfiles(); }
private bool SaveProfile() { bool result = true; //Check that all fields are provided if (string.IsNullOrEmpty(textBoxProfileName.Text)) { MessageBox.Show("Profile Name is mandatory!"); return(false); } //Check that the name of the connection is valid if (textBoxProfileName.Text.Contains(" ") || textBoxProfileName.Text.Contains("\\") || textBoxProfileName.Text.Contains("/") || textBoxProfileName.Text.Contains(">") || textBoxProfileName.Text.Contains("<") || textBoxProfileName.Text.Contains("?") || textBoxProfileName.Text.Contains("*") || textBoxProfileName.Text.Contains(":") || textBoxProfileName.Text.Contains("|") || textBoxProfileName.Text.Contains("\"") || textBoxProfileName.Text.Contains("'") ) { MessageBox.Show("You shouldn't use spaces nor the following characters (\\/<>?*:|\"') in the Profile Name as it will be used to create folders and files."); return(false); } if (comboBoxConnectionSource.SelectedItem == null) { MessageBox.Show("You must select a Source for the Profile"); return(false); } if (comboBoxFormat.SelectedItem == null) { MessageBox.Show("You must select an Export Format for the Profile"); return(false); } //Check if this is a creation if (currentProfile == null) { //Check if a Data Export Profile having the same name exist already MSCRMDataExportProfile existingProfile = man.Profiles.Find(d => d.ProfileName.ToLower() == textBoxProfileName.Text.ToLower()); if (existingProfile != null) { MessageBox.Show("Profile with the name " + textBoxProfileName.Text + " exist already. Please select another name"); return(false); } MSCRMDataExportProfile newProfile = new MSCRMDataExportProfile(); newProfile.ProfileName = textBoxProfileName.Text; newProfile.SourceConnectionName = comboBoxConnectionSource.SelectedItem.ToString(); newProfile.setSourceConneciton(); newProfile.ExportFormat = comboBoxFormat.SelectedItem.ToString(); if (comboBoxFieldSeparator.SelectedIndex == 0) { newProfile.FieldSeparator = ","; } else if (comboBoxFieldSeparator.SelectedIndex == 1) { newProfile.FieldSeparator = ":"; } else if (comboBoxFieldSeparator.SelectedIndex == 2) { newProfile.FieldSeparator = ";"; } else if (comboBoxFieldSeparator.SelectedIndex == 3) { newProfile.FieldSeparator = " "; } if (comboBoxDataSeparator.SelectedIndex == 0) { newProfile.DataSeparator = "'"; } else if (comboBoxDataSeparator.SelectedIndex == 1) { newProfile.DataSeparator = "\""; } else if (comboBoxDataSeparator.SelectedIndex == 2) { newProfile.DataSeparator = ""; } newProfile.Encoding = "Default"; if (comboBoxEncoding.SelectedIndex == 1) { newProfile.Encoding = "UTF8"; } else if (comboBoxEncoding.SelectedIndex == 1) { newProfile.Encoding = "Unicode"; } else if (comboBoxEncoding.SelectedIndex == 2) { newProfile.Encoding = "ASCII"; } else if (comboBoxEncoding.SelectedIndex == 3) { newProfile.Encoding = "BigEndianUnicode"; } newProfile.FetchXMLQuery = xmlEditor1.Text; man.CreateProfile(newProfile); comboBoxProfiles.Items.AddRange(new object[] { newProfile.ProfileName }); comboBoxProfiles.SelectedItem = newProfile.ProfileName; currentProfile = newProfile; } else { currentProfile.ProfileName = textBoxProfileName.Text; currentProfile.SourceConnectionName = comboBoxConnectionSource.SelectedItem.ToString(); currentProfile.ExportFormat = comboBoxFormat.SelectedItem.ToString(); if (comboBoxFieldSeparator.SelectedIndex == 0) { currentProfile.FieldSeparator = ","; } else if (comboBoxFieldSeparator.SelectedIndex == 1) { currentProfile.FieldSeparator = ":"; } else if (comboBoxFieldSeparator.SelectedIndex == 2) { currentProfile.FieldSeparator = ";"; } else if (comboBoxFieldSeparator.SelectedIndex == 3) { currentProfile.FieldSeparator = " "; } if (comboBoxDataSeparator.SelectedIndex == 0) { currentProfile.DataSeparator = "'"; } else if (comboBoxDataSeparator.SelectedIndex == 1) { currentProfile.DataSeparator = "\""; } else if (comboBoxDataSeparator.SelectedIndex == 2) { currentProfile.DataSeparator = ""; } currentProfile.Encoding = "Default"; if (comboBoxEncoding.SelectedIndex == 1) { currentProfile.Encoding = "UTF8"; } else if (comboBoxEncoding.SelectedIndex == 1) { currentProfile.Encoding = "Unicode"; } else if (comboBoxEncoding.SelectedIndex == 2) { currentProfile.Encoding = "ASCII"; } else if (comboBoxEncoding.SelectedIndex == 3) { currentProfile.Encoding = "BigEndianUnicode"; } currentProfile.FetchXMLQuery = xmlEditor1.Text; currentProfile.setSourceConneciton(); MSCRMDataExportProfile oldDEP = man.GetProfile(currentProfile.ProfileName); man.UpdateProfile(currentProfile); } runProfileToolStripMenuItem.Enabled = true; toolStripStatusLabel1.Text = "Profile " + currentProfile.ProfileName + " saved."; LogManager.WriteLog("Profile " + currentProfile.ProfileName + " saved."); return(result); }
private void comboBoxDataExportProfiles_SelectedIndexChanged(object sender, EventArgs e) { comboBoxConnectionSource.SelectedItem = null; if (comboBoxProfiles.SelectedItem != null) { currentProfile = man.Profiles[comboBoxProfiles.SelectedIndex]; textBoxProfileName.Text = currentProfile.ProfileName; comboBoxConnectionSource.SelectedItem = currentProfile.SourceConnectionName; comboBoxFormat.SelectedItem = currentProfile.ExportFormat; xmlEditor1.Text = currentProfile.FetchXMLQuery; if (currentProfile.FieldSeparator == ",") comboBoxFieldSeparator.SelectedItem = "Comma (,)"; else if (currentProfile.FieldSeparator == ";") comboBoxFieldSeparator.SelectedItem = "Semi-colon (;)"; else if (currentProfile.FieldSeparator == @"\t") comboBoxFieldSeparator.SelectedItem = "Tab (\t)"; if (currentProfile.DataSeparator == "'") comboBoxDataSeparator.SelectedItem = "Signle Quote (')"; else if (currentProfile.DataSeparator == "\"") comboBoxDataSeparator.SelectedItem = "Double Quote (\")"; else if (currentProfile.DataSeparator == "") comboBoxDataSeparator.SelectedItem = "None"; comboBoxEncoding.SelectedItem = "Default"; if (currentProfile.Encoding == "UTF8") comboBoxEncoding.SelectedItem = "UTF8"; else if (currentProfile.Encoding == "Unicode") comboBoxEncoding.SelectedItem = "Unicode"; else if (currentProfile.Encoding == "ASCII") comboBoxEncoding.SelectedItem = "ASCII"; else if (currentProfile.Encoding == "BigEndianUnicode") comboBoxEncoding.SelectedItem = "BigEndianUnicode"; deleteProfileToolStripMenuItem.Enabled = true; newToolStripMenuItem.Enabled = true; saveToolStripMenuItem.Enabled = true; textBoxProfileName.Enabled = false; runProfileToolStripMenuItem.Enabled = true; } else { currentProfile = null; textBoxProfileName.Text = ""; comboBoxFormat.SelectedItem = null; deleteProfileToolStripMenuItem.Enabled = false; newToolStripMenuItem.Enabled = false; saveToolStripMenuItem.Enabled = false; textBoxProfileName.Enabled = true; runProfileToolStripMenuItem.Enabled = false; } buttonOpenInExcel.Visible = false; dataExportReportToolStripMenuItem.Visible = false; }