private void AuditExportDetails_Load(object sender, EventArgs e)
        {
            Control[] ComboBoxes = au.Controls.Find("comboBoxConnectionSource", true);
            if (ComboBoxes.Length != 1)
            {
                return;
            }
            ComboBox              sourceCB = (ComboBox)ComboBoxes[0];
            EnvAuditStructure     es       = au.man.ReadEnvStructure(sourceCB.SelectedItem.ToString());
            List <EnvAuditEntity> eeList   = es.Entities;
            EnvAuditEntity        ee       = eeList.Find(eP => eP.LogicalName == this.entity);
            SelectedAuditEntity   se       = null;

            if (au.currentProfile != null)
            {
                se = au.currentProfile.SelectedEntities.Find(eP => eP.LogicalName == this.entity);
            }

            foreach (string Attribute in ee.Attributes)
            {
                checkedListBoxAttributes.Items.AddRange(new object[] { Attribute });
                checkedListBoxAttributes.SetItemChecked(checkedListBoxAttributes.Items.Count - 1, au.AuditType != "Attribute Change History");
                //Enable all Attributes selection for Audit Summary Export type
                //if (au.AuditType != "Attribute Change History")
                //    continue;
                if (se == null)
                {
                    continue;
                }
                if (se.SelectedAttributes == null)
                {
                    continue;
                }
                string selectedAttribute = se.SelectedAttributes.Find(a => a == Attribute);
                if (selectedAttribute == null)
                {
                    checkedListBoxAttributes.SetItemChecked(checkedListBoxAttributes.Items.Count - 1, false);
                }
                else
                {
                    checkedListBoxAttributes.SetItemChecked(checkedListBoxAttributes.Items.Count - 1, true);
                }
            }

            //Disable Attributes selection for Audit Summary Export type
            if (au.AuditType != "Attribute Change History")
            {
                checkedListBoxAttributes.Enabled = false;
            }

            //Display Export Filter
            if (se != null && se.Filter != null && se.Filter != "")
            {
                xmlEditor1.Text = se.Filter;
            }
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            List <string> SelectedAttributes = new List <string>();

            if (au.AuditType == "Attribute Change History")
            {
                if (checkedListBoxAttributes.CheckedItems.Count != 1)
                {
                    MessageBox.Show("You must select 1 Attribute for this Entity!");
                    return;
                }

                for (int j = 0; j < checkedListBoxAttributes.CheckedItems.Count; j++)
                {
                    SelectedAttributes.Add(checkedListBoxAttributes.CheckedItems[j].ToString());
                }
            }

            if (SelectedAttributes.Count < 1)
            {
                SelectedAttributes = null;
            }

            SelectedAuditEntity se = new SelectedAuditEntity
            {
                LogicalName        = this.entity,
                SelectedAttributes = SelectedAttributes,
                Filter             = xmlEditor1.Text
            };

            int index = au.SelectedEntityList.FindIndex(match => match.LogicalName == this.entity);

            if (index > -1)
            {
                au.SelectedEntityList[index] = se;
            }
            else
            {
                au.SelectedEntityList.Add(se);
            }

            this.Dispose();
        }
