/// <summary> /// Imports the specified profile. /// </summary> /// <param name="profile">The profile.</param> /// <param name="transportReportFileName">Name of the transport report file.</param> private void Import(NtoNAssociationsTransportProfile profile, string transportReportFileName) { int totalTreatedRecords = 0; int totalImportFailures = 0; int totalImportSuccess = 0; int ReconnectionRetryCount = 5; try { NtoNTransportReport report = new NtoNTransportReport(transportReportFileName); //Get Transport Report if (File.Exists(transportReportFileName)) { report = ReadTransportReport(transportReportFileName); } MSCRMConnection connection = profile.getTargetConneciton(); ; _serviceProxy = cm.connect(connection); IOrganizationService service = (IOrganizationService)_serviceProxy; LogManager.WriteLog("Start importing data in " + connection.ConnectionName); //Mesure import time DateTime importStartDT = DateTime.Now; //es = ReadEnvStructure(profile.SourceConnectionName); foreach (SelectedNtoNRelationship ee in profile.SelectedNtoNRelationships) { //Check if there are any records to import if (ee.ExportedRecords == 0) { continue; } //Mesure import time DateTime entityImportStartDT = DateTime.Now; string entityFolderPath = Folder + "\\" + profile.ProfileName + "\\Data\\" + ee.RelationshipSchemaName; string[] filePaths = Directory.GetFiles(entityFolderPath, "*.xml"); LogManager.WriteLog("Importing " + ee.RelationshipSchemaName + " records."); int treatedRecordsForEntity = 0; int importedRecordsForEntity = 0; int importFailuresForEntity = 0; foreach (string filePath in filePaths) { List<Type> knownTypes = new List<Type>(); knownTypes.Add(typeof(Entity)); XmlDictionaryReaderQuotas XRQ = new XmlDictionaryReaderQuotas(); XRQ.MaxStringContentLength = int.MaxValue; using(FileStream fs = new FileStream(filePath, FileMode.Open)) using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, XRQ)) { DataContractSerializer ser = new DataContractSerializer(typeof(EntityCollection), knownTypes); EntityCollection fromDisk = (EntityCollection)ser.ReadObject(reader, true); foreach (Entity en in fromDisk.Entities) { EntityReference relatedEntity1 = new EntityReference(); EntityReference relatedEntity2 = new EntityReference(); try { Guid relatedEntity1Id = getRelatedEntityGuid(en[ee.Entity1IntersectAttribute]); Guid relatedEntity2Id = getRelatedEntityGuid(en[ee.Entity2IntersectAttribute]); relatedEntity1 = new EntityReference { LogicalName = ee.Entity1LogicalName, Id = relatedEntity1Id }; relatedEntity2 = new EntityReference { LogicalName = ee.Entity2LogicalName, Id = relatedEntity2Id }; if (!AlreadyAssociated(_serviceProxy, ee, relatedEntity1Id, relatedEntity2Id)) { if (ee.IntersectEntityName == "listmember") { Guid entity_id = Guid.Empty; Guid list_id = Guid.Empty; if (ee.Entity1LogicalName == "list") { entity_id = relatedEntity2Id; list_id = relatedEntity1Id; } else { entity_id = relatedEntity1Id; list_id = relatedEntity2Id; } AddMemberListRequest request = new AddMemberListRequest(); request.EntityId = entity_id; request.ListId = list_id; AddMemberListResponse response = (AddMemberListResponse)service.Execute(request); } else if (ee.IntersectEntityName == "campaignitem") { Guid entity_id = Guid.Empty; Guid list_id = Guid.Empty; string EntityName = ""; if (ee.Entity1LogicalName == "campaign") { entity_id = relatedEntity2Id; list_id = relatedEntity1Id; EntityName = (string)en["entitytype"]; relatedEntity2.LogicalName = EntityName; } else { entity_id = relatedEntity1Id; list_id = relatedEntity2Id; EntityName = (string)en["entitytype"]; relatedEntity1.LogicalName = EntityName; } AddItemCampaignRequest req = new AddItemCampaignRequest(); req.CampaignId = relatedEntity1Id; req.EntityName = EntityName; req.EntityId = entity_id; AddItemCampaignResponse resp = (AddItemCampaignResponse)service.Execute(req); } else if (ee.IntersectEntityName == "campaignactivityitem") { Guid entity_id = Guid.Empty; Guid list_id = Guid.Empty; string EntityName = ""; if (ee.Entity1LogicalName == "campaignactivity") { entity_id = relatedEntity2Id; list_id = relatedEntity1Id; EntityName = (string)en["itemobjecttypecode"]; relatedEntity2.LogicalName = EntityName; } else { entity_id = relatedEntity1Id; list_id = relatedEntity2Id; EntityName = (string)en["itemobjecttypecode"]; relatedEntity1.LogicalName = EntityName; } AddItemCampaignActivityRequest req = new AddItemCampaignActivityRequest(); req.CampaignActivityId = relatedEntity1Id; req.EntityName = EntityName; req.ItemId = entity_id; AddItemCampaignActivityResponse resp = (AddItemCampaignActivityResponse)service.Execute(req); } else { EntityReferenceCollection relatedEntities = new EntityReferenceCollection(); relatedEntities.Add(relatedEntity2); Relationship relationship = new Relationship(ee.RelationshipSchemaName); relationship.PrimaryEntityRole = EntityRole.Referencing; service.Associate(relatedEntity1.LogicalName, relatedEntity1.Id, relationship, relatedEntities); } } importedRecordsForEntity++; totalImportSuccess++; } catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex) { totalImportFailures++; importFailuresForEntity++; NtoNRelationshipsImportFailure failure = new NtoNRelationshipsImportFailure { CreatedOn = DateTime.Now.ToString(), NtoNRelationshipName = ee.RelationshipSchemaName, Reason = ex.Detail.Message, UrlEntity1 = profile.getSourceConneciton().ServerAddress + "main.aspx?pagetype=entityrecord&etn=" + relatedEntity1.LogicalName + "&id=" + relatedEntity1.Id.ToString(), UrlEntity2 = profile.getSourceConneciton().ServerAddress + "main.aspx?pagetype=entityrecord&etn=" + relatedEntity2.LogicalName + "&id=" + relatedEntity2.Id.ToString() }; report.TotalImportFailures += 1; //Insert the Failure line in the Failures Report WriteNewImportFailureLine(failure, importFailuresReportFileName); } catch (Exception ex) { //Check if the authentification session is expired if (ex.InnerException != null && ex.InnerException.Message.StartsWith("ID3242")) { LogManager.WriteLog("Error:The CRM authentication session expired. Reconnection attempt n° " + ReconnectionRetryCount); ReconnectionRetryCount--; //On 5 failed reconnections exit if (ReconnectionRetryCount == 0) throw; _serviceProxy = cm.connect(connection); service = (IOrganizationService)_serviceProxy; LogManager.WriteLog("Error:The CRM authentication session expired."); totalImportFailures++; importFailuresForEntity++; NtoNRelationshipsImportFailure failure = new NtoNRelationshipsImportFailure { CreatedOn = DateTime.Now.ToString(), NtoNRelationshipName = ee.RelationshipSchemaName, Reason = ex.InnerException.Message, UrlEntity1 = profile.getSourceConneciton().ServerAddress + "main.aspx?pagetype=entityrecord&etn=" + relatedEntity1.LogicalName + "&id=" + relatedEntity1.Id.ToString(), UrlEntity2 = profile.getSourceConneciton().ServerAddress + "main.aspx?pagetype=entityrecord&etn=" + relatedEntity2.LogicalName + "&id=" + relatedEntity2.Id.ToString() }; report.TotalImportFailures += 1; //Insert the Failure line in the Failures Report WriteNewImportFailureLine(failure, importFailuresReportFileName); } else { throw; } } totalTreatedRecords++; treatedRecordsForEntity++; updateTransportReport(report, ee, importedRecordsForEntity, importFailuresForEntity, entityImportStartDT); } } } LogManager.WriteLog("Treated " + treatedRecordsForEntity + " " + ee.RelationshipSchemaName + " records with " + importedRecordsForEntity + " successfully imported records and " + importFailuresForEntity + " failures."); } TimeSpan importTimeSpan = DateTime.Now - importStartDT; LogManager.WriteLog("Import finished for " + connection.ConnectionName + ". Treated " + totalTreatedRecords + " records in " + importTimeSpan.ToString().Substring(0, 10) + ". Successfuly imported " + totalImportSuccess + " records and " + totalImportFailures + " failures."); WriteTransportReport(report, transportReportFileName); } 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> /// Exports the specified profile. /// </summary> /// <param name="profile">The profile.</param> /// <param name="transportReportFileName">Name of the transport report file.</param> private void Export(NtoNAssociationsTransportProfile profile, string transportReportFileName) { NtoNTransportReport report = null; try { report = new NtoNTransportReport(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(); NtoNRelationshipsStructure es = ReadEnvStructure(profile.SourceConnectionName); _serviceProxy = cm.connect(connection); IOrganizationService service = (IOrganizationService)_serviceProxy; List<NtoNTransportReportLine> TransportReport = new List<NtoNTransportReportLine>(); profile.TotalExportedRecords = 0; //Mesure export time DateTime exportStartDT = DateTime.Now; LogManager.WriteLog("Start exporting data from " + connection.ConnectionName); int recordCount = 0; if (es != null) { foreach (SelectedNtoNRelationship ee in profile.SelectedNtoNRelationships) { LogManager.WriteLog("Exporting data for relationship " + ee.RelationshipSchemaName); DateTime entityExportStartDT = DateTime.Now; string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"; fetchXml += "<entity name='" + ee.IntersectEntityName + "'>"; //Get Entity structure NtoNRelationship strE = new NtoNRelationship(); foreach (NtoNRelationship envE in es.NtoNRelationships) { if (envE.IntersectEntityName == ee.IntersectEntityName) { strE = envE; fetchXml += "<attribute name='" + strE.Entity1IntersectAttribute + "' />"; fetchXml += "<attribute name='" + strE.Entity2IntersectAttribute + "' />"; if (ee.IntersectEntityName == "listmember" || ee.IntersectEntityName == "campaignitem") fetchXml += "<attribute name='entitytype' />"; else if (ee.IntersectEntityName == "campaignactivityitem") fetchXml += "<attribute name='itemobjecttypecode' />"; break; } } //Add Query filter int objectTypeCode = 0; if (ee.Entity2LogicalName == "account") objectTypeCode = 1; else if (ee.Entity2LogicalName == "campaign") objectTypeCode = 4400; else if (ee.Entity2LogicalName == "contact") objectTypeCode = 2; else if (ee.Entity2LogicalName == "lead") objectTypeCode = 4; else if (ee.Entity2LogicalName == "list") objectTypeCode = 4300; else if (ee.Entity2LogicalName == "product") objectTypeCode = 1024; else if (ee.Entity2LogicalName == "salesliterature") objectTypeCode = 1038; if (ee.IntersectEntityName == "campaignitem") fetchXml += "<filter type='and'><condition attribute='entitytype' operator='eq' value='" + objectTypeCode + "' /></filter>"; else if (ee.IntersectEntityName == "campaignactivityitem") fetchXml += "<filter type='and'><condition attribute='itemobjecttypecode' operator='eq' value='" + objectTypeCode + "' /></filter>"; fetchXml += "</entity></fetch>"; int recordCountPerEntity = ExportEntity(profile, fetchXml, ee.RelationshipSchemaName); recordCount += recordCountPerEntity; DateTime entityExportEndDT = DateTime.Now; TimeSpan ts = entityExportEndDT - entityExportStartDT; NtoNTransportReportLine transportReportLine = new NtoNTransportReportLine(); transportReportLine.RelationshipSchemaName = ee.RelationshipSchemaName; 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); } } }