以前做练习的时候整理的,发上来供需要的朋友参考。
首先打开visio Studio 2005新建一项目,如下:
(解决方案我后来改为了MyTest.HandleContextObj,
程序集Accembly也是MyTest.HandleContextObj),而不是和类同名,原因是保存工作流时找不到类,不知何故
修改workflow1.cs为GetContextObjInfo.cs
从工具箱里面拖一个code控件过来
给codeActivity的ExecuteCode属性添加一个方法GetContextObjExe
加入我们需要把上下文的一些信息写到一个文件里面去,现在先添加写文件的代码:
GetContextObjInfo.cs完成代码如下:
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.IO;
namespace MyTest.HandleContextObj
{
public partial class GetContextObjInfo: SequenceActivity
{
public GetContextObjInfo()
{
InitializeComponent();
}
private void GetContextObjExe(object sender, EventArgs e)
{
//Write info to a file
string path = @"c:\contextObjInfo.txt";
string text = DateTime.Now.ToString();
StreamWriter writer = new StreamWriter(path);
writer.Write(text);
writer.Close();
}
}
}
添加引用
添加三个变量,这三个变量的名称是固定的。
名称 | 类型 |
__Context | WorkflowContext |
__ListId | string |
__ListItem | int |
修改MyProperty为:__Context string为:WorkflowContext
其他两个属性类似
在private void GetContextObjExe(object sender, EventArgs e)中添加如下内容:
SPWeb contextWeb = __Context.Web;
SPList contextList = contextWeb.Lists[new Guid(__ListId)];
SPListItem contextItem = contextList.GetItemById(__ListItem);
SPFile contextFile = contextItem.File;
并修改string text = DateTime.Now.ToString()为
string text = DateTime.Now.ToString() + " DocName: " + "docName:" + contextFile.Name + " creator: " + contextFile.Author;已反映我们获取的上下文信息到文件中。
GetContextObjInfo.cs完成代码如下:
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.IO;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SharePoint;
namespace MyTest.HandleContextObj
{
public partial class GetContextObjInfo: SequenceActivity
{
public GetContextObjInfo()
{
InitializeComponent();
}
public static DependencyProperty __ContextProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(GetContextObjInfo));
[Description("This is the description which appears in the Property Browser")]
[Category("This is the category which will be displayed in the Property Browser")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public WorkflowContext __Context
{
get
{
return ((WorkflowContext)(base.GetValue(GetContextObjInfo.__ContextProperty)));
}
set
{
base.SetValue(GetContextObjInfo.__ContextProperty, value);
}
}
public static DependencyProperty __ListIdProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__ListId", typeof(string), typeof(GetContextObjInfo));
[Description("This is the description which appears in the Property Browser")]
[Category("This is the category which will be displayed in the Property Browser")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string __ListId
{
get
{
return ((string)(base.GetValue(GetContextObjInfo.__ListIdProperty)));
}
set
{
base.SetValue(GetContextObjInfo.__ListIdProperty, value);
}
}
public static DependencyProperty __ListItemProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__ListItem", typeof(int), typeof(GetContextObjInfo));
[Description("This is the description which appears in the Property Browser")]
[Category("This is the category which will be displayed in the Property Browser")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public int __ListItem
{
get
{
return ((int)(base.GetValue(GetContextObjInfo.__ListItemProperty)));
}
set
{
base.SetValue(GetContextObjInfo.__ListItemProperty, value);
}
}
private void GetContextObjExe(object sender, EventArgs e)
{
SPWeb contextWeb = __Context.Web;
SPList contextList = contextWeb.Lists[new Guid(__ListId)];
SPListItem contextItem = contextList.GetItemById(__ListItem);
SPFile contextFile = contextItem.File;
//Write info to a file
string path = @"c:\contextObjInfo.txt";
string text = DateTime.Now.ToString() + " DocName: " + "docName:" + contextFile.Name + " creator: " + contextFile.Author;
StreamWriter writer = new StreamWriter(path);
writer.Write(text);
writer.Close();
}
}
}
添加强签名
“生成”菜单里面选择“生成GetContextObjInfo”,如果出现问题一般是没有添加引用,添加即可
用reflactor察看公匙,并记下
到目录
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\2052\Workflow
下面,添加Action文件
GetContextObjInfo.ACTIONS内容如下:(注意公匙要和Dll一致,蓝色部分很重要)
<?xml version="1.0" encoding="utf-8"?>
<WorkflowInfo Language="zh-cn">
<Actions>
<Action Name="GetContextObjectInfomation"
ClassName=" MyTest.HandleContextObj.GetContextObjInfo"
Assembly="MyTest.HandleContextObj, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2305ddc6d88a7e80"
AppliesTo="all"
Category="ContextRelated">
<RuleDesigner Sentence="This Activity will Write context infomation to the file">
</RuleDesigner>
<Parameters>
<Parameter Name="__Context" Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext, Microsoft.SharePoint.WorkflowActions" Direction="In"/>
<Parameter Name="__ListId" Type="System.String, mscorlib, mscorlib" Direction="In" />
<Parameter Name="__ListItem" Type="System.Int32, mscorlib, mscorlib" Direction="In" />
</Parameters>
</Action>
</Actions>
</WorkflowInfo>
拷贝GetContextObjInfo.dll到目录:C:\Inetpub\wwwroot\wss\VirtualDirectories\8000\_app_bin
不要忘了修改web.config文件,添加一个新的authorizedType,否则在创建工作流Activity的时候,虽然能找到我们开发的类,但是取加不上去,也不报错。
<System.Workflow.ComponentModel.WorkflowCompiler>
<authorizedTypes>
……
<!--Add by shyang for getContext: new activity-->
<authorizedType Assembly="MyTest.HandleContextObj, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2305ddc6d88a7e80" Namespace="MyTest.HandleContextObj " TypeName="*" Authorized="True" />
<!--End Add by shyang for getContext: new activity-->
….
</authorizedTypes>
</System.Workflow.ComponentModel.WorkflowCompiler>
重启IIS
C:\Documents and Settings\Administrator>iisreset
正在尝试停止...
Internet 服务已成功停止
正在尝试启动...
Internet 服务已成功重新启动
打开Sharepoint Designer,如图可以在工具栏上直接进入该站点:
新建工作流:
完成,保存工作流退出
登陆sharepoint
在文档库中,新建一文档,选择走上述流程,完成后,在C盘目录下会看到文件:contextObjInfo.txt
打开内容如下: