locked
Custom workflow action always gots error RRS feed

  • Question

  • Hi all,

    I hope to make a custom action for workflow and could be used in SPD 2010. This action takes 2 paramter, one is string another is date to query the calendar and return a string from an event title. The code is like this:

    Imports System.Collections
    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.UserCode
    Imports Microsoft.SharePoint.Workflow
    
    Namespace MyWorkflow.Actions
        Public Class CalendarQuery
            Public Function TestFunction(Context As SPUserCodeWorkflowContext, CalendarName As String, EventDate As Date) As Hashtable
                Dim htResult As New Hashtable(1)
                htResult.Add("Result", "Test")
                Return htResult
            End Function
        End Class
    End Namespace
             

    Then I made an Element file in the VS project as this

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <WorkflowActions>
        <Action Name="Return value test"
                SandboxedFunction="true" 
                Assembly="$SharePoint.Project.AssemblyFullName$"
                ClassName="MyWorkflow.Actions.CalendarQuery"
                FunctionName="TestFunction"
                AppliesTo="all"
                UsesCurrentItem="true"
                Category="Test Actions">
          <RuleDesigner Sentence="From calendar [%2] on date [%1] get OT rate and put into [%3]">
            <FieldBind Id="1" Field="EventDate" DesignerType="ParameterNames" Text="OT Date"></FieldBind>
            <FieldBind Id="2" Field="CalendarName" DesignerType="ParameterNames" Text="Calendar Name"></FieldBind>
            <FieldBind Id="3" Field="Result" DesignerType="ParameterNames" Text="Action Result"></FieldBind>
          </RuleDesigner>
          <Parameters>
            <Parameter Name="__Context" Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext, Microsoft.SharePoint.WorkflowActions" Direction="In" DesignerType="Hide"/>
            <Parameter Name="EventDate" Type="System.DateTime, mscorlib" Direction="In" />
            <Parameter Name="CalendarName" Type="System.String, mscorlib" Direction="In" />
            <Parameter Name="Result" Type="System.String, mscorlib" Direction="Out" DesignerType="ParameterNames"/>
          </Parameters>
        </Action>        
      </WorkflowActions>
    </Elements>
    

    The compile and deploy are succesfully finished. And the action is visible in SPD when making a list workflow. But the workflow always get error in the history, never complete.

    My main reference are:

    http://msdn.microsoft.com/en-us/library/ff798499.aspx

    http://msdn.microsoft.com/en-us/library/gg615449.aspx

    But these two articles have some conflict such as whether the function should be declared as 'Shared', which mean 'static' in c#.

    Does any one has the idea about how to modify the code and the element file to make it work?


    Alex Du

    Sunday, June 10, 2012 11:06 AM

Answers

  • Generally, this problem is caused by the compiler difference between C# and VB.NET.

    The solution is: add a Shared Sub New for the VB class.

    Reason: a C# class contains a static method will be mark a flag 'beforefieldinit' by the compiler, and visible in ILDasm. But VB.NET compiler doesn't do this. So the runtime try to locate and invode the static constructor, Shared Sub New in VB.NET, at first before invoke the function declared in Element.xml and got the error like "Activation did not throw an exception but it returned null".

    I spent 4 days on this issue, and finally solved.


    Alex Du

    • Marked as answer by Alex Du Thursday, June 14, 2012 10:19 AM
    Thursday, June 14, 2012 10:19 AM