/// <summary>
        /// Reads the env structure.
        /// </summary>
        /// <param name="connectionName">Name of the connection.</param>
        /// <returns>The Environment Structure</returns>
        public EnvStructure ReadEnvStructure(string connectionName)
        {
            string filename = Folder + "\\" + connectionName + ".xml";

            if (File.Exists(filename))
            {
                try
                {
                    XmlDictionaryReaderQuotas XRQ = new XmlDictionaryReaderQuotas();
                    XRQ.MaxStringContentLength = int.MaxValue;

                    using (FileStream fs = new FileStream(filename, FileMode.Open))
                        using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, XRQ))
                        {
                            DataContractSerializer ser = new DataContractSerializer(typeof(EnvStructure));
                            EnvStructure           es  = (EnvStructure)ser.ReadObject(reader, true);
                            return(es);
                        }
                }
                catch (Exception)
                {
                    LogManager.WriteLog("Error while reading the struction of connection" + connectionName + ". The structure file may be corrupted.");
                    throw;
                }
            }
            return(null);
        }
        /// <summary>
        /// Writes the environment structure.
        /// </summary>
        /// <param name="str">The Environment Structure.</param>
        private void WriteEnvStructure(EnvStructure str)
        {
            string                 filename = Folder + "\\" + str.connectionName + ".xml";
            FileStream             writer   = new FileStream(filename, FileMode.Create);
            DataContractSerializer ser      = new DataContractSerializer(typeof(EnvStructure));

            ser.WriteObject(writer, str);
            writer.Close();
        }
        /// <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 void populateCheckedEntities()
        {
            checkedListBoxEntities.Items.Clear();

            //If Structure already downloaded display entities list
            if (comboBoxConnectionSource.SelectedItem == null)
                return;

            try
            {
                es = man.ReadEnvStructure(comboBoxConnectionSource.SelectedItem.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error while reading the struction of connection " + comboBoxConnectionSource.SelectedItem.ToString() + ". The structure file may be corrupted.\r\n\r\n" + ex.Message);
                return;
            }
            labelStructureLastLoadedDate.Text = "never";
            if (es != null)
            {
                string structureFileName = man.Folder + "\\" + comboBoxConnectionSource.SelectedItem + ".xml";
                DateTime structureRefreshedOn = File.GetLastWriteTime(structureFileName);
                labelStructureLastLoadedDate.Text = structureRefreshedOn.ToString();
                foreach (EnvEntity ee in es.Entities)
                {
                    checkedListBoxEntities.Items.AddRange(new object[] { ee.EntityName });
                    if (currentProfile == null)
                        continue;
                    if (currentProfile.SelectedEntities != null)
                    {
                        foreach (SelectedEntity ee1 in currentProfile.SelectedEntities)
                        {
                            if (ee.EntityName == ee1.EntityName)
                            {
                                checkedListBoxEntities.SetItemChecked(checkedListBoxEntities.Items.Count - 1, true);
                            }
                        }
                    }
                }
            }
        }
        private void buttonLoadEntities_Click(object sender, EventArgs e)
        {
            DialogResult dResTest = MessageBox.Show("Loading the structure may take some time. The application will become unresponsive during this time.\n\nAre you sure you want to load the structure from the Source?", "Confirm Structure Load", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dResTest == DialogResult.No)
            {
                return;
            }

            toolStripStatusLabel.Text = "Loading structure. Please wait...";
            Application.DoEvents();

            try
            {
                es = man.downloadEnvStructure(comboBoxConnectionSource.SelectedItem.ToString());
            }
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
            {
                MessageBox.Show("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    MessageBox.Show("Error:" + ex.Message + "\n" + ex.InnerException.Message);
                else
                {
                    MessageBox.Show("Error:" + ex.Message);
                }
            }

            if (es == null)
                return;

            string structureFileName = man.Folder + "\\" + comboBoxConnectionSource.SelectedItem.ToString() + ".xml";
            DateTime structureRefreshedOn = File.GetLastWriteTime(structureFileName);
            labelStructureLastLoadedDate.Text = structureRefreshedOn.ToString();
            //Display entities list
            checkedListBoxEntities.Items.Clear();
            foreach (EnvEntity ee in es.Entities)
            {
                checkedListBoxEntities.Items.AddRange(new object[] { ee.EntityName });
            }

            toolStripStatusLabel.Text = "Structure successfully loaded";
        }
 /// <summary>
 /// Writes the environment structure.
 /// </summary>
 /// <param name="str">The Environment Structure.</param>
 private void WriteEnvStructure(EnvStructure str)
 {
     string filename = Folder + "\\" + str.connectionName + ".xml";
     FileStream writer = new FileStream(filename, FileMode.Create);
     DataContractSerializer ser = new DataContractSerializer(typeof(EnvStructure));
     ser.WriteObject(writer, str);
     writer.Close();
 }
        /// <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;
            }
        }