This documentation is archived and is not being maintained.

Custom Workflow Activity: Date Checker

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

The following sample workflow activity checks to see if the opportunity's estimated closed date is greater than 10 days. If so, an e-mail is sent to the administrator to verify the close date with the owner of the opportunity. Note that this uses dynamic entity rather than strong types as is recommended for workflows and plug-ins.

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



using System;
using System.IO;
using System.Net;
using System.ComponentModel;
using System.ComponentModel.Design;
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 Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Workflow;
using Microsoft.Crm.Workflow.Activities;

namespace CustomWorkflowActivity
   [CrmWorkflowActivity("Date Checker", "Custom")]
   public class DateChecker : SequenceActivity
      protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
         // If the opportunity's "Est. Close Date" is greater than 10 days, send an e-mail to the  
         // administrator so that he or she can verify the close date with the owner of the opportunity.
         DateTime todayPlusTen = DateTime.Now.AddDays(10.0);

            if (DateTime.Compare(this.estCloseDate.UserTime, todayPlusTen) > 0)
            IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
            IWorkflowContext context = contextService.Context;

            ICrmService crmService = context.CreateCrmService();

            // Need system user id for activity party.
            WhoAmIRequest systemUserRequest = new WhoAmIRequest();
            WhoAmIResponse systemUser = (WhoAmIResponse)crmService.Execute(systemUserRequest);

            // Create an activity party for the e-mail.
                DynamicEntity[] parties = new DynamicEntity[1];
                parties[0] = new DynamicEntity();
                parties[0].Name = EntityName.activityparty.ToString();
                PropertyCollection party = new PropertyCollection();
                party.Add(new LookupProperty("partyid", new Lookup(EntityName.systemuser.ToString(), systemUser.UserId)));
                parties[0].Properties = party;
            // Create an e-mail message.
                DynamicEntity emailDE = new DynamicEntity();
                emailDE.Name =;
            // Set e-mail properties.
                PropertyCollection emailProperties = new PropertyCollection();
                emailProperties.Add(new DynamicEntityArrayProperty("to", parties));
                emailProperties.Add(new DynamicEntityArrayProperty("from", parties));
                emailProperties.Add(new StringProperty("subject", "Warning: Close date has been extended more than 10 days."));
                emailProperties.Add(new StringProperty("description", "Verify close date is correct."));
                emailProperties.Add(new CrmBooleanProperty("directioncode", new CrmBoolean(true)));
                emailDE.Properties = emailProperties;
            Guid emailID = crmService.Create(emailDE);

            // Create a SendEmail request.
            SendEmailRequest emailRequest = new SendEmailRequest();
            emailRequest.EmailId = emailID;
            emailRequest.TrackingToken = "";
            emailRequest.IssueSend = true;

            // Send the e-mail message.
            SendEmailResponse emailResponse = (SendEmailResponse)crmService.Execute(emailRequest);
            // Est. Close Date is <= 10 days from today.  No action required.

         return ActivityExecutionStatus.Closed;

      public static DependencyProperty estCloseDateProperty = DependencyProperty.Register("estCloseDate", typeof(CrmDateTime), typeof(DateChecker));

      [CrmInput("The estimated close date")]
      public CrmDateTime estCloseDate
            return (CrmDateTime)base.GetValue(estCloseDateProperty);
            base.SetValue(estCloseDateProperty, value);

See Also


© 2010 Microsoft Corporation. All rights reserved.