Custom Action for Sharepoint Designer

I'm going to create a custom action for Sharepoint Designer that creates a discussion thread in a discussion list based on the current list item.

I'm going to use the code found here and put it into a SPD custom action.

First create a Workflow Activity Library project in Visual Studio 2005.

 

Rename the Activity1 to MyCustomActivity and add a Code activity.

 

Add the Microsoft.Sharepint.dll and Microsoft.Sharepoint.WorkflowActions.dll assembly reference

Now we're going to create the input properties from where we're going to receive input data from the workflow.

public static DependencyProperty ListIdProperty = DependencyProperty.Register

("ListId", typeof(string), typeof(MyCustomActivity));

 

The ListIdProperty is going to receive the list ID of the list item that we're going to copy to the discussion list (a string representation of the Guid)..

public static DependencyProperty ToListIdProperty = DependencyProperty.Register

("ToListId", typeof(string), typeof(MyCustomActivity));

 

The ToListIdProperty is going to receive the discussion's list ID (a string representation of the Guid).

public static DependencyProperty ListItemProperty = DependencyProperty.Register

("ListItem", typeof(Int32), typeof(MyCustomActivity));

 

This is the list item ID

public static DependencyProperty __ContextProperty = DependencyProperty.Register

("__Context", typeof(WorkflowContext), typeof(MyCustomActivity));

 

And this is a special property that is going to have the workflow context to have access to the current SPWeb.

Then add the public properties

[Description("My description which can be any text I want.")]

[Category("My category which could be anything.")]

[ValidationOption(ValidationOption.Required)]

[Browsable(true)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

public string ListId

{

get {return ((string) (base.GetValue(MyCustomActivity.ListIdProperty)));}

set {base.SetValue(MyCustomActivity.ListIdProperty, value);}

}

 

[Description("My description which can be any text I want.")]

[Category("My category which could be anything.")]

[ValidationOption(ValidationOption.Required)]

[Browsable(true)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

public string ToListId

{

get {return ((string) (base.GetValue(MyCustomActivity.ToListIdProperty)));}

set {base.SetValue(MyCustomActivity.ToListIdProperty, value);}

}

 

[Description("My description which can be any text I want.")]

[Category("My category which could be anything.")]

[ValidationOption(ValidationOption.Required)]

[Browsable(true)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

public Int32 ListItem

{

get {return ((Int32) (base.GetValue(MyCustomActivity.ListItemProperty)));}

set {base.SetValue(MyCustomActivity.ListItemProperty, value);}

}

 

[Description("My description which can be any text I want.")]

[Category("My category which could be anything.")]

[ValidationOption(ValidationOption.Required)]

[Browsable(true)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]

public WorkflowContext __Context

{

get {return ((WorkflowContext)(base.GetValue(MyCustomActivity.__ContextProperty)));}

set {base.SetValue(MyCustomActivity.__ContextProperty, value);}

}

 

Now just add the following code to the Code activity handler

private void _mainCodeActivity_ExecuteCode(object sender, EventArgs e)

{

// get the list

SPList sourceList = __Context.Web.Lists[new Guid(ListId)];

 

// get list item

SPListItem item = __Context.GetListItem(sourceList, ListItem);

 

// get the discussion list

SPList targetList = __Context.Web.Lists[new Guid(ToListId)];

 

// create the discussion item

SPListItem discussionItem = SPUtility.CreateNewDiscussion(targetList.Items, item.Title);

discussionItem.Update();

}

 

Sign the project with a key, compile and put the assembly in GAC.

 

Now you have to define the new action in the WSS.ACTIONS file found in \12\TEMPLATE\1033\Workflow

<Action Name="My Custom Action"

ClassName="SPDCustomActions.MyCustomActivity"

Assembly="SPDCustomActions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a980f97a0f622f27"

AppliesTo="all"

Category="List Actions">

<RuleDesigner Sentence="Create discussion item in %1 to %2.">

<FieldBind Field="ListId,ListItem" Text="this list" Id="1" DesignerType="ChooseListItem" />

<FieldBind Field="ToListId" Text="this list" Id="2" DesignerType="ListNames" />

</RuleDesigner>

<Parameters>

<Parameter Name="ListId" Type="System.String, mscorlib" Direction="In" />

<Parameter Name="ListItem" Type="System.Int32, mscorlib" Direction="In" />

<Parameter Name="ToListId" Type="System.String, mscorlib" Direction="In" />

<Parameter Name="__Context" Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext, Microsoft.SharePoint.WorkflowActions" Direction="In" />

</Parameters>

</Action>

 

Then add the following line to the web.config file of your site collection

<authorizedType Assembly=" SPDCustomActions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a980f97a0f622f27" Namespace=" SPDCustomActions" TypeName="*" Authorized="True" />

 

That's it! Test the action in Sharepoint Designer:

 

Leave a Reply

Your email address will not be published. Required fields are marked *