/// <summary>
        /// Exports the specified profile.
        /// </summary>
        /// <param name="profile">The profile.</param>
        private void Export(MSCRMAuditExportProfile profile)
        {
            MSCRMConnection connection = profile.getSourceConneciton();
            _serviceProxy = cm.connect(connection);

            IOrganizationService service = (IOrganizationService)_serviceProxy;
            es = ReadEnvStructure(profile.SourceConnectionName);

            List<string> columns = new List<string> { "transactionid", "createdon", "userid", "objecttypecode", "objectid", "action", "operation" };
            List<string> DisplayedColumns = new List<string> { "TransactionId", "Date", "User", "Entity", "Record", "Action", "Operation" };

            if (!(profile.AuditType == "User Acces Audit") && !(profile.AuditType == "Audit Summary View"))
            {
                columns.Add("key");
                columns.Add("oldValue");
                columns.Add("newValue");
                DisplayedColumns.Add("Attribute");
                DisplayedColumns.Add("Old_Value");
                DisplayedColumns.Add("New_Value");
            }

            if (profile.AuditType == "User Acces Audit" || profile.AuditType == "Audit Summary View")
            {
                LogManager.WriteLog("Exporting " + profile.AuditType);
                string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
                fetchXml += "<entity name='audit'>";
                fetchXml += "<attribute name='transactionid' />";
                fetchXml += "<attribute name='attributemask' />";
                fetchXml += "<attribute name='action' />";
                fetchXml += "<attribute name='createdon' />";
                fetchXml += "<attribute name='auditid' />";
                fetchXml += "<attribute name='userid' />";
                fetchXml += "<attribute name='operation' />";
                fetchXml += "<attribute name='objectid' />";
                fetchXml += "<order attribute='createdon' descending='true' />";
                fetchXml += "<filter type='and'>";
                if (!profile.AllUsersSelected && profile.SelectedUsers.Count > 0)
                {
                    //When a User Access Audit type is selected the User is in the Object
                    if (profile.AuditType == "User Acces Audit")
                        fetchXml += "<condition attribute='objectid' operator='in'>";
                    else
                        fetchXml += "<condition attribute='userid' operator='in'>";

                    foreach (AuditUser u in profile.SelectedUsers)
                    {
                        fetchXml += "<value uitype='systemuser'>" + u.Id.ToString() + "</value>";
                    }
                    fetchXml += "</condition>";
                }
                if (!profile.AllActionsSelected && profile.SelectedActions.Count > 0)
                {
                    fetchXml += "<condition attribute='action' operator='in'>";
                    foreach (int i in profile.SelectedActions)
                    {
                        fetchXml += "<value>" + i + "</value>";
                    }
                    fetchXml += "</condition>";
                }
                if (!profile.AllOperationsSelected && profile.SelectedOperations.Count > 0)
                {
                    fetchXml += "<condition attribute='operation' operator='in'>";
                    foreach (int i in profile.SelectedOperations)
                    {
                        fetchXml += "<value>" + i + "</value>";
                    }
                    fetchXml += "</condition>";
                }
                if (profile.AuditType != "User Acces Audit" && !profile.AllEntitiesSelected && profile.SelectedEntities.Count > 0)
                {
                    fetchXml += "<condition attribute='objecttypecode' operator='in'>";
                    foreach (SelectedAuditEntity i in profile.SelectedEntities)
                    {
                        fetchXml += "<value>" + i.ObjectTypeCode + "</value>";
                    }
                    fetchXml += "</condition>";
                }

                if (profile.AuditRecordCreatedOnFilter == "Last X Days")
                    fetchXml += "<condition attribute='createdon' operator='last-x-days' value='" + profile.AuditRecordCreatedOnFilterLastX + "' />";
                else if (profile.AuditRecordCreatedOnFilter == "Last X Months")
                    fetchXml += "<condition attribute='createdon' operator='last-x-months' value='" + profile.AuditRecordCreatedOnFilterLastX + "' />";
                else if (profile.AuditRecordCreatedOnFilter == "Last X Years")
                    fetchXml += "<condition attribute='createdon' operator='last-x-years' value='" + profile.AuditRecordCreatedOnFilterLastX + "' />";
                else if (profile.AuditRecordCreatedOnFilter == "Between Dates")
                {
                    fetchXml += "<condition attribute='createdon' operator='on-or-after' value='" + String.Format("{0:yyyy-MM-dd}", profile.AuditRecordCreatedOnFilterFrom) + "' />";
                    fetchXml += "<condition attribute='createdon' operator='on-or-before' value='" + String.Format("{0:yyyy-MM-dd}", profile.AuditRecordCreatedOnFilterTo) + "' />";
                }

                fetchXml += "</filter>";
                fetchXml += "</entity></fetch>";
                int recordCountPerEntity = ExportEntity(profile, fetchXml, "", columns, DisplayedColumns, null);
            }
            else
            {
                foreach (SelectedAuditEntity sae in profile.SelectedEntities)
                {
                    //Get entity PrimaryNameAttribute
                    String PrimaryNameAttribute = es.Entities.Find(e => e.LogicalName == sae.LogicalName).PrimaryNameAttribute;
                    LogManager.WriteLog("Exporting audit data for entity " + sae.LogicalName);
                    string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
                    fetchXml += "<entity name='" + sae.LogicalName + "'>";
                    fetchXml += "<attribute name='" + PrimaryNameAttribute + "' />";
                    fetchXml += sae.Filter;
                    fetchXml += "</entity></fetch>";
                    int recordCountPerEntity = ExportEntity(profile, fetchXml, PrimaryNameAttribute, columns, DisplayedColumns, sae);
                }
            }
        }