Example #1
0
        private async void Connect()
        {
            if (_connector == null)
            {
                _connector = new Connector();
            }

            if (!_connector.Connect())
            {
                MessageBox.Show("Please click Connect to try connecting to Dynamics CRM again. A valid connection is required.");
                return;
            }

            IsBusy = true;

            if (_logic == null)
            {
                _logic = new ExecuteBulkWorkflowLogic(_connector.OrganizationServiceProxy);
            }
            else
            {
                _logic.OrganizationService = _connector.OrganizationServiceProxy;
            }

            ConnectText = "Reconnect";

            _messenger.Send(new UpdateHeaderMessage
            {
                Header = "Bulk Workflow Executor: " + _connector.OrganizationFriendlyName
            });

            var workflows = await RetrieveWorkflows();

            Workflows = new ObservableCollection <Workflow>(workflows);

            IsBusy = false;
        }
        protected override void ProcessRecord()
        {
            try
            {
                var service = ConnectToCrm();

                WriteDebug("Instantiating logic...");
                var logic = new ExecuteBulkWorkflowLogic(service);

                WriteDebug("Locating view...");
                View view = null;

                if (ViewId != Guid.Empty)
                {
                    WriteDebug("Locating view by Id...");
                    view = logic.RetrieveView(ViewId, EntityLogicalName, IsUserView.IsPresent);
                }
                else
                {
                    WriteDebug("Locating view by name...");
                    view = logic.RetrieveView(ViewName, EntityLogicalName, IsUserView.IsPresent);
                }

                if (view == null)
                {
                    throw new Exception("Could not locate the specified view.");
                }

                WriteDebug("Locating workflow...");
                Workflow workflow = null;

                if (WorkflowId != Guid.Empty)
                {
                    WriteDebug("Locating workflow by Id...");
                    workflow = logic.RetrieveWorkflow(WorkflowId);
                }
                else
                {
                    WriteDebug("Locating workflow by name...");
                    workflow = logic.RetrieveWorkflow(WorkflowName);
                }

                if (workflow == null)
                {
                    throw new Exception("Could not locate the specified workflow.");
                }

                WriteDebug("Transforming view to QueryExpression...");
                var query = logic.GetQuery(view.FetchXml);

                var moreRecords    = true;
                var page           = 1;
                var processedCount = 0;

                var progressRecord = new ProgressRecord(
                    0,
                    string.Format("Execute '{0}' against '{1}'.", workflow.Name, view.Name),
                    "Starting...");

                WriteProgress(progressRecord);

                do
                {
                    WriteDebug("Running workflow on page " + page);

                    var result = logic.Execute(query, workflow.WorkflowId, page);

                    if (result.HasError)
                    {
                        throw new Exception("Execute workflow failed. " + result.ErrorMessage);
                    }

                    processedCount += result.ProcessedCount;
                    moreRecords     = result.HasMoreResults;

                    progressRecord.StatusDescription = string.Format("{0} records processed...", processedCount);
                    WriteProgress(progressRecord);

                    page++;
                } while (moreRecords);

                WriteDebug("Completed.");
            }
            catch (Exception ex)
            {
                ThrowTerminatingError(new ErrorRecord(ex, ex.GetType().Name, ErrorCategory.NotSpecified, null));
            }
        }