private void ToolStripButton1_Click(object sender, EventArgs e)
        {
            Hashtable processList;

            if (txtFilePath.Text.Length == 0)
            {
                return;
            }
            if (File.Exists(txtFilePath.Text))
            {
                processList = ReadExcelFile(txtFilePath.Text);

                //checkedListBox1.Items.Clear();
                for (int i = 0; i < checkedListBox1.Items.Count; i++)
                {
                    WorkflowList item        = (WorkflowList)checkedListBox1.Items[i];
                    string       guidValue   = item.RealValue.ToString();
                    bool         statusValue = checkedListBox1.GetItemCheckState(i).ToString() == "Checked" ? true : false;

                    if (processList[guidValue] == null)
                    {
                        continue;
                    }

                    string excelStatusValue = processList[guidValue].ToString();
                    bool   excelStatus      = excelStatusValue == "Activated" ? true : false;

                    if (statusValue != excelStatus)
                    {
                        checkedListBox1.SetItemChecked(checkedListBox1.Items.IndexOf(item), excelStatus);
                    }
                }
            }
        }
        private void UpdateWorkflows()
        {
            List <WorkflowList> items = new List <WorkflowList>();

            WorkAsync(new WorkAsyncInfo
            {
                Message = "Updating Workflows...",
                Work    = (bw, evt) =>
                {
                    int totalItems = checkedListBox1.Items.Count;

                    for (int i = 0; i < totalItems; i++)
                    {
                        if (errorMsg.Length > 0)
                        {
                            errorMsg = "";
                            break;
                        }
                        WorkflowList item = (WorkflowList)checkedListBox1.Items[i];
                        //Guid wfGuid = new Guid(item.RealValue);
                        bool origStatus = item.State;

                        bool currentStatus        = checkedListBox1.GetItemCheckState(i).ToString() == "Checked" ? true : false;
                        string currentStatusValue = currentStatus ? "Activated" : "Draft";

                        if (origStatus != currentStatus)
                        {
                            item.State = currentStatus;
                            items.Add(item);
                            SetStateWorkflow(item, currentStatusValue, Service);
                        }
                    }
                },
                PostWorkCallBack = evt =>
                {
                    if (evt.Error != null)
                    {
                        string errorMessage = CrmExceptionHelper.GetErrorMessage(evt.Error, true);
                        MessageBox.Show(this, errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    foreach (WorkflowList item in items)
                    {
                        checkedListBox1.Items[checkedListBox1.Items.IndexOf(item)] = item;
                    }
                },
                AsyncArgument = null,
                IsCancelable  = true,
                MessageWidth  = 340,
                MessageHeight = 150
            });
        }
        private void SetStateWorkflow(WorkflowList item, string state, IOrganizationService service)
        {
            SetStateRequest stateRequest;
            int             statecode, statuscode;
            Guid            workflowid = new Guid(item.RealValue);

            try
            {
                if (state == "Draft")
                {
                    statecode  = 0;
                    statuscode = 1;

                    stateRequest = new SetStateRequest
                    {
                        EntityMoniker = new EntityReference("workflow", workflowid),
                        State         = new OptionSetValue(statecode),
                        Status        = new OptionSetValue(statuscode)
                    };
                    SetStateResponse stateSet = (SetStateResponse)service.Execute(stateRequest);
                }

                if (state == "Activated")
                {
                    statecode  = 1;
                    statuscode = 2;

                    stateRequest = new SetStateRequest
                    {
                        EntityMoniker = new EntityReference("workflow", workflowid),
                        State         = new OptionSetValue(statecode),
                        Status        = new OptionSetValue(statuscode)
                    };

                    SetStateResponse stateSet = (SetStateResponse)service.Execute(stateRequest);
                }
            }
            catch (System.ObjectDisposedException ex)
            {
                string msg = ex.Message;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Manage Workflows");
                errorMsg = ex.Message;
            }
        }