BizTalk - How to create custom functoid.

Functoid is no more than a piece of .Net code, find my another post BizTalk-Get to know functoid for more detail. The functoids coming with BizTalk do not always satisfy your needs. How to create our own custom functoid? In this article, I want to create a functoid which get current date/date time based on given format.

1. All functoids are derived from BaseFunctoid class which is built in Microsoft.BizTalk.BaseFunctoids.dll, it can be found under <BizTalk Installation Path>\Developer Tools.

2. BizTalk artifacts are required to be deployed in GAC, that’s to say they are strong named assemblies, we need a snk file. You can create a snk file by running sn –k sn.snk visual studio command prompt window.

3. Coding time now! Let’s create a c# class project called FunctoidDemo, include sn.snk in this project and add reference to Microsoft.BizTalk.BaseFunctoids.dll.

4. In this step we add a resource file to FunctoidDemo solution. BaseFunctoid is designed to store some settings in resource file such as functoid name, tooltip, tool description, so all derived functoid class should also have its related resource file.

 

5. The real c# code is pretty simple.

Code
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using Microsoft.BizTalk.BaseFunctoids;
 5using System.Reflection;
 6
 7namespace FunctoidDemo
 8{
 9    public class FormattedDate : BaseFunctoid
10    {
11        public FormattedDate()
12        {
13            this.ID = 9500;
14            //Specify the resource assembly name from which some settings are retrieved.
15            this.SetupResourceAssembly("FunctoidDemo.Properties.Resources", Assembly.GetExecutingAssembly());
16
17            // Get settings from resource by given keys.
18            this.SetTooltip("IDS_FORMATTEDDATEFUNCTOID_TOOLTIP");
19            this.SetBitmap("IDI_FORMATTEDDATE_ICON");
20            this.SetName("IDS_FORMATTEDDATEFUNCTOID_NAME");
21            this.SetDescription("IDS_FORMATTEDDATEFUNCTOID_DESCRIPTION");
22            this.SetMinParams(0);
23            this.SetMaxParams(1);
24
25            // this method call lets the mapper what function to call, when functoid
26            // is deployed to GAC, ConvertCurrencyField is the method we'll include later.
27            this.SetExternalFunctionName(GetType().Assembly.FullName, "FunctoidDemo.FormattedDate""GetCurrentDateTime");
28            base.SetScriptBuffer(ScriptType.CSharp, this.GetCSharpBuffer());
29            // defining category in toolbox, 
30            //this decides under which category the functoid will be added.
31            this.Category = FunctoidCategory.DateTime;
32
33            //define output and input connection types..
34            this.OutputConnectionType = ConnectionType.AllExceptRecord;
35            this.AddInputConnectionType(ConnectionType.AllExceptRecord);
36
37        }

38        //This function is to return current date time with given format.
39        private string GetCSharpBuffer()
40        {
41            StringBuilder builder = new StringBuilder();
42            builder.Append("public string GetCurrentDateTime( string format)\n");
43            builder.Append("{\n");
44            builder.Append("\treturn DateTime.Now.ToString(format);\n");
45            builder.Append("}");
46            return builder.ToString();
47        }

48
49    }

50}

51


6. Register FunctoidDemo.dll go GAC by running GacUtil -I FunctoidDemo.dll.

7. Copy FunctoidDemo.dll to <BizTalk Installation Path>\Developer Tools\Mapper Extensions, If you skip this step, you will get an error like this.

BizTalk - How to create custom functoid._第1张图片 

8. Add this custom functoid to tool box. NOTE: FormattedDate is the class name, it is different from assembly name.

BizTalk - How to create custom functoid._第2张图片 

9. Now add a BizTalk project to current solution for testing purpose.

BizTalk - How to create custom functoid._第3张图片 

I keep the map very simple for easy testing. The selected functoid is my custom functoid, the icon looks a little bit ugly. J I did not dray anything, just a white square, a beautiful icon does not help anything from technical perspective.

BizTalk - How to create custom functoid._第4张图片 

10. Specify the input parameter as yyyyMMdd, I want to use this pattern to format current date time.

BizTalk - How to create custom functoid._第5张图片 

11. Now I test the map with automatically generated sample file, see the output data.

BizTalk - How to create custom functoid._第6张图片 

The checkInTime is formatted as 20080529.

12. Source code download.

I will explain the structure of BaseFunctoid class in detail later.
Hope it helps!

你可能感兴趣的:(BizTalk - How to create custom functoid.)