一、用EventHandler在列表中建立啟動工作流程欄,先擷取觸發事件的列表,組合啟動的url,其中TemplateId 通過讀相關列表的工作流程(list.WorkflowAssociations)得到,獲得觸發事件的清單項目得到清單項目的ID,將 url賦值給清單項目,更新清單項目。
public class Meeting : SPItemEventReceiver
{
public string startWFString = string.Empty;
public override void ItemAdded(SPItemEventProperties properties) //重載ItemAdded函數,監控建立列表條目事件
{
SPSecurity.RunWithElevatedPrivileges(delegate() //用此方法類比管理員賬戶運行此事件處理常式
{
using (SPSite site = new SPSite(properties.SiteId)) //用此方法的話就不用dispose()了
{
using (SPWeb web = site.OpenWeb(properties.OpenWeb().ID)) //注意獲得web的方法!!!
{
try
{
startWFString = web.Url ;
if (!startWFString.EndsWith("/"))
startWFString = startWFString + "/";
startWFString += "_layouts/IniWrkflIP.aspx?List=";
SPList list = web.Lists[properties.ListId]; //獲得觸發事件的列表
SPWorkflowAssociationCollection wfCollection = list.WorkflowAssociations;
//啟動工作流程的列
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(wfCollection.SoapXml);
System.Xml.XmlNode node = doc.SelectSingleNode("WorkflowTemplates/WorkflowTemplate/WorkflowTemplateIdSet/@TemplateId");
if (list.EnableMinorVersions)
{
list.EnableMinorVersions = false;
}
startWFString += list.ID + "&ID=";
SPListItem item = list.Items.GetItemById(properties.ListItemId); //獲得觸發事件的列表條目
startWFString += properties.ListItemId+ "&TemplateID=";
startWFString += node.InnerText + "&Source=";
string pUrl = web.ParentWeb.Url;
string listUrl = list.DefaultViewUrl;
startWFString += pUrl + listUrl;
item["StartWorkflow"] = startWFString;
item["Title"] = item.File.Name;
item.Update();
string tmp = item["StartWorkflow"].ToString();
if (tmp.Contains(","))
{
string[] arr = tmp.Split(new char[]{','});
item["StartWorkflow"] = arr[0] + ", 啟動工作流程";
item.Update();
}
list.EnableMinorVersions = true;
}
catch (Exception ex)
{
properties.ErrorMessage = ex.Message;
}
}
}
});
}
}
二、 部署EventHandler
利用D:"MOSS"EventHandlerExplorer"EventHandlerExplorer工程部署,點擊EventHandlerExplorer.exe。
U2UCamlCreator.exe可以查詢列表的基本資料。
SPCAMLEditor.exe 修改列表的基本資料。