/// <summary> /// Handles the Load event of the SetupIgnoredAttributes control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void SetupIgnoredAttributes_Load(object sender, EventArgs e) { Control[] ComboBoxes = rdt.Controls.Find("comboBoxConnectionSource", true); if (ComboBoxes.Length != 1) { return; } ComboBox sourceCB = (ComboBox)ComboBoxes[0]; EnvStructure es = rdt.man.ReadEnvStructure(sourceCB.SelectedItem.ToString()); List <EnvEntity> eeList = es.Entities; EnvEntity ee = eeList.Find(eP => eP.EntityName == this.entity); labelEntityName.Text = "Entity: " + ee.EntityName; SelectedEntity se = null; if (rdt.currentProfile != null) { se = rdt.currentProfile.SelectedEntities.Find(eP => eP.EntityName == this.entity); } foreach (string Attribute in ee.Attributes) { checkedListBoxAttributes.Items.AddRange(new object[] { Attribute }); checkedListBoxAttributes.SetItemChecked(checkedListBoxAttributes.Items.Count - 1, true); if (se == null) { continue; } if (se.IgnoredAttributes == null) { continue; } string ignoredAttribute = se.IgnoredAttributes.Find(a => a == Attribute); if (ignoredAttribute == null) { checkedListBoxAttributes.SetItemChecked(checkedListBoxAttributes.Items.Count - 1, true); } else { checkedListBoxAttributes.SetItemChecked(checkedListBoxAttributes.Items.Count - 1, false); } } checkedListBoxAttributes.Items.AddRange(new object[] { "ownerid" }); checkedListBoxAttributes.SetItemChecked(checkedListBoxAttributes.Items.Count - 1, true); //Display Export Filter if (se != null && se.Filter != null && se.Filter != "") { xmlEditor1.Text = se.Filter; } }
/// <summary> /// Exports the specified profile. /// </summary> /// <param name="profile">The profile.</param> /// <param name="transportReportFileName">Name of the transport report file.</param> private void Export(TransportationProfile profile, string transportReportFileName) { try { TransportReport report = new TransportReport(transportReportFileName); //Get Transport Report if (File.Exists(transportReportFileName)) { report = ReadTransportReport(transportReportFileName); } //Clean Data folder string dataExportFolder = Folder + "\\" + profile.ProfileName + "\\Data"; if (Directory.Exists(dataExportFolder)) { Directory.Delete(dataExportFolder, true); } Directory.CreateDirectory(dataExportFolder); MSCRMConnection connection = profile.getSourceConneciton(); EnvStructure es = ReadEnvStructure(profile.SourceConnectionName); _serviceProxy = cm.connect(connection); IOrganizationService service = (IOrganizationService)_serviceProxy; List <TransportReportLine> TransportReport = new List <TransportReportLine>(); profile.TotalExportedRecords = 0; //Mesure export time DateTime exportStartDT = DateTime.Now; LogManager.WriteLog("Start exporting data from " + connection.ConnectionName); int recordCount = 0; if (es != null) { //Order export according to profile's transport order IOrderedEnumerable <SelectedEntity> orderedSelectedEntities = profile.SelectedEntities.OrderBy(se => se.TransportOrder); foreach (SelectedEntity ee in orderedSelectedEntities) { LogManager.WriteLog("Exporting data for entity " + ee.EntityName); DateTime entityExportStartDT = DateTime.Now; string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"; fetchXml += "<entity name='" + ee.EntityName + "'>"; //Get Entity structure EnvEntity strE = new EnvEntity(); foreach (EnvEntity envE in es.Entities) { if (envE.EntityName == ee.EntityName) { strE = envE; break; } } //Create fetchXML Query foreach (string ea in strE.Attributes) { if (ee.IgnoredAttributes == null) { fetchXml += "<attribute name='" + ea + "' />"; } else if (!ee.IgnoredAttributes.Contains(ea)) { fetchXml += "<attribute name='" + ea + "' />"; } } //Add Query filter fetchXml += ee.Filter; fetchXml += "</entity></fetch>"; int recordCountPerEntity = ExportEntity(profile, fetchXml); recordCount += recordCountPerEntity; DateTime entityExportEndDT = DateTime.Now; TimeSpan ts = entityExportEndDT - entityExportStartDT; TransportReportLine transportReportLine = new TransportReportLine(); transportReportLine.Entity = ee.EntityName; transportReportLine.ExportedRecords = recordCountPerEntity; report.TotalExportedRecords += recordCountPerEntity; transportReportLine.ExportedIn = ts.ToString().Substring(0, 10); transportReportLine.ExportStartedAt = entityExportStartDT.ToString(); transportReportLine.ExportFinishedAt = entityExportEndDT.ToString(); report.ReportLines.Add(transportReportLine); WriteTransportReport(report, transportReportFileName); } } TimeSpan exportTimeSpan = DateTime.Now - exportStartDT; LogManager.WriteLog("Export finished for " + profile.SourceConnectionName + ". Exported " + recordCount + " records in " + exportTimeSpan.ToString().Substring(0, 10)); } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ex) { LogManager.WriteLog("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText); } catch (Exception ex) { if (ex.InnerException != null) { LogManager.WriteLog("Error:" + ex.Message + "\n" + ex.InnerException.Message); } else { LogManager.WriteLog("Error:" + ex.Message); } } }
/// <summary> /// Downloads the env structure. /// </summary> /// <param name="connectionName">Name of the connection.</param> /// <returns>The Environment Structure</returns> public EnvStructure downloadEnvStructure(string connectionName) { try { MSCRMConnection connection = cm.getConnection(connectionName); _serviceProxy = cm.connect(connection); IOrganizationService service = (IOrganizationService)_serviceProxy; EnvStructure es = new EnvStructure(); es.Entities = new List <EnvEntity>(); RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest() { EntityFilters = EntityFilters.Attributes, RetrieveAsIfPublished = true }; // Retrieve the MetaData. RetrieveAllEntitiesResponse AllEntitiesResponse = (RetrieveAllEntitiesResponse)_serviceProxy.Execute(request); IOrderedEnumerable <EntityMetadata> EMD = AllEntitiesResponse.EntityMetadata.OrderBy(ee => ee.LogicalName); List <string> AdditionalEntities = new List <string> { "duplicaterule", "duplicaterulecondition", "incidentresolution", "kbarticlecomment", "opportunityclose", "orderclose", "postcomment", "postlike", "quoteclose", "subject", "uom" }; List <string> IgnoredEntities = new List <string> { "activitypointer", "asyncoperation", "fieldsecurityprofile", "importjob", "pluginassembly", "plugintype", "processsession", "recurringappointmentmaster", "sdkmessage", "sdkmessagefilter", "sdkmessageprocessingstep", "sdkmessageprocessingstepimage", "sdkmessageprocessingstepsecureconfig", "workflow", "channelaccessprofile" }; List <string> IgnoredAttributes = new List <string> { "importsequencenumber", "statuscode", "timezoneruleversionnumber", "utcconversiontimezonecode", "overriddencreatedon", "ownerid", "haveprivilegeschanged" }; foreach (EntityMetadata currentEntity in EMD) { if (currentEntity.LogicalName == "externalpartyitem") { AdditionalEntities.Add(currentEntity.LogicalName); } if (IgnoredEntities.IndexOf(currentEntity.LogicalName) < 0 && currentEntity.IsIntersect.Value == false && (currentEntity.IsValidForAdvancedFind.Value || AdditionalEntities.IndexOf(currentEntity.LogicalName) >= 0) ) { EnvEntity ee = new EnvEntity(); ee.EntityName = currentEntity.LogicalName; ee.Attributes = new List <string>(); IOrderedEnumerable <AttributeMetadata> AMD = currentEntity.Attributes.OrderBy(a => a.LogicalName); foreach (AttributeMetadata currentAttribute in AMD) { // Only write out main attributes enabled for reading and creation. if ((currentAttribute.AttributeOf == null) && IgnoredAttributes.IndexOf(currentAttribute.LogicalName) < 0 && currentAttribute.IsValidForRead != null && currentAttribute.IsValidForRead.Value && currentAttribute.IsValidForCreate != null && currentAttribute.IsValidForCreate.Value) { ee.Attributes.Add(currentAttribute.LogicalName); } } //Dont export entitites for which only the ID is retrieved if (ee.Attributes.Count > 1) { es.Entities.Add(ee); } } } es.connectionName = connectionName; WriteEnvStructure(es); return(es); } catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { throw; } catch (Exception) { throw; } }
private int ExportGUI(string transportReportFileName, BackgroundWorker worker, DoWorkEventArgs e) { TransportReport report = null; try { report = new TransportReport(man.ReportFileName); //Get Transport Report if (File.Exists(man.ReportFileName)) { report = man.ReadTransportReport(man.ReportFileName); } //Clean Data folder string dataExportFolder = man.Folder + "\\" + currentProfile.ProfileName + "\\Data"; if (Directory.Exists(dataExportFolder)) { Directory.Delete(dataExportFolder, true); } Directory.CreateDirectory(dataExportFolder); MSCRMConnection connection = currentProfile.getSourceConneciton(); EnvStructure es = man.ReadEnvStructure(currentProfile.SourceConnectionName); man._serviceProxy = cm.connect(connection); IOrganizationService service = (IOrganizationService)_serviceProxy; List<TransportReportLine> TransportReport = new List<TransportReportLine>(); currentProfile.TotalExportedRecords = 0; //Mesure export time DateTime exportStartDT = DateTime.Now; LogManager.WriteLog("Start exporting data from " + connection.ConnectionName); int recordCount = 0; if (es != null) { int treatedEntities = 1; //Order export according to profile's transport order IOrderedEnumerable<SelectedEntity> orderedSelectedEntities = currentProfile.SelectedEntities.OrderBy(se => se.TransportOrder); foreach (SelectedEntity ee in orderedSelectedEntities) { if (worker.CancellationPending) { e.Cancel = true; return 0; } int percentage = 0; if (currentProfile.SelectedEntities.Count != 0) percentage = (int)(100 * treatedEntities / currentProfile.SelectedEntities.Count); worker.ReportProgress(percentage); treatedEntities++; currentlyTransportedEntity = ee.EntityName; LogManager.WriteLog("Exporting data for entity " + ee.EntityName); DateTime entityExportStartDT = DateTime.Now; string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"; fetchXml += "<entity name='" + ee.EntityName + "'>"; //Get Entity structure EnvEntity strE = new EnvEntity(); foreach (EnvEntity envE in es.Entities) { if (envE.EntityName == ee.EntityName) { strE = envE; break; } } //Create fetchXML Query foreach (string ea in strE.Attributes) { if (ee.IgnoredAttributes == null) { fetchXml += "<attribute name='" + ea + "' />"; } else if (!ee.IgnoredAttributes.Contains(ea)) { fetchXml += "<attribute name='" + ea + "' />"; } } //Add Query filter fetchXml += ee.Filter; fetchXml += "</entity></fetch>"; int recordCountPerEntity = man.ExportEntity(currentProfile, fetchXml); recordCount += recordCountPerEntity; DateTime entityExportEndDT = DateTime.Now; TimeSpan ts = entityExportEndDT - entityExportStartDT; TransportReportLine transportReportLine = new TransportReportLine(); transportReportLine.Entity = ee.EntityName; transportReportLine.ExportedRecords = recordCountPerEntity; report.TotalExportedRecords += recordCountPerEntity; transportReportLine.ExportedIn = ts.ToString().Substring(0, 10); transportReportLine.ExportStartedAt = entityExportStartDT.ToString(); transportReportLine.ExportFinishedAt = entityExportEndDT.ToString(); report.ReportLines.Add(transportReportLine); man.WriteTransportReport(report, transportReportFileName); } } TimeSpan exportTimeSpan = DateTime.Now - exportStartDT; LogManager.WriteLog("Export finished for " + currentProfile.SourceConnectionName + ". Exported " + recordCount + " records in " + exportTimeSpan.ToString().Substring(0, 10)); return recordCount; } catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex) { //Stop Transport threads if running transportStopped = true; e.Cancel = true; MessageBox.Show("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText); LogManager.WriteLog("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText); } catch (Exception ex) { //Stop Transport threads if running transportStopped = true; e.Cancel = true; if (ex.InnerException != null) { MessageBox.Show("Error:" + ex.Message + "\n" + ex.InnerException.Message); LogManager.WriteLog("Error:" + ex.Message + "\n" + ex.InnerException.Message); } else { MessageBox.Show("Error:" + ex.Message); LogManager.WriteLog("Error:" + ex.Message); } } return 0; }
/// <summary> /// Exports the specified profile. /// </summary> /// <param name="profile">The profile.</param> /// <param name="transportReportFileName">Name of the transport report file.</param> private void Export(TransportationProfile profile, string transportReportFileName) { try { TransportReport report = new TransportReport(transportReportFileName); //Get Transport Report if (File.Exists(transportReportFileName)) { report = ReadTransportReport(transportReportFileName); } //Clean Data folder string dataExportFolder = Folder + "\\" + profile.ProfileName + "\\Data"; if (Directory.Exists(dataExportFolder)) { Directory.Delete(dataExportFolder, true); } Directory.CreateDirectory(dataExportFolder); MSCRMConnection connection = profile.getSourceConneciton(); EnvStructure es = ReadEnvStructure(profile.SourceConnectionName); _serviceProxy = cm.connect(connection); IOrganizationService service = (IOrganizationService)_serviceProxy; List<TransportReportLine> TransportReport = new List<TransportReportLine>(); profile.TotalExportedRecords = 0; //Mesure export time DateTime exportStartDT = DateTime.Now; LogManager.WriteLog("Start exporting data from " + connection.ConnectionName); int recordCount = 0; if (es != null) { //Order export according to profile's transport order IOrderedEnumerable<SelectedEntity> orderedSelectedEntities = profile.SelectedEntities.OrderBy(se => se.TransportOrder); foreach (SelectedEntity ee in orderedSelectedEntities) { LogManager.WriteLog("Exporting data for entity " + ee.EntityName); DateTime entityExportStartDT = DateTime.Now; string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"; fetchXml += "<entity name='" + ee.EntityName + "'>"; //Get Entity structure EnvEntity strE = new EnvEntity(); foreach (EnvEntity envE in es.Entities) { if (envE.EntityName == ee.EntityName) { strE = envE; break; } } //Create fetchXML Query foreach (string ea in strE.Attributes) { if (ee.IgnoredAttributes == null) { fetchXml += "<attribute name='" + ea + "' />"; } else if (!ee.IgnoredAttributes.Contains(ea)) { fetchXml += "<attribute name='" + ea + "' />"; } } //Add Query filter fetchXml += ee.Filter; fetchXml += "</entity></fetch>"; int recordCountPerEntity = ExportEntity(profile, fetchXml); recordCount += recordCountPerEntity; DateTime entityExportEndDT = DateTime.Now; TimeSpan ts = entityExportEndDT - entityExportStartDT; TransportReportLine transportReportLine = new TransportReportLine(); transportReportLine.Entity = ee.EntityName; transportReportLine.ExportedRecords = recordCountPerEntity; report.TotalExportedRecords += recordCountPerEntity; transportReportLine.ExportedIn = ts.ToString().Substring(0, 10); transportReportLine.ExportStartedAt = entityExportStartDT.ToString(); transportReportLine.ExportFinishedAt = entityExportEndDT.ToString(); report.ReportLines.Add(transportReportLine); WriteTransportReport(report, transportReportFileName); } } TimeSpan exportTimeSpan = DateTime.Now - exportStartDT; LogManager.WriteLog("Export finished for " + profile.SourceConnectionName + ". Exported " + recordCount + " records in " + exportTimeSpan.ToString().Substring(0, 10)); } catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex) { LogManager.WriteLog("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText); } catch (Exception ex) { if (ex.InnerException != null) { LogManager.WriteLog("Error:" + ex.Message + "\n" + ex.InnerException.Message); } else { LogManager.WriteLog("Error:" + ex.Message); } } }
/// <summary> /// Downloads the env structure. /// </summary> /// <param name="connectionName">Name of the connection.</param> /// <returns>The Environment Structure</returns> public EnvStructure downloadEnvStructure(string connectionName) { try { MSCRMConnection connection = cm.getConnection(connectionName); _serviceProxy = cm.connect(connection); IOrganizationService service = (IOrganizationService)_serviceProxy; EnvStructure es = new EnvStructure(); es.Entities = new List<EnvEntity>(); RetrieveAllEntitiesRequest request = new RetrieveAllEntitiesRequest() { EntityFilters = EntityFilters.Attributes, RetrieveAsIfPublished = true }; // Retrieve the MetaData. RetrieveAllEntitiesResponse AllEntitiesResponse = (RetrieveAllEntitiesResponse)_serviceProxy.Execute(request); IOrderedEnumerable<EntityMetadata> EMD = AllEntitiesResponse.EntityMetadata.OrderBy(ee => ee.LogicalName); List<string> AdditionalEntities = new List<string> {"duplicaterule", "duplicaterulecondition", "incidentresolution", "kbarticlecomment", "opportunityclose", "orderclose", "postcomment", "postlike", "quoteclose", "subject", "uom"}; List<string> IgnoredEntities = new List<string> { "activitypointer", "asyncoperation", "fieldsecurityprofile", "importjob", "pluginassembly", "plugintype", "processsession", "recurringappointmentmaster", "sdkmessage", "sdkmessagefilter", "sdkmessageprocessingstep", "sdkmessageprocessingstepimage", "sdkmessageprocessingstepsecureconfig", "workflow" }; List<string> IgnoredAttributes = new List<string> { "importsequencenumber", //"statuscode", "timezoneruleversionnumber", "utcconversiontimezonecode", //"overriddencreatedon"//, //"ownerid" }; foreach (EntityMetadata currentEntity in EMD) { if (currentEntity.IsIntersect.Value == false && IgnoredEntities.IndexOf(currentEntity.LogicalName) < 0 && (currentEntity.IsValidForAdvancedFind.Value || AdditionalEntities.IndexOf(currentEntity.LogicalName) >= 0) ) { EnvEntity ee = new EnvEntity(); ee.EntityName = currentEntity.LogicalName; ee.Attributes = new List<string>(); IOrderedEnumerable<AttributeMetadata> AMD = currentEntity.Attributes.OrderBy(a => a.LogicalName); foreach (AttributeMetadata currentAttribute in AMD) { // Only write out main attributes enabled for reading and creation, or the statecode if ((((currentAttribute.AttributeOf == null) && currentAttribute.IsValidForRead.Value && currentAttribute.IsValidForCreate.Value && IgnoredAttributes.IndexOf(currentAttribute.LogicalName) < 0) || currentAttribute.LogicalName.Equals("statecode"))) { ee.Attributes.Add(currentAttribute.LogicalName); } } //ee.Attributes.Add("statecode"); //Dont export entitites for which only the ID is retrieved if (ee.Attributes.Count > 1) es.Entities.Add(ee); } } es.connectionName = connectionName; WriteEnvStructure(es); return es; } catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>) { throw; } catch (Exception) { throw; } }