コード例 #1
0
        /// <summary>
        /// Executes the WorkFlow.
        /// </summary>
        /// <param name="crmWorkflowContext">The <see cref="WorkFlowActivityBase.LocalWorkflowContext"/> </param> which contains the
        /// <param name="executionContext" > <see cref="CodeActivityContext"/>
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics 365 caches WorkFlow instances.
        /// The WorkFlow's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the WorkFlow. Also, multiple system threads
        /// could execute the WorkFlow at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in WorkFlows.
        /// </remarks>
        public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext)
        {
            crmWorkflowContext.TracingService.Trace("Workflow started");

            if (crmWorkflowContext == null)
            {
                throw new ArgumentNullException("crmWorkflowContext");
            }

            try
            {
                //Todo: active only
                //todo: exclude a record. so sibling distribution is possible
                //todo: Many-to-many
                //todo: FetchXML
                var wfRef = DistributedWorkflow.Get <EntityReference>(executionContext);

                if (wfRef == null)
                {
                    crmWorkflowContext.TracingService.Trace("Distributed Workflow Input is null.");
                    throw new NullReferenceException("Distributed Workflow");
                }

                // Get parent record ID
                var parentUrl = ParentRecordUrl.Get(executionContext);
                if (String.IsNullOrEmpty(parentUrl))
                {
                    throw new NullReferenceException("ParentRecordUrl");
                }
                var parser = new DynamicUrlParser(parentUrl);

                var relationshipName = RelationshipName.Get(executionContext);
                if (String.IsNullOrEmpty(relationshipName))
                {
                    throw new NullReferenceException("RelationshipName WF Input Parameter");
                }

                var keys = GetKeys(parser.GetEntityReference(crmWorkflowContext.OrganizationService), relationshipName, crmWorkflowContext.OrganizationService);

                this.Distribute(wfRef.Id, keys, crmWorkflowContext.OrganizationService, crmWorkflowContext.TracingService);
            }
            catch (FaultException <OrganizationServiceFault> e)
            {
                // Handle the exception.
                crmWorkflowContext.TracingService.Trace("Exception: {0}\r\nInner Exception {1}", e.Message,
                                                        e.InnerException);
                throw e;
            }

            crmWorkflowContext.TracingService.Trace("Workflow ended");
        }
コード例 #2
0
        /// <summary>
        /// Executes the workflow activity.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        protected override void Execute(CodeActivityContext executionContext)
        {
            // Create the tracing service
            ITracingService tracingService = executionContext.GetExtension <ITracingService>();

            if (tracingService == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
            }

            tracingService.Trace("Entered GetRecordGuid.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}",
                                 executionContext.ActivityInstanceId,
                                 executionContext.WorkflowInstanceId);

            // Create the context
            IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>();

            if (context == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");
            }

            tracingService.Trace("GetRecordGuid.Execute(), Correlation Id: {0}, Initiating User: {1}",
                                 context.CorrelationId,
                                 context.InitiatingUserId);

            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>();
            IOrganizationService        service        = serviceFactory.CreateOrganizationService(context.UserId);

            try
            {
                var entityReference = new DynamicUrlParser(RecordUrl.Get <string>(executionContext));

                RecordGuid.Set(executionContext, entityReference.Id.ToString());
                EntityLogicalName.Set(executionContext, entityReference.GetEntityLogicalName(service));
            }
            catch (FaultException <OrganizationServiceFault> e)
            {
                tracingService.Trace("Exception: {0}", e.ToString());

                // Handle the exception.
                throw;
            }

            tracingService.Trace("Exiting GetRecordGuid.Execute(), Correlation Id: {0}", context.CorrelationId);
        }
        /// <summary>
        /// Executes the WorkFlow.
        /// </summary>
        /// <param name="executionContext" > <see cref="CodeActivityContext"/>
        /// <param name="crmWorkflowContext">The <see cref="WorkFlowActivityBase.LocalWorkflowContext"/> which contains the
        /// </param>
        /// <remarks>
        /// For improved performance, Microsoft Dynamics 365 caches WorkFlow instances.
        /// The WorkFlow's Execute method should be written to be stateless as the constructor
        /// is not called for every invocation of the WorkFlow. Also, multiple system threads
        /// could execute the WorkFlow at the same time. All per invocation state information
        /// is stored in the context. This means that you should not use global variables in WorkFlows.
        /// </remarks>
        public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext)
        {
            if (crmWorkflowContext == null)
            {
                throw new ArgumentNullException("crmWorkflowContext");
            }

            try
            {
                var wfRef = DistributedWorkflow.Get <EntityReference>(executionContext);
                if (wfRef == null)
                {
                    crmWorkflowContext.TracingService.Trace("Distributed Workflow Input is null.");
                    throw new NullReferenceException("Distributed Workflow");
                }

                // Get initial record reference
                var initialRecordUrl = InitialRecordUrl.Get(executionContext);
                if (initialRecordUrl == null)
                {
                    throw new NullReferenceException("InitialRecordUrl");
                }
                var parser           = new DynamicUrlParser(initialRecordUrl);
                var initialRecordRef = parser.GetEntityReference(crmWorkflowContext.OrganizationService);
                if (initialRecordRef == null)
                {
                    crmWorkflowContext.TracingService.Trace("Cannot convert from the record URL {0} to a entity reference", initialRecordUrl);
                    throw new NullReferenceException("initialRecordRef");
                }

                var relationshipName = RelationshipName.Get(executionContext);
                if (String.IsNullOrEmpty(relationshipName))
                {
                    throw new NullReferenceException("relationshipName");
                }

                var keys = GetKeys(initialRecordRef, relationshipName, crmWorkflowContext.OrganizationService);

                this.Distribute(wfRef.Id, keys, crmWorkflowContext.OrganizationService, crmWorkflowContext.TracingService);
            }
            catch (FaultException <OrganizationServiceFault> e)
            {
                // Handle the exception.
                throw e;
            }
        }
コード例 #4
0
        /// <summary>
        /// Executes the workflow activity.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        protected override void Execute(CodeActivityContext executionContext)
        {
            // Create the tracing service
            ITracingService tracingService = executionContext.GetExtension <ITracingService>();

            if (tracingService == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
            }

            tracingService.Trace("Entered CreateDocumentLocation.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}",
                                 executionContext.ActivityInstanceId,
                                 executionContext.WorkflowInstanceId);

            // Create the context
            IWorkflowContext context = executionContext.GetExtension <IWorkflowContext>();

            if (context == null)
            {
                throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");
            }

            tracingService.Trace("CreateDocumentLocation.Execute(), Correlation Id: {0}, Initiating User: {1}",
                                 context.CorrelationId,
                                 context.InitiatingUserId);

            IOrganizationServiceFactory serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>();
            IOrganizationService        service        = serviceFactory.CreateOrganizationService(context.UserId);
            IOrganizationService        privService    = serviceFactory.CreateOrganizationService(null);

            try
            {
                var      url       = RecordUrl.Get <string>(executionContext);
                var      recordRef = new DynamicUrlParser(url).ToEntityReference(privService);
                string   config    = GetSecureConfigValue(privService, "PrivSharePointUser");;
                string[] user      = config.Split(';');

                // Create a new sharepoint service using the given priv sharepoint user credentials
                SPService spService   = new SPService(user[0], user[1]);
                var       docLocation = new DocumentLocationHelper(privService, spService);

                // Get the site passed into the workflow activity
                var siteId = Site.Get(executionContext);
                var site   = (SharePointSite)service.Retrieve(SharePointSite.EntityLogicalName, siteId.Id, new ColumnSet("sharepointsiteid", "absoluteurl"));

                if (site != null)
                {
                    // Set the name of the folder
                    recordRef.Name = RecordFolderName.Get <string>(executionContext);
                    var newLocation = docLocation.CreateDocumentLocation(site, DocumentLibraryName.Get(executionContext), recordRef);

                    DocumentLocation.Set(executionContext, newLocation.ToEntityReference());
                    DocumentFolder.Set(executionContext, newLocation.AbsoluteURL);
                }
                else
                {
                    DocumentLocation.Set(executionContext, null);
                }
            }
            catch (FaultException <OrganizationServiceFault> e)
            {
                tracingService.Trace("Exception: {0}", e.ToString());

                // Handle the exception.
                throw;
            }

            tracingService.Trace("Exiting CreateDocumentLocation.Execute(), Correlation Id: {0}", context.CorrelationId);
        }