How to: Localize Dependency Properties of Workflow Activities in SharePoint 2010

Summary: Learn how to localize dependency properties of a workflow activity in SharePoint 2010 by using language resource files.

Applies to: Business Connectivity Services | Open XML | SharePoint Designer 2010 | SharePoint Foundation 2010 | SharePoint Online | SharePoint Server 2010 | Visual Studio

Provided by:  Aravind Cheziyan, Microsoft Corporation

Contents

  • Localizing the Dependency Properties of a Workflow

  • Creating a List-Level Workflow Custom Activity Project

  • Creating the Resources File

  • Adding a Custom Workflow Activity Class

  • Adding the Custom Activity to the List Workflow

  • Testing the Site Workflow Custom Activity

  • Conclusion

Localizing the Dependency Properties of a Workflow

The procedures in this article demonstrate how to localize dependency properties of a workflow activity in SharePoint 2010 by using language resource files. The custom activity sends an email message with localized subject text that is based on the current website locale.

This article demonstrates the following tasks:

  1. Creating a list-level workflow

  2. Creating a custom workflow activity

  3. Creating a resources file (.resx)

  4. Sending an email message with localized subject text

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio version that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

Creating a List-Level Workflow Custom Activity Project

First, create a project to hold and test the custom workflow activity.

To create the workflow project

  1. Start Visual Studio 2010.

  2. Create a project by clicking the File menu, pointing to New, and then clicking Project.

  3. In the Visual C# node, click 2010, click SharePoint, and then click Sequential Workflow.

  4. In the Name box, type SendMail, and then click OK. The SharePoint Customization Wizard starts.

  5. On the What is the local site you want to use for debugging? page, click Next to accept the default site. This step also sets the trust level for the solution as farm solution, the only available option for workflow projects.

  6. In the Specify the workflow name for debugging page, accept the default name SendMail - Workflow1. Keep the workflow template type as List Workflow, and then click Next.

  7. Click Finish to accept the remaining default settings.

Creating the Resources File

Next, create a folder mapped to the SharePoint Resources folder, and add a global resources file to add localized text to the project.

To create the resources file

  1. On the Project menu, click Add, and then click SharePoint Mapped Folder to open the Add SharePoint Mapped Folder dialog box.

  2. In the installed SharePoint Mapped Folder tree view, choose Resources, and then click OK. Visual Studio creates a Resources folder.

  3. Right-click the Resources folder, and then click Add New Item.

  4. In the Add New Item dialog box, in the Installed Templates tree view, click the General node, and then click Resources File in the list of project item templates. Use the default name Resources1.resx.

  5. Open the Resources1.resx file, and add the Name as MailSubject and Value as Welcome to Localization. Save the file.

Adding a Custom Workflow Activity Class

Next, add a class to the project to contain the code for the custom workflow activity.

To add a workflow activity class

  1. On the Project menu, click Add New Item.

  2. In the Add New Item dialog box, in the Installed Templates tree view, click the Code node, and then click Class in the list of project item templates. Name the class SendMail.

  3. Add the following namespaces to the SendMail class.

    using System;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Text;
    using System.Workflow.ComponentModel;
    using System.Workflow.ComponentModel.Compiler;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    
  4. Replace all the code in the SendMail class with the following code.

    public class SendMail : System.Workflow.ComponentModel.Activity
        {
            public SendMail()
            {
    
            }
    
            public static DependencyProperty ToMailAddressProperty = DependencyProperty.Register("ToMailAddress", typeof(string), typeof(SendMail), new PropertyMetadata(""));
            [Description("To mail address")]
            [BrowsableAttribute(true)]
            [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
            [ValidationOption(ValidationOption.Optional)]
            public string ToMailAddress
            {
                get { return ((string)(base.GetValue(SendMail.ToMailAddressProperty))); }
                set { base.SetValue(SendMail.ToMailAddressProperty, value); }
            }
    
            public static DependencyProperty SiteUrlProperty = DependencyProperty.Register("SiteUrl", typeof(string), typeof(SendMail), new PropertyMetadata(""));
            [Description("URL of the site")]
            [BrowsableAttribute(true)]
            [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
            [ValidationOption(ValidationOption.Optional)]
            public string SiteUrl
            {
                get { return ((string)(base.GetValue(SendMail.SiteUrlProperty))); }
                set { base.SetValue(SendMail.SiteUrlProperty, value); }
            }
    
            // Triggers when the activity is executed.
            protected override System.Workflow.ComponentModel.ActivityExecutionStatus Execute(System.Workflow.ComponentModel.ActivityExecutionContext executionContext)
            {
                try
                {
                          SPSite site = new SPSite(SiteUrl);
                          SPWeb web = site.OpenWeb("/");
    
                          StringBuilder strB = new StringBuilder();
                          strB.Append("<html><body><center><table border='1' cellpadding='0' cellspacing='0'>");
                          strB.Append("<tr>");
                          strB.Append("<td align='center' valign='middle'>Test Mail for Localization</td>");
                          strB.Append("</tr>");
                          strB.Append("</table></center></body></html>");
    
                          StringDictionary headers = new StringDictionary();
                          headers.Add("to", ToMailAddress);
                          headers.Add("subject", SPUtility.GetLocalizedString("$Resources:Resource1,MailSubject;", "Resource1", web.Language));
                          SPUtility.SendEmail(web, headers, strB.ToString());
                  }
    
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Error: " + ex.ToString());
                }
    
                return base.Execute(executionContext);
            }
    
  5. Save the project. On the Build menu, click Build Solution. SendMail appears as a custom action in the Toolbox under the SharePoint Workflow tab.

Adding the Custom Activity to the List Workflow

Next, add an activity to the workflow to contain the custom code.

To add an activity to the workflow

  1. Open SendMail in the Workflow Designer in design view.

  2. Drag SendMail from the Toolbox, and drop it under the onWorkflowActivated1 activity.

  3. Set the SiteUrl property and ToMailAddress property of the SendMail activity to appropriate values. Save the project.

Testing the Site Workflow Custom Activity

Next, run the project and add a new item to the Announcements list. The custom activity sends an email message to the ToMailAddess address with the localized subject line.

To test the project

  1. Press F5 to run the project and deploy it to SharePoint.

  2. On the Quick Launch bar, click Lists to display all the lists available in the SharePoint site. Navigate to the list for announcements named Announcements. Add a new item to the list.

    This triggers the SendMail – Workflow 1 activity, and an email message is sent to the recipient email address with the localized subject line.

Conclusion

Language resource files make it easy to localize the dependency properties of custom workflow activities. In this article, you saw how to do this by creating a workflow that sends an email message with a localized subject line. You should continue to experiment with this functionality to see how you can use it in your own applications.

See Also

Concepts

Building Effective Workflows Using SharePoint Server 2010

Workflow Technical Articles for SharePoint 2010

Other Resources

Workflow Resource Center | SharePoint 2010

Best Practices for Creating Custom Activities in SharePoint 2010