Example #3
0
        /// <summary>
        /// Saves the profile.
        /// </summary>
        /// <returns>True or False if the profile was succesfuly saved.</returns>
        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 (comboBoxAuditTypeExport.SelectedItem == null)
            {
                MessageBox.Show("You must select an Audit Type to Export for the Profile");
                return false;
            }

            if (comboBoxFormat.SelectedItem == null)
            {
                MessageBox.Show("You must select an Export Format for the Profile");
                return false;
            }

            if (checkedListBoxActions.CheckedItems.Count == 0)
            {
                MessageBox.Show("You must select a least 1 Action");
                return false;
            }
            if (checkedListBoxOperations.CheckedItems.Count == 0)
            {
                MessageBox.Show("You must select a least 1 Operation");
                return false;
            }
            if (checkedListBoxUsers.CheckedItems.Count == 0)
            {
                MessageBox.Show("You must select a least 1 User");
                return false;
            }
            if (comboBoxAuditTypeExport.SelectedItem.ToString() != "User Acces Audit" && checkedListBoxEntities.CheckedItems.Count == 0)
            {
                MessageBox.Show("You must select a least 1 Entity");
                return false;
            }
            if (comboBoxAuditTypeExport.SelectedItem.ToString() == "Attribute Change History")
            {
                List<SelectedAuditEntity> SAEList = SelectedEntityList.Where(x => (x.SelectedAttributes == null || x.SelectedAttributes.Count == 0)).ToList<SelectedAuditEntity>();

                if (SAEList.Count > 0)
                {
                    MessageBox.Show("You must select 1 Attribute for each selected Entity!");
                    return false;
                }
            }

            //Check if this is a creation
            if (currentProfile == null)
            {
                //Check if a Data Export Profile having the same name exist already
                MSCRMAuditExportProfile 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;
                }

                MSCRMAuditExportProfile newProfile = new MSCRMAuditExportProfile();
                newProfile.ProfileName = textBoxProfileName.Text;
                newProfile.SourceConnectionName = comboBoxConnectionSource.SelectedItem.ToString();
                newProfile.setSourceConneciton();
                newProfile.ExportFormat = comboBoxFormat.SelectedItem.ToString();
                newProfile.AuditType = comboBoxAuditTypeExport.SelectedItem.ToString();

                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.SelectedActions = new List<int>();
                if (checkedListBoxActions.CheckedItems.Count == checkedListBoxActions.Items.Count)
                {
                    newProfile.AllActionsSelected = true;
                }
                else
                {
                    newProfile.AllActionsSelected = false;
                    foreach (ActionListBoxItem checkedAction in checkedListBoxActions.CheckedItems)
                    {
                        newProfile.SelectedActions.Add(checkedAction.Key);
                    }
                }

                newProfile.SelectedOperations = new List<int>();
                if (checkedListBoxOperations.CheckedItems.Count == checkedListBoxOperations.Items.Count)
                {
                    newProfile.AllOperationsSelected = true;
                }
                else
                {
                    newProfile.AllOperationsSelected = false;
                    for (int i = 0; i < checkedListBoxOperations.Items.Count; i++)
                    {
                        if (checkedListBoxOperations.CheckedItems.IndexOf(checkedListBoxOperations.Items[i]) > -1)
                        {
                            KeyValuePair<int, string> u = new KeyValuePair<int, string>(i + 1, checkedListBoxOperations.Items[i].ToString());
                            newProfile.SelectedOperations.Add(u.Key);
                        }
                    }
                }

                newProfile.SelectedUsers = new List<AuditUser>();
                if (checkedListBoxUsers.CheckedItems.Count == checkedListBoxUsers.Items.Count)
                {
                    newProfile.AllUsersSelected = true;
                }
                else
                {
                    newProfile.AllUsersSelected = false;
                    foreach (CheckListBoxItem checkedUser in checkedListBoxUsers.CheckedItems)
                    {
                        AuditUser u = new AuditUser { Id = checkedUser.Id, FullName = checkedUser.Text };
                        newProfile.SelectedUsers.Add(u);
                    }
                }

                newProfile.SelectedEntities = new List<SelectedAuditEntity>();
                if (checkedListBoxEntities.CheckedItems.Count == checkedListBoxEntities.Items.Count)
                    newProfile.AllEntitiesSelected = true;
                else
                    newProfile.AllEntitiesSelected = false;

                foreach (EntityListBoxItem checkedEntity in checkedListBoxEntities.CheckedItems)
                {
                    SelectedAuditEntity ee = new SelectedAuditEntity();
                    ee.LogicalName = checkedEntity.Value;
                    ee.ObjectTypeCode = checkedEntity.ObjectTypeCode;

                    SelectedAuditEntity seForIgnoredAttributes = SelectedEntityList.Find(match => match.LogicalName == checkedEntity.Value);
                    if (seForIgnoredAttributes != null)
                    {
                        ee.SelectedAttributes = seForIgnoredAttributes.SelectedAttributes;
                        ee.Filter = seForIgnoredAttributes.Filter;
                    }

                    newProfile.SelectedEntities.Add(ee);
                }

                if (comboBoxAuditRecordCreatedOnFilter.SelectedItem != null)
                    newProfile.AuditRecordCreatedOnFilter = comboBoxAuditRecordCreatedOnFilter.SelectedItem.ToString();
                newProfile.AuditRecordCreatedOnFilterLastX = numericUpDownLastX.Value;
                newProfile.AuditRecordCreatedOnFilterFrom = dateTimePickerAuditCreatedFrom.Value;
                newProfile.AuditRecordCreatedOnFilterTo = dateTimePickerAuditCreatedTo.Value;

                man.CreateProfile(newProfile);
                currentProfile = newProfile;
                comboBoxProfiles.Items.AddRange(new object[] { newProfile.ProfileName });
                comboBoxProfiles.SelectedItem = newProfile.ProfileName;
            }
            else
            {
                currentProfile.ProfileName = textBoxProfileName.Text;
                currentProfile.SourceConnectionName = comboBoxConnectionSource.SelectedItem.ToString();
                currentProfile.ExportFormat = comboBoxFormat.SelectedItem.ToString();
                currentProfile.AuditType = comboBoxAuditTypeExport.SelectedItem.ToString();

                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.SelectedActions = new List<int>();
                if (checkedListBoxActions.CheckedItems.Count == checkedListBoxActions.Items.Count)
                {
                    currentProfile.AllActionsSelected = true;
                }
                else
                {
                    currentProfile.AllActionsSelected = false;
                    foreach (ActionListBoxItem checkedAction in checkedListBoxActions.CheckedItems)
                    {
                        currentProfile.SelectedActions.Add(checkedAction.Key);
                    }
                }

                currentProfile.SelectedOperations = new List<int>();
                if (checkedListBoxOperations.CheckedItems.Count == checkedListBoxOperations.Items.Count)
                {
                    currentProfile.AllOperationsSelected = true;
                }
                else
                {
                    currentProfile.AllOperationsSelected = false;
                    for (int i = 0; i < checkedListBoxOperations.Items.Count; i++)
                    {
                        if (checkedListBoxOperations.CheckedItems.IndexOf(checkedListBoxOperations.Items[i]) > -1)
                        {
                            KeyValuePair<int, string> u = new KeyValuePair<int, string>(i + 1, checkedListBoxOperations.Items[i].ToString());
                            currentProfile.SelectedOperations.Add(u.Key);
                        }
                    }
                }

                currentProfile.SelectedUsers = new List<AuditUser>();
                if (checkedListBoxUsers.CheckedItems.Count == checkedListBoxUsers.Items.Count)
                {
                    currentProfile.AllUsersSelected = true;
                }
                else
                {
                    currentProfile.AllUsersSelected = false;
                    foreach (CheckListBoxItem checkedUser in checkedListBoxUsers.CheckedItems)
                    {
                        AuditUser u = new AuditUser { Id = checkedUser.Id, FullName = checkedUser.Text };
                        currentProfile.SelectedUsers.Add(u);
                    }
                }

                currentProfile.SelectedEntities = new List<SelectedAuditEntity>();
                if (checkedListBoxEntities.CheckedItems.Count == checkedListBoxEntities.Items.Count)
                    currentProfile.AllEntitiesSelected = true;
                else
                    currentProfile.AllEntitiesSelected = false;

                foreach (EntityListBoxItem checkedEntity in checkedListBoxEntities.CheckedItems)
                {
                    SelectedAuditEntity ee = new SelectedAuditEntity();
                    ee.LogicalName = checkedEntity.Value;
                    ee.ObjectTypeCode = checkedEntity.ObjectTypeCode;

                    SelectedAuditEntity seForIgnoredAttributes = SelectedEntityList.Find(match => match.LogicalName == checkedEntity.Value);
                    if (seForIgnoredAttributes != null)
                    {
                        ee.SelectedAttributes = seForIgnoredAttributes.SelectedAttributes;
                        ee.Filter = seForIgnoredAttributes.Filter;
                    }

                    currentProfile.SelectedEntities.Add(ee);
                }

                if (comboBoxAuditRecordCreatedOnFilter.SelectedItem != null)
                    currentProfile.AuditRecordCreatedOnFilter = comboBoxAuditRecordCreatedOnFilter.SelectedItem.ToString();
                currentProfile.AuditRecordCreatedOnFilterLastX = numericUpDownLastX.Value;
                currentProfile.AuditRecordCreatedOnFilterFrom = dateTimePickerAuditCreatedFrom.Value;
                currentProfile.AuditRecordCreatedOnFilterTo = dateTimePickerAuditCreatedTo.Value;

                currentProfile.setSourceConneciton();
                MSCRMAuditExportProfile 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;
        }
        /// <summary>
        /// Exports the entity.
        /// </summary>
        /// <param name="profile">The profile.</param>
        /// <param name="fetchXml">The fetch XML query.</param>
        /// <param name="PrimaryNameAttribute">The primary name attribute.</param>
        /// <param name="columns">The columns.</param>
        /// <param name="DisplayedColumns">The displayed columns.</param>
        /// <param name="sae">The sae.</param>
        /// <returns>The number of exported records</returns>
        public int ExportEntity(MSCRMAuditExportProfile profile, string fetchXml, string PrimaryNameAttribute, List<string> columns, List<string> DisplayedColumns, SelectedAuditEntity sae)
        {
            //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 recordCount = 0;
            // Specify the current paging cookie. For retrieving the first page, pagingCookie should be null.
            string pagingCookie = null;
            string entityName = "";

            while (true)
            {
                // Build fetchXml string with the placeholders.
                string xml = CreateXml(fetchXml, pagingCookie, fileNumber, fetchCount);

                RetrieveMultipleRequest fetchRequest = new RetrieveMultipleRequest
                {
                    Query = new FetchExpression(xml)
                };

                EntityCollection returnCollection = ((RetrieveMultipleResponse)_serviceProxy.Execute(fetchRequest)).EntityCollection;
                recordCount += returnCollection.Entities.Count;
                if (recordCount > 0)
                {
                    if (profile.AuditType == "User Acces Audit" || profile.AuditType == "Audit Summary View")
                    {
                        if (profile.ExportFormat.ToLower() == "csv")
                            WriteCSV(returnCollection, fileName, columns, DisplayedColumns);
                        else if (profile.ExportFormat.ToLower() == "xml")
                            WriteXML(returnCollection, fileName, columns, DisplayedColumns);
                        else if (profile.ExportFormat.ToLower() == "xml spreadsheet 2003")
                            WriteXMLSpreadsheet2003(returnCollection, fileName, columns, DisplayedColumns);
                    }
                    else
                    {
                        foreach (Entity e in returnCollection.Entities)
                        {
                            AuditDetailCollection details = new AuditDetailCollection();
                            if (profile.AuditType == "Record Change History")
                            {
                                RetrieveRecordChangeHistoryRequest changeRequest = new RetrieveRecordChangeHistoryRequest();
                                changeRequest.Target = new EntityReference(returnCollection.EntityName, e.Id);
                                RetrieveRecordChangeHistoryResponse changeResponse = (RetrieveRecordChangeHistoryResponse)_serviceProxy.Execute(changeRequest);
                                details = changeResponse.AuditDetailCollection;
                            }
                            else
                            {
                                //Attribute change history
                                var attributeChangeHistoryRequest = new RetrieveAttributeChangeHistoryRequest
                                {
                                    Target = new EntityReference(e.LogicalName, e.Id),
                                    AttributeLogicalName = sae.SelectedAttributes[0]
                                };

                                var attributeChangeHistoryResponse = (RetrieveAttributeChangeHistoryResponse)_serviceProxy.Execute(attributeChangeHistoryRequest);
                                details = attributeChangeHistoryResponse.AuditDetailCollection;
                            }

                            foreach (AuditDetail detail in details.AuditDetails)
                            {
                                DisplayAuditDetails(detail, profile, (string)e[PrimaryNameAttribute], fileName, columns, DisplayedColumns);
                            }
                        }
                    }
                }
                // 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;
                }
            }

            if (profile.ExportFormat.ToLower() == "xml" && File.Exists(fileName))
            {
                using (var writer = new StreamWriter(fileName, true, encoding))
                {
                    writer.WriteLine("</Records>");
                    writer.Flush();
                }
            }
            else if (profile.ExportFormat.ToLower() == "xml spreadsheet 2003" && File.Exists(fileName))
            {
                using (var writer = new StreamWriter(fileName, true, encoding))
                {
                    writer.WriteLine("</Table></Worksheet></Workbook>\n");
                    writer.Flush();
                }
            }

            LogManager.WriteLog("Exported " + recordCount + " " + entityName + " records.");

            return recordCount;
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            List<string> SelectedAttributes = new List<string>();

            if (au.AuditType == "Attribute Change History")
            {
                if (checkedListBoxAttributes.CheckedItems.Count != 1)
                {
                    MessageBox.Show("You must select 1 Attribute for this Entity!");
                    return;
                }

                for (int j = 0; j < checkedListBoxAttributes.CheckedItems.Count; j++)
                {
                    SelectedAttributes.Add(checkedListBoxAttributes.CheckedItems[j].ToString());
                }
            }

            if (SelectedAttributes.Count < 1)
                SelectedAttributes = null;

            SelectedAuditEntity se = new SelectedAuditEntity
            {
                LogicalName = this.entity,
                SelectedAttributes = SelectedAttributes,
                Filter = xmlEditor1.Text
            };

            int index = au.SelectedEntityList.FindIndex(match => match.LogicalName == this.entity);
            if (index > -1)
                au.SelectedEntityList[index] = se;
            else
                au.SelectedEntityList.Add(se);

            this.Dispose();
        }