private void FindContacts(Guid guid)
        {
            Guid                        selectedUser   = (Guid)ddlUsers.SelectedValue;
            List <AuditItem>            data           = new List <Model.AuditItem>();
            FetchExpression             query          = null;
            Tuple <int, string, string> selectedEntity = (Tuple <int, string, string>)ddlEntities.SelectedItem;
            string                      auditFetchXml;

            if (selectedUser == Guid.Empty)
            {
                auditFetchXml = string.Format(ConnectionDetail.OrganizationMajorVersion < 8 ?
                                              FetchXml.DeletedAuditLogs.Replace("regardingobject", "object") :
                                              FetchXml.DeletedAuditLogs,
                                              dateFrom.Value.ToString("yyyy-MM-dd"),
                                              dateTo.Value.AddDays(1).ToString("yyyy-MM-dd"),
                                              2);
            }
            else
            {
                auditFetchXml = string.Format(ConnectionDetail.OrganizationMajorVersion < 8 ?
                                              FetchXml.DeleteAuditLogsByUser.Replace("regardingobject", "object") :
                                              FetchXml.DeleteAuditLogsByUser,
                                              dateFrom.Value.ToString("yyyy-MM-dd"),
                                              dateTo.Value.AddDays(1).ToString("yyyy-MM-dd"),
                                              2,
                                              selectedUser);
            }
            query = new FetchExpression(auditFetchXml);
            var queryResult = Service.RetrieveMultiple(query);

            foreach (Entity item in queryResult.Entities)
            {
                RetrieveAuditDetailsRequest auditDetailRequest = new RetrieveAuditDetailsRequest();
                auditDetailRequest.AuditId = item.Id;
                RetrieveAuditDetailsResponse response        = (RetrieveAuditDetailsResponse)Service.Execute(auditDetailRequest);
                AttributeAuditDetail         attributeDetail = (AttributeAuditDetail)response.AuditDetail;
                EntityMetadata metadata = entityMetadataList.FirstOrDefault(x => (x.ObjectTypeCode == 2));

                AuditItem auditItem = new Model.AuditItem()
                {
                    AuditId      = item.Id,
                    DeletedBy    = ((EntityReference)item["userid"]).Name,
                    DeletionDate = (DateTime)item["createdon"],
                    Entity       = ((EntityReference)item["objectid"]).LogicalName,
                    RecordId     = ((EntityReference)item["objectid"]).Id,
                    AuditDetail  = attributeDetail,
                    Metadata     = metadata
                };

                if (selectedEntity.Item3 != null && attributeDetail.OldValue.Contains(selectedEntity.Item3))
                {
                    auditItem.Name = attributeDetail.OldValue[selectedEntity.Item3].ToString();
                }

                data.Add(auditItem);
            }
            var contact = data.First(x => x.AuditDetail.OldValue.Id == guid);

            Service.Create(contact.AuditDetail.OldValue);
        }
        /// <summary>
        /// Handles the Click event of the btnShowRecords 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 btnShowRecords_Click(object sender, EventArgs e)
        {
            if (IsValid())
            {
                WorkAsync(new WorkAsyncInfo
                {
                    Message = "Loading deleted records...",
                    Work    = (w, ev) =>
                    {
                        Guid selectedUser     = (Guid)ddlUsers.SelectedValue;
                        List <AuditItem> data = new List <Model.AuditItem>();
                        FetchExpression query = null;
                        Tuple <int, string, string> selectedEntity = (Tuple <int, string, string>)ddlEntities.SelectedItem;
                        string auditFetchXml;
                        if (selectedUser == Guid.Empty)
                        {
                            auditFetchXml = string.Format(ConnectionDetail.OrganizationMajorVersion < 8 ?
                                                          FetchXml.DeletedAuditLogs.Replace("regardingobject", "object") :
                                                          FetchXml.DeletedAuditLogs,
                                                          dateFrom.Value.ToString("yyyy-MM-dd"),
                                                          dateTo.Value.AddDays(1).ToString("yyyy-MM-dd"),
                                                          ddlEntities.SelectedValue);
                        }
                        else
                        {
                            auditFetchXml = string.Format(ConnectionDetail.OrganizationMajorVersion < 8 ?
                                                          FetchXml.DeleteAuditLogsByUser.Replace("regardingobject", "object") :
                                                          FetchXml.DeleteAuditLogsByUser,
                                                          dateFrom.Value.ToString("yyyy-MM-dd"),
                                                          dateTo.Value.AddDays(1).ToString("yyyy-MM-dd"),
                                                          ddlEntities.SelectedValue,
                                                          selectedUser);
                        }
                        query           = new FetchExpression(auditFetchXml);
                        var queryResult = Service.RetrieveMultiple(query);
                        foreach (Entity item in queryResult.Entities)
                        {
                            RetrieveAuditDetailsRequest auditDetailRequest = new RetrieveAuditDetailsRequest();
                            auditDetailRequest.AuditId            = item.Id;
                            RetrieveAuditDetailsResponse response = (RetrieveAuditDetailsResponse)Service.Execute(auditDetailRequest);
                            AttributeAuditDetail attributeDetail  = (AttributeAuditDetail)response.AuditDetail;
                            EntityMetadata metadata = entityMetadataList.FirstOrDefault(x => (x.ObjectTypeCode == selectedEntity.Item1));
                            AuditItem auditItem     = new Model.AuditItem()
                            {
                                AuditId      = item.Id,
                                DeletedBy    = ((EntityReference)item["userid"]).Name,
                                DeletionDate = (DateTime)item["createdon"],
                                Entity       = ((EntityReference)item["objectid"]).LogicalName,
                                RecordId     = ((EntityReference)item["objectid"]).Id,
                                AuditDetail  = attributeDetail,
                                Metadata     = metadata
                            };

                            if (selectedEntity.Item3 != null && attributeDetail.OldValue.Contains(selectedEntity.Item3))
                            {
                                auditItem.Name = attributeDetail.OldValue[selectedEntity.Item3].ToString();
                            }

                            data.Add(auditItem);
                        }

                        ev.Result = data.OrderByDescending(x => x.DeletionDate).ToList();
                    },
                    ProgressChanged = ev =>
                    {
                        // If progress has to be notified to user, use the following method:
                        SetWorkingMessage("Loading entities with auditing enabled...");
                    },
                    PostWorkCallBack = ev =>
                    {
                        GridDeletedRecords.DataSource = ev.Result;
                    },
                    AsyncArgument = null,
                    IsCancelable  = true,
                    MessageWidth  = 340,
                    MessageHeight = 150
                });
            }
        }