- Open Visual Studio
- On the File menu, point to New, and then click Project
- Under the Workflow Project Type, select the Workflow Activity Library project template.
- In the Name box, type CorrespondanceEmailActivities(any project name), and then click OK.
- Rename Activity1 to EmailActivity1. (any project name)
- The first thing you must do is change your class declaration to inherit directly from Activity instead of SequenceActivity.
Open your activity in code view, and change SequenceActivity to Activity. Your class definition should look like the following.
- Now you must create some Dependency Properties. Add the following fields inside your class definition.
- public static DependencyProperty ToProperty= DependencyProperty.Register("To", typeof(string), typeof(EmailActivity1));public static DependencyProperty FromProperty= DependencyProperty.Register("From", typeof(string), typeof(EmailActivity1));public static DependencyProperty CCProperty= DependencyProperty.Register("CC", typeof(string), typeof(EmailActivity1));public static DependencyProperty SubjectProperty= DependencyProperty.Register("Subject", typeof(string), typeof(EmailActivity1));public static DependencyProperty BodyProperty= DependencyProperty.Register("Body", typeof(string), typeof(EmailActivity1));public static DependencyProperty AttachmentProperty= DependencyProperty.Register("Attachment", typeof(string), typeof(EmailActivity1));public static DependencyProperty SmtpServerProperty= DependencyProperty.Register("SmtpServer", typeof(string), typeof(EmailActivity1));public static DependencyProperty InvokeEvent= DependencyProperty.Register("Invoke", typeof(EventHandler), typeof(EmailActivity1));
public partial class EmailActivity1: Activity{ … Notice that the last field, InvokeEvent, is of type EventHandler. This enables you to add custom code to your activity when it is used in a workflow.
- Now, you must add properties for your fields. Add the following code under the fields you just added.
- Now, you must add a property for your InvokeEvent field. This EventHandler property lets you add code in a workflow to interact with this activity programmatically. Add this code under the Properties you just added.
- Add a using statement at the top of the class. This enables you to access the ASP.NET Mail namespace for sending your e-mail message. Add the following code.
- The last thing you must do here is override the Execute method. Add the following method inside your class definition.
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [ValidationOption(ValidationOption.Required)] [Browsable(true)] [Description("Enter any e-mail recipients, separated by semicolons")] public string To { get { return ((string)(base.GetValue(EmailActivity1.ToProperty))); } set { base.SetValue(EmailActivity1.ToProperty, value); } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [ValidationOption(ValidationOption.Required)] [Browsable(true)] [Description("Enter the e-mail sender")] public string From { get { return ((string)(base.GetValue(EmailActivity1.FromProperty))); } set { base.SetValue(EmailActivity1.FromProperty, value); } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [ValidationOption(ValidationOption.Optional)] [Browsable(true)] [Description("Enter any carbon copy recipients, separated by semicolons")] public string CC { get { return ((string)(base.GetValue(EmailActivity1.CCProperty))); } set { base.SetValue(EmailActivity1.CCProperty, value); } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [ValidationOption(ValidationOption.Required)] [Browsable(true)] [Description("Enter the e-mail subject")] public string Subject { get { return ((string)(base.GetValue(EmailActivity1.SubjectProperty))); } set { base.SetValue(EmailActivity1.SubjectProperty, value); } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [ValidationOption(ValidationOption.Optional)] [Browsable(true)] [Description("Enter the body text of the e-mail")] public string Body { get { return ((string)(base.GetValue(EmailActivity1.BodyProperty))); } set { base.SetValue(EmailActivity1.BodyProperty, value); } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [ValidationOption(ValidationOption.Optional)] [Browsable(true)] [Description("Enter an attachment file path")] public string Attachment { get { return ((string)(base.GetValue (EmailActivity1.AttachmentProperty))); } set { base.SetValue(EmailActivity1.AttachmentProperty, value); } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [ValidationOption(ValidationOption.Required)] [Browsable(true)] [Description("Enter the Smtp Server for the email")] [DisplayName("Smtp Server")] public string SmtpServer { get { return ((string)(base.GetValue (EmailActivity1.SmtpServerProperty))); } set { base.SetValue(EmailActivity1.SmtpServerProperty, value); } } [DescriptionAttribute("Invoke")] [CategoryAttribute("Invoke Category")] [BrowsableAttribute(true)][DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)] public event EventHandler Invoke { add { base.AddHandler(EmailActivity1.InvokeEvent, value); } remove { base.RemoveHandler(EmailActivity1.InvokeEvent, value); } }using System.Net.Mail; Protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext) { // Raise Invoke Event to execute custom code in the workflow. this.RaiseEvent(EmailActivity1.InvokeEvent, this, EventArgs.Empty); // Set reference to Smtp Server. SmtpClient smtp = new SmtpClient(SmtpServer); // Create mail message. MailMessage msg = new MailMessage(); msg.To.Add(To); msg.From = new MailAddress(From); if (!String.IsNullOrEmpty(CC)) { msg.CC.Add(CC); } if (!String.IsNullOrEmpty(Subject)) { msg.Subject = Subject; } if (!String.IsNullOrEmpty(Body)) { msg.Body = Body; } if (!String.IsNullOrEmpty(Attachment)) { msg.Attachments.Add(new Attachment(Attachment)); } // Send the e-mail. smtp.Send(msg); // Indicate that the activity has completed. return ActivityExecutionStatus.Closed;}Installing a Custom Activity
Before you can use your custom activity, you must install it in the global assembly cache (GAC). For an assembly to be installed in the GAC, you must give it a strong name as shown below.
Below are shown steps which you should follow whenever first time custom activity workflow is created and also whenever any change is done in custom activity workflow application file.
Go to application –make changes
Then click properties of solution file and create strong name as shown-
Click ok and build application
Deploy assembly to GAC
If assembly is CorrespondanceEmailActivities.dll then go to project folder->bin->Debug -> CorrespondanceEmailActivities.dll
Right click on assembly dll file and in properties copy path of it. Like as below-
C:\Project\CorrespondanceEmailActivities\CorrespondanceEmailActivities\bin\Debug
Open Visual studio command prompt and run below commands-
cd C:\Project\CorrespondanceEmailActivities\CorrespondanceEmailActivities\bin\Debug
gacutil /if CorrespondanceEmailActivities.dll
sn -T CorrespondanceEmailActivities.dll
it will generate public key token just copy that key.
<authorizedType Assembly="CorrespondanceEmailActivities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cbf3c65e6ac8e98e" Namespace="CorrespondanceEmailActivities" TypeName="*" Authorized="True" />
. go to ->C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\1033\Workflow
Copy public key token in txt file and save.















No comments:
Post a Comment