public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext     context        = serviceProvider.GetPluginExecutionContext();
            IOrganizationServiceFactory serviceFactory = serviceProvider.GetOrganizationServiceFactory();
            // Who is a caller in async process?
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);

            // Get input parameters
            EntityReference jobReference = context.GetInputParameter <EntityReference>(Target);
            Entity          job          = service.Retrieve(jobReference, "fixrm_scheduleid");

            // Get schedule
            EntityReference scheduleReference = job.GetAttributeValue <EntityReference>("fixrm_scheduleid");
            Entity          schedule          = service.Retrieve(scheduleReference, "fixrm_pattern");

            // Get recurrence pattern
            string         pattern    = schedule.GetAttributeValue <string>("fixrm_pattern");
            CronExpression recurrence = new CronExpression(pattern);

            // Calculate next occurrence date and set output parameters
            // From last run?
            DateTimeOffset?nextRun = recurrence.GetTimeAfter(DateTimeOffset.UtcNow);

            context.OutputParameters[NextRecurrence] = nextRun?.UtcDateTime;
        }
 /// <summary>
 /// Gets the target entity from plugin context input parameters.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <returns>Target entity.</returns>
 /// <exception cref="System.ArgumentNullException">context</exception>
 public static Entity GetTargetEntity(this IPluginExecutionContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     return(context.GetInputParameter <Entity>("Target"));
 }
        /// <summary>
        /// Gets the named input parameters and attempts to cast as type T.
        /// </summary>
        /// <typeparam name="T">Type of the input parameter.</typeparam>
        /// <param name="context">The context.</param>
        /// <param name="parameterName">Name of the parameter.</param>
        /// <returns>Input parameter as type T, or null if does not cast.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// context
        /// or
        /// parameterName
        /// </exception>
        public static T GetInputParameter <T>(this IPluginExecutionContext context, string parameterName) where T : class
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (parameterName == null)
            {
                throw new ArgumentNullException("parameterName");
            }

            var target = context.GetInputParameter(parameterName);

            // TODO: should probably throw an exception if the cast fails.
            return(target as T);
        }
Пример #4
0
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext     context = serviceProvider.GetPluginExecutionContext();
            IOrganizationServiceFactory factory = serviceProvider.GetOrganizationServiceFactory();
            // Who is a caller in async process?
            IOrganizationService service = factory.CreateOrganizationService(context.InitiatingUserId);

            // Get input parameters
            EntityReference target = context.GetInputParameter <EntityReference>(Target);
            Entity          job    = service.Retrieve(target, "fixrm_query", "fixrm_processid");

            String          query     = job.GetAttributeValue <String>("fixrm_query");
            EntityReference processId = job.GetAttributeValue <EntityReference>("fixrm_processid");

            // No query means that process should be executed against job record itself
            if (string.IsNullOrEmpty(query))
            {
                service.Execute(new ExecuteWorkflowRequest()
                {
                    EntityId   = target.Id,
                    WorkflowId = processId.Id
                });
            }
            // If job has query, process should be executed against each query result
            else
            {
                IEnumerable <Entity> results = service.RetrieveMultiple(new FetchExpression(query), null);

                foreach (Entity result in results)
                {
                    service.Execute(new ExecuteWorkflowRequest()
                    {
                        EntityId   = result.Id,
                        WorkflowId = processId.Id
                    });
                }
            }
        }