Export (0) Print
Expand All

Custom Workflow Activity: Create a Task

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

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

This sample code can be found in the following files in the SDK download:

Server\FullSample\CustomWorkflowActivity\CS\CustomActivity.cs
Server\FullSample\CustomWorkflowActivity\VB\CustomActivity.cs

Example

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

        }


    }
}

See Also

Concepts


© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2014 Microsoft