Sample Custom Workflow Activity To Create a Task

Create a Task

using System;
using System.Collections;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Reflection;

using Microsoft.Crm.Workflow;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;

namespace SampleWorkflows
{
    [CrmWorkflowActivity("Create a Task")]
    public class CustomActivity : Activity
    {
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {

            IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
            IWorkflowContext context = contextService.Context;

            ICrmService crmService = context.CreateCrmService();

            DynamicEntity entity = new DynamicEntity();
            entity.Name = EntityName.task.ToString();
            entity.Properties = new PropertyCollection();
            entity.Properties.Add(new StringProperty("subject", taskId.Value.ToString()));
            entity.Properties.Add(new KeyProperty("activityid", new Key(taskId.Value)));
            crmService.Create(entity);

            return base.Execute(executionContext);
        }

        public static DependencyProperty taskIdProperty =
           DependencyProperty.Register("taskId",
           typeof(Lookup),
           typeof(CustomActivity));

        [CrmInput("The id")]
        [CrmOutput("The output")]
        [CrmReferenceTarget("task")]
        public Lookup taskId
        {
            get
            {
                return (Lookup)base.GetValue(taskIdProperty);
            }
            set
            {
                base.SetValue(taskIdProperty, value);
            }

        }

    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s