Activity in WorkFlow with Sharepoint May 6, 2008
Posted by spunkyvt in Programming, Sharepoint.Tags: .Action, activity, Sharepoint, workflow
4 comments
I am going to show you how to add an activity to a workflow, which will take a sharepoint list item and convert it to a dataset.
I will be using vs 2005 with workflow developer starter kit for windows sharepoint services V3 installed
Start a new project and under workflow pick the Workflow Activity Library
Add the Microsoft.SharePoint.dll
In order to use properties you have to use a special type of property called a DependencyProperty. These are used when you define the workflow parameters.
Add these DependencyProperties to class.
public static DependencyProperty _ContextProperty = DependencyProperty.Register(“_Context”, typeof(WorkflowContext), typeof(Activity1));
public static DependencyProperty ListItemProperty = DependencyProperty.Register(“ListItem”, typeof(int), typeof(Activity1));
public static DependencyProperty FileSavePathProperty = DependencyProperty.Register(“FileSavePath”, typeof(string), typeof(Activity1));
public static DependencyProperty ListIdProperty = DependencyProperty.Register(“ListId”, typeof(string), typeof(Activity1));
[Description("Cross-Site Actions")]
[ValidationOption(ValidationOption.Required)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string FileSavePath
{
get
{
return ((string)(base.GetValue(Activity1.FileSavePathProperty)));
}
set
{
base.SetValue(Activity1.FileSavePathProperty, value);
}
}
[Description("Context")]
[ValidationOption(ValidationOption.Required)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public WorkflowContext _Context
{
get
{
return ((WorkflowContext)(base.GetValue(Activity1._ContextProperty)));
}
set
{
base.SetValue(Activity1._ContextProperty, value);
}
}
[Category("Cross-Site Actions"), Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[ValidationOption(ValidationOption.Required)]
public string ListId
{
get
{
return ((string)
(base.GetValue(Activity1.ListIdProperty )));
}
set
{
base.SetValue(Activity1.ListIdProperty, value);
}
}
[Category("Cross-Site Actions"), Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int ListItem
{
get
{
return ((int)
(base.GetValue(Activity1.ListItemProperty)));
}
set
{
base.SetValue(Activity1.ListItemProperty, value);
}
}
In the Execute method as this
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
try
{
using (SPSite mySite = new SPSite(this._Context.Web.Site.ID))
{
using (SPWeb web = mySite.AllWebs[this._Context.Web.ID])
{
Guid myG = new Guid(this.ListId);
SPList list = web.Lists[myG];
SPListItem li = list.Items[this.ListItem-1];
DataTable dt = new DataTable(“Sharepoint”);
dt.Rows.Add(dt.NewRow());
foreach (SPField var in li.Fields)
{
string colName = var.InternalName;
DataColumn dc = new DataColumn(colName);
dt.Columns.Add(dc);
DataRow dr = dt.Rows[0];
dr[dc] = li[var.InternalName];
}
if (Directory.Exists(this.FileSavePath))
{
string dir;
if (this.FileSavePath.EndsWith(“\\”))
dir = this.FileSavePath;
else
dir = this.FileSavePath + “\\”;
dt.WriteXml(dir + li.UniqueId.ToString()+ “.xml” );
}
else
{
throw new Exception(“Missing Directory”);
}
}
}
}
catch (Exception)
{
throw;
}
return ActivityExecutionStatus.Closed;
}
Now sign the assembly and put the compiled Dll int he GAC
This is at as far a code is concerned. Now to add it to sharepoint.
Next go the web.config file for your sharepoint site.
You add an entry to the <System.Workflow.ComponentModel.WorkflowCompiler> section.
similar to this
<authorizedType Assembly=“ActivityLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8fd4b4c3a190a3c6“ Namespace=“ActivityLibrary1“ TypeName=“*“ Authorized=“True“ />
The publickeytoken comes from when you installed it in the GAC.
Next find the .ACTIONS file in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\1033\Workflow.
Add this xml node to the file between the <Actions></Actions> section. There might already be others there so just put it at the end of the last one. Be sure that publickeytoken matches the one you have in the GAC.
<Action
Name=”Write List item to the folder”
ClassName=”ActivityLibrary1.Activity1″
Assembly=”ActivityLibrary1, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=2d6bcfd54854da8d”
AppliesTo=”all”
Category=”Extras”>
<RuleDesigner Sentence=”Context %1 SavePath %2″>
<FieldBind
Field=”ListId,ListItem”
Text=”this”
Id=”1″
DesignerType=”ChooseDoclibItem” />
<FieldBind
Field=”FileSavePath”
Text=”Path”
Id=”2″
DesignerType=”Text” />
</RuleDesigner>
<Parameters>
<Parameter
Name=”__Context”
Type=”Microsoft.SharePoint.WorkflowActions.WorkflowContext,
Microsoft.SharePoint.WorkflowActions”
Direction=”In”/>
<Parameter
Name=”ListId”
Type=”System.String, mscorlib”
Direction=”In” />
<Parameter
Name=”ListItem”
Type=”System.Int32, mscorlib”
Direction=”In” />
<Parameter
Name=”FileSavePath”
Type=”System.String, mscorlib”
Direction=”In” />
</Parameters>
</Action>
You might need an iisreset here. So at Start-Run just type iisreset.
To make life easy for you, use the Sharepoint Designer.
Point to you sharepoint site and under new add a new Workflow.
Give the workflow a name and pick wich list you want to attach it to and the start options you want for your workflow
Click next
Under actions go to more actions. In the list you should find the “Write List Item to the Folder”.
Click the add
After this click the blue “this” and use current item. Then click the blue “Path” and type in the path to the folder where you will be saving you list items.
Your workflow is now installed and ready to go. Give it a GO!