コード例 #1
0
        /// <summary>
        /// This method first creates 3 accounts with the same name, then issues a BulkDetectDuplicates
        /// request to show the duplicate detection.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user will be prompted to delete all
        /// created entities.</param>

        public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
        {
            try
            {
                //<snippetBulkDetectDuplicates1>
                // Connect to the Organization service. 
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,
                                                                     serverConfig.Credentials, serverConfig.DeviceCredentials))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();

                    CreateRequiredRecords();

                    // Create the BulkDetectDuplicatesRequest object
                    Console.WriteLine("  Creating the BulkDetectDuplicatesRequest object");
                    BulkDetectDuplicatesRequest request = new BulkDetectDuplicatesRequest()
                    {
                        JobName = "Detect Duplicate Accounts",
                        Query = new QueryExpression()
                        {
                            EntityName = Account.EntityLogicalName,
                            ColumnSet = new ColumnSet(true)
                        },
                        RecurrencePattern = String.Empty,
                        RecurrenceStartTime = DateTime.Now,
                        ToRecipients = new Guid[0],
                        CCRecipients = new Guid[0]
                    };

                    // Execute the request
                    Console.WriteLine("  Executing BulkDetectDuplicatesRequest");
                    _response = (BulkDetectDuplicatesResponse)_serviceProxy
                        .Execute(request);

                    #region check success

                    Console.WriteLine("  Waiting for job to complete...");
                    WaitForAsyncJobToFinish(_response.JobId, 120);

                    QueryByAttribute query = new QueryByAttribute()
                    {
                        ColumnSet = new ColumnSet(true),
                        EntityName = "duplicaterecord"
                    };
                    query.Attributes.Add("asyncoperationid");
                    query.Values.Add(_response.JobId);
                    EntityCollection results = _serviceProxy.RetrieveMultiple(query);

                    // check to make sure each id is found in the collection
                    var duplicateIds = results.Entities.Select((entity) => 
                        ((DuplicateRecord)entity).BaseRecordId.Id);
                    foreach (var id in _duplicateAccounts.Select((account) => account.Id))
                    {
                        if (!duplicateIds.Contains(id))
                        {
                            throw new Exception(String.Format(
                                "Account with ID {0} was not detected as a duplicate",
                                id));
                        }
                    }
                    Console.WriteLine("  All accounts detected as duplicates successfully");

                    #endregion

                    Console.WriteLine();

                    DeleteRequiredRecords(promptforDelete);
                }
                //</snippetBulkDetectDuplicates1>
            }

            // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }
