Create a Task

The following sample workflow activity demonstrates how to create a task within an activity.



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);
}

}


}
}