Custom Workflow Activity - Add to Marketing List

by Bill Owens 26. November 2008 18:39
Wednesday, 22 October 2008 Posted by Chris Cohen


I needed to use workflow to add a new member to a marketing list, but found I needed to create a custom workflow activity to do this. In fact once you have VS2005 setup with workflow project types it wasn't too hard.

You need to use Dependency Properties to pass parameters in (and out). The worflow activity form assistant will allow you to set some dynamically. You can use the context to get a handle to the crmService. CrmWorkflowActivity defines the menu labelling in the workflow activity.

Use the Developer PluginRegistration tool to register it, or there is sample installer code in the SDK workflow walkthrough.

Hope this helps someone.


using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
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 Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Workflow;

namespace Vizola.VEMWorkflow
{
[CrmWorkflowActivity("Add to Marketing List", "VEM Workflow")]
public partial class Add2ML: SequenceActivity
{
public static DependencyProperty listIdProperty = DependencyProperty.Register("listId", typeof(Lookup), typeof(Add2ML));
[CrmInput("Marketing List")]
[CrmReferenceTarget("list")]
public Lookup listId
{
get
{
return (Lookup)base.GetValue(listIdProperty);
}
set
{
base.SetValue(listIdProperty, value);
}
}
public static DependencyProperty contactIdProperty = DependencyProperty.Register("contactId", typeof(Lookup), typeof(Add2ML));
[CrmInput("Contact")]
[CrmReferenceTarget("contact")]
public Lookup contactId
{
get
{
return (Lookup)base.GetValue(contactIdProperty);
}
set
{
base.SetValue(contactIdProperty, value);
}
}
public Add2ML()
{
InitializeComponent();
}
///
/// The Execute method is called by the workflow runtime to execute an activity.
///

/// The context for the activity
///
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
// Get the context service.
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext context = contextService.Context;
// Use the context service to create an instance of CrmService.
ICrmService crmService = context.CreateCrmService(true);

AddMemberListRequest amReq = new AddMemberListRequest();
amReq.EntityId = contactId.Value;
amReq.ListId = listId.Value;
AddMemberListResponse amRes = (AddMemberListResponse)crmService.Execute(amReq);

return ActivityExecutionStatus.Closed;
}
}
}

Tags: , ,

CRM 4.0

Page List

About the author

I work for a consulting firm in Dublin Ohio called Affiliated Resource Group. For the last five years I have been spearheading our Microsoft Dynamics CRM practice. I have a deep appreciation for the Microsoft CRM platform and I am very excited about it. You might even describe me as a Microsoft CRM Advocate. I have many battle scars from my experience with the product and I’m constantly being asked questions about CRM and how-to-do something in it. Hence, this BLOG is to help disseminate that knowledge and information to everyone. As of last year I was posting links to many other blogs to help spread the knowledge, but now with the community.dynamics.com doing that for me, I will be following that practice unless a really juicy article catches my eye. Many people have asked where my post are for the first half of 2010, my company had me posting to another blog and maintain two was near impossible. I am now down to just this blog. So good luck and I hope that this blog may help in some way. If you have suggestions or questions, please email me them.

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012 BillOnCRM