我正在sharepoint 2013中创建listItem的工作流任务。当我添加列表项时,工作流正常工作,创建任务并且没有错误。但是,当我更新项目时,工作流程代码会给我这个错误:
<nativehr>0x8102009b</nativehr><nativestack></nativestack>
栈跟踪
at HBLEventReceiverWP.ApprovalWFRecevier.ApprovalWFRecevier.LookupAndStartWorkflow(SPListItem listItem, SPWorkflowManager manager, SPWorkflowAssociationCollection associationCollection, String workflowId)
at HBLEventReceiverWP.ApprovalWFRecevier.ApprovalWFRecevier.ItemUpdated(SPItemEventProperties properties)
at Microsoft.SharePoint.SPEventManager.RunItemEventReceiver(SPItemEventReceiver receiver, SPUserCodeInfo userCodeInfo, SPItemEventProperties properties, SPEventContext context, String receiverData)
at Microsoft.SharePoint.SPEventManager.RunItemEventReceiverHelper(Object receiver, SPUserCodeInfo userCodeInfo, Object properties, SPEventContext context, String receiverData)
at Microsoft.SharePoint.SPEventManager.<>c__DisplayClassa`1.<InvokeEventReceiver>b__7()
at Microsoft.SharePoint.SPSecurity.RunAsUser(SPUserToken userToken, Boolean bResetContext, WaitCallback code, Object param)
这是itemupdated
活动的代码。
public class ApprovalWFRecevier : SPItemEventReceiver
{
/// <summary>
/// An item was updated.
/// </summary>
public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
// get site collection web
using (SPWeb spWeb = properties.OpenWeb())
{
// get current site
SPSite spSite = spWeb.Site;
//
spWeb.AllowUnsafeUpdates = true;
// check if it is a component list
bool isCompLst = CheckIsComponentList(properties.List.ToString());
if (isCompLst == true)
{
// set the list item
SPListItem listItem = properties.ListItem;
// check if item is in preview mode
if (properties.List.ToString() == "SitePages")
{
if (Convert.ToBoolean(listItem["Preview"]) == false)
{
SPWorkflowManager manager = spSite.WorkflowManager;
//get item's parent list
SPList parentList = listItem.ParentList;
//get all workflows that are associated with the list
SPWorkflowAssociationCollection associationCollection = parentList.WorkflowAssociations;
//lookup and start the worflow
LookupAndStartWorkflow(listItem, manager, associationCollection, "Approval");
}
}
else if (CheckPreviewCol(listItem, properties) == false)
{
//obtain an instance of SPWorkflowManager
//which will be later used to start the workflow
SPWorkflowManager manager = spSite.WorkflowManager;
//get item's parent list
SPList parentList = listItem.ParentList;
//get all workflows that are associated with the list
SPWorkflowAssociationCollection associationCollection = parentList.WorkflowAssociations;
//lookup and start the worflow
LookupAndStartWorkflow(listItem, manager, associationCollection, "Approval");
}
}
}
}
#region Methods
public bool CheckIsComponentList(string listName)
{
try
{
bool IsCompLst = false;
switch (listName)
{
case "HeadingComponent":
// set component list to true
IsCompLst = true;
break;
case "ParagraphComponent":
// set component list to true
IsCompLst = true;
break;
case "FAQComponent":
// set component list to true
IsCompLst = true;
break;
case "FAQComponentDetail":
// set component list to true
IsCompLst = true;
break;
case "BulletComponent":
// set component list to true
IsCompLst = true;
break;
case "BulletComponentDetail":
// set component list to true
IsCompLst = true;
break;
case "ButtonsComponent":
// set component list to true
IsCompLst = true;
break;
case "TwoImageComponent":
// set component list to true
IsCompLst = true;
break;
case "ImageGridComponent":
// set component list to true
IsCompLst = true;
break;
case "ImageWithDescriptionComponent":
// set component list to true
IsCompLst = true;
break;
case "GreenTextComponent":
// set component list to true
IsCompLst = true;
break;
case "TableComponent":
// set component list to true
IsCompLst = true;
break;
case "TableComponentRows":
// set component list to true
IsCompLst = true;
break;
case "CenterComponent":
// set component list to true
IsCompLst = true;
break;
case "CenterComponentDetail":
// set component list to true
IsCompLst = true;
break;
case "SitePages":
// set component list to true
IsCompLst = true;
break;
default:
// set component list to true
IsCompLst = false;
break;
}
return IsCompLst;
}
catch (Exception ex)
{
throw ex;
}
}
public bool CheckPreviewCol(SPListItem listItem, SPItemEventProperties properties)
{
try
{
bool isPreview = true;
bool oldValue = listItem["Preview"] != null ? Convert.ToBoolean(listItem["Preview"]) : true;
if (oldValue == true)
{
bool newValue = properties.AfterProperties["Preview"] != null ? Convert.ToBoolean(properties.AfterProperties["Preview"]) : true;
if (oldValue != newValue)
isPreview = false;
}
else
isPreview = false;
return isPreview;
}
catch (Exception ex)
{
throw ex;
}
}
private static void LookupAndStartWorkflow(SPListItem listItem, SPWorkflowManager manager, SPWorkflowAssociationCollection associationCollection, string workflowId)
{
try
{
//iterate workflow associations and lookup the workflow to be started
foreach (SPWorkflowAssociation association in associationCollection)
{
//if the workflow association matches the workflow we are looking for,
//get its association data and start the workflow
if (association.Name.ToLower().Equals(workflowId.ToLower()))
{
//get workflow association data
string data = association.AssociationData;
//start workflow with privileges
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWorkflow wf = manager.StartWorkflow(listItem, association, data, true);
});
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
答案 0 :(得分:0)
此错误可能意味着此特定项目已存在活动工作流程。您的案例中的解决方案是检查工作流程是否已完成。那将是这样的:
if(workflow.IsCompleted)
{
//you can start it
//this is pseudocode do not rely on it
}
有关详细信息,请查看this source。