SharePoint Designer工作流的初始界面,是一个系统默认生成的aspx页面,可以在该页面上设置启动变量,但是该界面太过于简陋,并且不支持某些控件,比如用户控件,如果想在启动的时候指定审核人员,那只有定制该页面,并将用户输入的值传递给流程。
另外,在某些情况下,也需要通过程序自动启动流程,并将初始变量传递给工作流,那该怎样做呢?
工作流启动的方法:
spSite.WorkflowManager.StartWorkflow(SPListItem spListItem, SPWorkflowAssociation associationTemplate, string initiationData, bool thebool);
其中变量initiationData即记录了启动流程需要的初始变量,其格式如下:
<wfiniprops>
<myreviewer>
anetdemo\user01
</myreviewer>
<myapprover>
anetdemo\administrator
</myapprover>
</wfiniprops>;
wfiniprops名字可以任意,myreviewer和myapprover必须和初始化变量名称一致。
示例代码:
using (SPSite site = new SPSite("http://yoursite:port/weburl"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["listName"];
SPListItem item = list.GetItemById(18);
string assciationData = "<wfiniprops>" +
"<myreviewer>" +
"mydomain\\user01" +
"</myreviewer>" +
"<myapprover>" +
"mydomain\\administrator" +
"</myapprover>" +
"</wfiniprops>";
SPWorkflowAssociationCollection objWorkflowAssociationCollection = list.WorkflowAssociations;
Guid wfGuid = getGuidOfWorkflow(list, "工作流 1");
SPWorkflowAssociation wrkFl = list.WorkflowAssociations[wfGuid];
wrkFl.AssociationData = assciationData;
site.WorkflowManager.StartWorkflow(item, wrkFl, wrkFl.AssociationData, true);
}
}
。。。
/// <summary>
/// 通过工作流名称获得其Guid的方法
/// </summary>
/// <param name="splist"></param>
/// <param name="workflowName"></param>
/// <returns></returns>
public static Guid getGuidOfWorkflow(SPList splist, string workflowName)
{
SPWorkflowAssociationCollection workflowsAssociations = splist.WorkflowAssociations;
foreach (SPWorkflowAssociation spwa in workflowsAssociations)
{
if (spwa.Name.Equals(workflowName) && spwa.Enabled)
{
return spwa.Id;
}
}
return new Guid();
}