/// <summary>
        /// Executes the WorkFlow.
        /// </summary>
        /// <param name="crmWorkflowContext">The <see cref="LocalWorkflowContext"/> 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)
        {
            if (crmWorkflowContext == null)
            {
                throw new ArgumentNullException("crmWorkflowContext");
            }

            try
            {
                # region Create the services
                var tracingService = executionContext.GetExtension <ITracingService>();
                if (tracingService == null)
                {
                    throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
                }
                tracingService.Trace($"Entered ShareRecordWithTeam.ExecuteCRMWorkFlowActivity(), Activity Instance Id: {executionContext.ActivityInstanceId}, Workflow Instance Id: {executionContext.WorkflowInstanceId}");
                var serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>();
                var service        = serviceFactory.CreateOrganizationService(null);
                #endregion

                #region Get Parameters

                bool   shareAppend      = this.ShareAppend.Get(executionContext);
                bool   shareAppendTo    = this.ShareAppendTo.Get(executionContext);
                bool   shareAssign      = this.ShareAssign.Get(executionContext);
                bool   shareDelete      = this.ShareDelete.Get(executionContext);
                bool   shareRead        = this.ShareRead.Get(executionContext);
                bool   shareShare       = this.ShareShare.Get(executionContext);
                bool   shareWrite       = this.ShareWrite.Get(executionContext);
                string sharingRecordUrl = SharingRecordURL.Get(executionContext);
                if (string.IsNullOrEmpty(sharingRecordUrl))
                {
                    return;
                }
                var refObject = DataAccessMetaData.GetEntityReferenceFromRecordUrl(service, sharingRecordUrl);

                List <EntityReference> principals    = new List <EntityReference>();
                EntityReference        teamReference = Team.Get(executionContext);
                principals.Clear();
                if (teamReference != null)
                {
                    principals.Add(teamReference);
                }
                #endregion

                #region Grant Access Request

                tracingService.Trace("Grant Request Start");

                service.GrantAccess(refObject, principals, shareAppend, shareAppendTo, shareAssign, shareDelete,
                                    shareRead, shareShare, shareWrite);

                tracingService.Trace("Grant Request End");

                #endregion
            }
        /// <summary>
        /// Executes the WorkFlow.
        /// </summary>
        /// <param name="crmWorkflowContext">The <see cref="LocalWorkflowContext"/> 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)
        {
            if (crmWorkflowContext == null)
            {
                throw new ArgumentNullException("crmWorkflowContext");
            }

            try
            {
                #region Create the services
                var tracingService = executionContext.GetExtension <ITracingService>();
                if (tracingService == null)
                {
                    throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
                }
                tracingService.Trace($"Entered UnshareRecordWithTeam.ExecuteCRMWorkFlowActivity(), Activity Instance Id: {executionContext.ActivityInstanceId}, Workflow Instance Id: {executionContext.WorkflowInstanceId}");
                var serviceFactory = executionContext.GetExtension <IOrganizationServiceFactory>();
                var service        = serviceFactory.CreateOrganizationService(null);
                #endregion

                #region Get Parameters

                string sharingRecordUrl = SharingRecordUrl.Get(executionContext);
                if (string.IsNullOrEmpty(sharingRecordUrl))
                {
                    return;
                }
                var refObject = DataAccessMetaData.GetEntityReferenceFromRecordUrl(service, sharingRecordUrl);

                //
                // Why do we need an List of principals?
                //
                List <EntityReference> principals    = new List <EntityReference>();
                EntityReference        teamReference = Team.Get(executionContext);
                principals.Clear();

                if (teamReference != null)
                {
                    principals.Add(teamReference);
                }
                #endregion

                #region Revoke Access

                service.RevokeAccess(refObject, principals);

                tracingService.Trace("Revoked Permissions--- OK");

                #endregion
            }
            catch (FaultException <OrganizationServiceFault> e)
            {
                // Handle the exception.
                throw e;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Main Execution Method
        /// </summary>
        /// <param name="executionContext">Activity Execution Context</param>
        /// <param name="crmWorkflowContext">Includes organisation service and trace service</param>
        public override void ExecuteCRMWorkFlowActivity(CodeActivityContext executionContext, LocalWorkflowContext crmWorkflowContext)
        {
            // 1. Validation
            string parentRecordUrl = this.ParentRecordURL.Get(executionContext);

            if (string.IsNullOrEmpty(parentRecordUrl))
            {
                return;
            }

            // 2. Get the entity reference for the given record url
            EntityReference entityReference = DataAccessMetaData.GetEntityReferenceFromRecordUrl(crmWorkflowContext.OrganizationService, parentRecordUrl);

            // 3. Deactivate all queue items for the record
            DataAccessQueues.DeactivateQueueItems(crmWorkflowContext.OrganizationService, entityReference.Id);
        }