コード例 #2
0
        /// <summary>
        /// This method first creates 3 accounts with the same name, then issues a BulkDetectDuplicates
        /// request to show the duplicate detection.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user will be prompted to delete all
        /// created entities.</param>

        public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
        {
            try
            {
                // Connect to the Organization service.
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,
                                                                    serverConfig.Credentials, serverConfig.DeviceCredentials))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();

                    CreateRequiredRecords();

                    // Create the BulkDetectDuplicatesRequest object
                    Console.WriteLine("  Creating the BulkDetectDuplicatesRequest object");
                    BulkDetectDuplicatesRequest request = new BulkDetectDuplicatesRequest()
                    {
                        JobName = "Detect Duplicate Accounts",
                        Query   = new QueryExpression()
                        {
                            EntityName = Account.EntityLogicalName,
                            ColumnSet  = new ColumnSet(true)
                        },
                        RecurrencePattern   = String.Empty,
                        RecurrenceStartTime = DateTime.Now,
                        ToRecipients        = new Guid[0],
                        CCRecipients        = new Guid[0]
                    };

                    // Execute the request
                    Console.WriteLine("  Executing BulkDetectDuplicatesRequest");
                    _response = (BulkDetectDuplicatesResponse)_serviceProxy
                                .Execute(request);

                    #region check success

                    Console.WriteLine("  Waiting for job to complete...");
                    WaitForAsyncJobToFinish(_response.JobId, 120);

                    QueryByAttribute query = new QueryByAttribute()
                    {
                        ColumnSet  = new ColumnSet(true),
                        EntityName = "duplicaterecord"
                    };
                    query.Attributes.Add("asyncoperationid");
                    query.Values.Add(_response.JobId);
                    EntityCollection results = _serviceProxy.RetrieveMultiple(query);

                    // check to make sure each id is found in the collection
                    var duplicateIds = results.Entities.Select((entity) =>
                                                               ((DuplicateRecord)entity).BaseRecordId.Id);
                    foreach (var id in _duplicateAccounts.Select((account) => account.Id))
                    {
                        if (!duplicateIds.Contains(id))
                        {
                            throw new Exception(String.Format(
                                                    "Account with ID {0} was not detected as a duplicate",
                                                    id));
                        }
                    }
                    Console.WriteLine("  All accounts detected as duplicates successfully");

                    #endregion

                    Console.WriteLine();

                    DeleteRequiredRecords(promptforDelete);
                }
            }

            // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }
        private void tsbGenerate_Click(object sender, EventArgs e)
        {
            #region # Varibale declaration #
            EntityCollection entityColl = null;
            CreateExcelDoc   excell_app = null;
            int  progressCounter        = 0;
            bool createJob = false;
            #endregion # Varibale declaration #

            if (txtFilePath.Text.Length == 0)
            {
                MessageBox.Show(this, "Please select file location.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (this.lvAttributes.CheckedItems == null || this.lvAttributes.CheckedItems.Count <= 0)
            {
                MessageBox.Show(this, "Please select Attributes.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (this.cmbDuplicateDetectionJobs.SelectedItem == null && ApplicationSetting.ExistingJobIds != null && ApplicationSetting.ExistingJobIds.Count > 0)
            {
                // MessageBox.Show(this, "Please select existing Job to run.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                DialogResult result = MessageBox.Show("Please select existing Job to run. If you want to run Job for all records click Yes, else click No button.", "Confirmation", MessageBoxButtons.YesNoCancel);
                if (result == DialogResult.Yes)
                {
                    createJob = true;
                }
                else if (result == DialogResult.No)
                {
                    return;
                }
            }
            else if (ApplicationSetting.ExistingJobIds.Count <= 0)
            {
                DialogResult result = MessageBox.Show("If you want to run Job for all records click Yes, else click No button.", "Confirmation", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    createJob = true;
                }
                else if (result == DialogResult.No)
                {
                    return;
                }
            }

            GetAttributesList();

            WorkAsync(new WorkAsyncInfo
            {
                Message       = string.Concat("Duplicate ", ApplicationSetting.SelectedEntity.DisplayName, " detection started......"),
                AsyncArgument = null,
                Work          = (bw, evt) =>
                {
                    #region # Bulk Detection #
                    Guid jobId          = Guid.Empty;
                    bool isJobCompleted = false;
                    if (createJob == true)
                    {
                        BulkDetectDuplicatesRequest request = new BulkDetectDuplicatesRequest()
                        {
                            JobName = string.Format("{0} {1} {2}", "Detect duplicate", ApplicationSetting.SelectedEntity.DisplayName, DateTime.Now),
                            Query   = new QueryExpression()
                            {
                                EntityName = ApplicationSetting.SelectedEntity.LogicalName,
                                ColumnSet  = new ColumnSet(true)
                            },

                            RecurrencePattern   = String.Empty,
                            RecurrenceStartTime = DateTime.Now,
                            ToRecipients        = new Guid[0],
                            CCRecipients        = new Guid[0]
                        };

                        BulkDetectDuplicatesResponse _response = (BulkDetectDuplicatesResponse)Service.Execute(request);
                        jobId = _response.JobId;
                        UpdateJobStatus(jobId);
                        isJobCompleted = IsJobCompleted(jobId);
                    }
                    else
                    {
                        this.Invoke(new Action(() => { jobId = new Guid(cmbDuplicateDetectionJobs.SelectedValue.ToString()); }));

                        isJobCompleted = true;
                    }

                    #endregion # Bulk Detection #

                    if (jobId != Guid.Empty && isJobCompleted)
                    {
                        ApplicationSetting.JobId.Add(jobId);

                        entityColl = GetDuplicateRecords(jobId);
                        ApplicationSetting.DuplicateCollection = new EntityCollection();
                        ApplicationSetting.DuplicateRecords    = new List <DuplicateRecords>();

                        if (entityColl != null && entityColl.Entities.Count > 0)
                        {
                            this.Invoke(new Action(() =>
                            {
                                btnMergeDuplicates.Enabled     = true;
                                rbCreatedOn.Enabled            = true;
                                rbModifiedOn.Enabled           = true;
                                rbNoOfActivities.Enabled       = true;
                                rbnNoOfCompletedFields.Enabled = true;
                            }));

                            excell_app = new CreateExcelDoc(txtFilePath.Text.Trim());

                            //GenerateHeader(excell_app);

                            bw.ReportProgress(100, string.Concat("Duplicate ", ApplicationSetting.SelectedEntity.DisplayName, " found (", entityColl.Entities.Count, ")"));

                            int row = 1;

                            bw.ReportProgress(0, "Retrieving duplicate records........");
                            int rowCount = 0;
                            IEnumerable <System.Linq.IGrouping <object, Entity> > entityCollGroupBy = entityColl.Entities.GroupBy(ent => ent.Attributes[ApplicationSetting.SelectedEntity.PrimaryIdAttribute]);
                            List <Guid> chidList = new List <Guid>();
                            foreach (IEnumerable <Entity> elements in entityCollGroupBy)
                            {
                                foreach (Entity entity in elements)
                                {
                                    bw.ReportProgress(progressCounter * 100 / entityColl.Entities.Count(), string.Concat("Generating duplicate report........"));
                                    if (entity != null)
                                    {
                                        if (!DoesItContain(ApplicationSetting.DuplicateRecords, entity.Id))
                                        {
                                            rowCount++;
                                            DuplicateRecords rec = new DuplicateRecords();
                                            rec.MasterId         = entity.Id;
                                            if (elements != null && elements.Count() > 0)
                                            {
                                                var activityGroupBy = elements.Where(cnt => cnt.Attributes.Contains("Activities.activitytypecode") && cnt.Attributes["Activities.activitytypecode"] != null);
                                                entity.Attributes["ActivitiesCount"] = activityGroupBy != null ? activityGroupBy.Count() : 0;
                                            }
                                            else
                                            {
                                                entity.Attributes["ActivitiesCount"] = 0;
                                            }
                                            rec.MasterEntity = entity;

                                            Entity duplicateTemp = new Entity(ApplicationSetting.SelectedEntity.LogicalName);
                                            duplicateTemp.Id     = entity.Id;
                                            var re = new RetrieveDuplicatesRequest
                                            {
                                                BusinessEntity     = duplicateTemp,
                                                MatchingEntityName = duplicateTemp.LogicalName,
                                                PagingInfo         = new PagingInfo()
                                                {
                                                    PageNumber = 1, Count = 500
                                                }
                                            };

                                            var response = (RetrieveDuplicatesResponse)Service.Execute(re);
                                            if (response != null && response.DuplicateCollection != null)
                                            {
                                                List <Entity> childRecords = new List <Entity>();
                                                rec.ChildIds = new List <Guid>();
                                                foreach (Entity duplicate in response.DuplicateCollection.Entities)
                                                {
                                                    if (!DoesItContain(ApplicationSetting.DuplicateRecords, duplicate.Id))
                                                    {
                                                        rowCount++;
                                                        rec.ChildIds.Add(duplicate.Id);
                                                        chidList.Add(duplicate.Id);
                                                        childRecords.Add(duplicate);
                                                    }
                                                }
                                                rec.ChildEntity = childRecords;
                                            }
                                            ApplicationSetting.DuplicateRecords.Add(rec);
                                        }
                                        progressCounter++;

                                        break;// Break every time...Row is repeating for every activity
                                    }
                                }
                            }

                            bw.ReportProgress(0, "Generating Duplicate report........");
                            progressCounter = 0;
                            List <DuplicateRecords> DuplicateRecords = ApplicationSetting.DuplicateRecords;
                            EntityCollection childs = new EntityCollection();
                            if (chidList != null && chidList.Count() > 0)
                            {
                                childs = GetRelatedDuplicateRecords(chidList.ToArray());
                            }

                            int columnCount = ApplicationSetting.AttributesToDisplayName.Count() + 2;
                            var data        = new object[rowCount + 1, columnCount];

                            GenerateHeader(excell_app, ref data);

                            for (int indexofRecord = 0; indexofRecord < DuplicateRecords.Count(); indexofRecord++)
                            {
                                bw.ReportProgress(progressCounter * 100 / DuplicateRecords.Count(), string.Concat("Generating Duplicate report........"));
                                ApplicationSetting.DuplicateRecords[indexofRecord].MasterEntity = GetExcelPopulated(DuplicateRecords[indexofRecord].MasterEntity, 0, excell_app, ref row, null, Convert.ToInt32(DuplicateRecords[indexofRecord].MasterEntity.Attributes["ActivitiesCount"]), true, ref data);

                                for (int indexOfChild = 0; indexOfChild < DuplicateRecords[indexofRecord].ChildEntity.Count(); indexOfChild++)
                                {
                                    if (DuplicateRecords[indexofRecord].ChildEntity.ElementAt(indexOfChild) != null)
                                    {
                                        var activities = childs != null && childs.Entities.Count > 0 ? (childs.Entities.Where(i => ((EntityReference)i.Attributes["regardingobjectid"]).Id == DuplicateRecords[indexofRecord].ChildEntity.ElementAt(indexOfChild).Id).Count()) : 0;
                                        ApplicationSetting.DuplicateRecords[indexofRecord].ChildEntity[indexOfChild] = GetExcelPopulated(DuplicateRecords[indexofRecord].ChildEntity.ElementAt(indexOfChild), 0, excell_app, ref row, null, activities, false, ref data);
                                    }
                                }
                                progressCounter++;
                            }

                            excell_app.WriteArray(rowCount + 1, columnCount, data);
                            bw.ReportProgress(100, "Duplicate detection completed........");

                            excell_app.worksheet.SaveAs(txtFilePath.Text);

                            this.Invoke(new Action(() => { MessageBox.Show(this, "Duplicate Detection Completed."); }));

                            this.Invoke(new Action(() =>
                            {
                                excell_app.app.DisplayAlerts  = true;
                                excell_app.app.Calculation    = Microsoft.Office.Interop.Excel.XlCalculation.xlCalculationAutomatic;
                                excell_app.app.ScreenUpdating = true;
                                excell_app.app.Visible        = true;
                            }));

                            excell_app.ReleaseObject();
                            excell_app = null;

                            this.Invoke(new Action(() => { txtFilePath.Text = ""; }));
                        }
                        else
                        {
                            this.Invoke(new Action(() => { MessageBox.Show(this, "No Duplicates found."); }));
                        }

                        cmbDuplicateDetectionJobs.DataSource = null;
                        // Bind the Jobs
                        GetExistingJobs();
                    }
                },
                PostWorkCallBack = evt =>
                {
                    if (evt.Error != null)
                    {
                        this.Invoke(new Action(() => { MessageBox.Show(this, "An error occured " + evt.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }));
                    }
                },
                ProgressChanged = evt => { SetWorkingMessage(string.Format("{0}%\r\n{1}", evt.ProgressPercentage, evt.UserState)); }
            });
        }