我想在想要用代码来生成K2 KPRX 文件 , 现在的问题是文件已经生成成功,也能够通过代码发布,但是一旦发起流程 ,K2 就会报错 , 错误信息是:Key could not be found in the collection 。
代码如下 :
想请教下高手怎么解决这个问题, 我现在是不知道哪里写错了 。
enter code here
#region 设置节点审核界面
ClientEvent approvalClientEvent = WorkflowFactory.CreateEvent(node.ApprovalNode_Name, WizardNames.DefaultClientEvent);
approvalClientEvent.EventItem.InternetPlatform = "ASP";
approvalClientEvent.EventItem.SendToInternet = true;
////需要绑定smartobject作为审核界面来源
SmartObjectFieldPart smfp = new SmartObjectFieldPart();
//#region 创建locals
Local localSmart = new Local("Name", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart(Config.SmartObjectName)));
smfp.Properties.Locals.Add("Name", localSmart);
EnvironmentFieldModel environment = new EnvironmentFieldModel("SmartObject Server", "");
ArtifactLibraryFieldPart fieldPart = new ArtifactLibraryFieldPart(environment);
Local localServer = new Local("Server", K2FieldFactory.CreateK2Field(typeof(string), fieldPart));
smfp.Properties.Locals.Add("Server", localServer);
Local localMothed = new Local("MethodDisplayName", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart(Config.FormUrl)));
smfp.Properties.Locals.Add("MethodDisplayName", localMothed);
Local localMothedType = new Local("MethodType", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart("read")));
smfp.Properties.Locals.Add("MethodType", localMothedType);
Local localDisplayName = new Local("DisplayName", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart(Config.SmartObjectName)));
smfp.Properties.Locals.Add("DisplayName", localDisplayName);
//Local localIsList = new Local("IsList", K2FieldFactory.CreateK2Field(typeof(bool), new ValueTypePart("False")));
//smfp.Properties.Locals.Add("IsList", localIsList);
Local localGuid = new Local("Guid", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart(Guid.NewGuid().ToString())));
smfp.Properties.Locals.Add("Guid", localGuid);
Local localMethodName = new Local("MethodName", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart(Config.FormUrl)));
smfp.Properties.Locals.Add("MethodName", localMethodName);
Local localType = new Local("Type", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart("SourceCode.SmartFunctions.Syntax.Expressions.SmartObjectExp")));
smfp.Properties.Locals.Add("Type", localType);
#endregion
#region 创建inputs
Input inputProId = new Input("procId", "procId", "Text", true, K2FieldFactory.CreateK2Field(typeof(string), new DataFieldPart("Process", "RequestID", DataTypes.String, FieldListOption.None)));
smfp.Properties.Inputs.Add("procId", inputProId);
Input inputActivityName = new Input("activityName", "activityName", "Text", true, K2FieldFactory.CreateK2Field(typeof(string), new WorkflowContextFieldPart(WorkflowContextProperty.ActivityInstanceName)));
smfp.Properties.Inputs.Add("activityName", inputActivityName);
#endregion
#region 创建returns
Return returnResult = new Return("result");
returnResult.DisplayName = "result";
returnResult.IsSelected = true;
returnResult.Type = "Text";
smfp.Properties.Returns.Add("result", returnResult);
#endregion
#region 创建properties
Property pResult = new Property();
pResult.Name = "result";
pResult.DisplayName = "result";
pResult.Type = "Text";
smfp.Properties.Properties.Add("result", pResult);
#endregion
#region
SourceCode.Workflow.Authoring.K2Field interPart = new K2Field();
interPart.Parts.Add(smfp);
interPart.Parts.Add(new ValueTypePart("&"));
interPart.Parts.Add(new SerialNoFieldPart());
approvalClientEvent.EventItem.InternetUrl = interPart;
approvalClientEvent.EventItem.InternetUrl = K2FieldFactory.CreateK2Field(
typeof(string),
new ValueTypePart("http://webserver/page.aspx?"),
new SerialNoFieldPart());
activity.Events.Add(approvalClientEvent);
#endregion
#region 设置节点审核人
//如何通过smartobject设置审核人
SimpleDestinationRule destinationRule = new SimpleDestinationRule();
DestinationSet defaultDestinationSet = new DestinationSet();
Destination destination = new Destination();
destination.Type = DestinationTypes.User;
SmartObjectFieldPart smfpDestination = new SmartObjectFieldPart();
//smfpDestination.SmartObjectGuid = smartobject.Guid;
#region 创建locals
Local localSmartDestination = new Local("Name", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart(Config.SmartObjectName)));
smfpDestination.Properties.Locals.Add("Name", localSmartDestination);
EnvironmentFieldModel environmentDestination = new EnvironmentFieldModel("SmartObject Server", "");
ArtifactLibraryFieldPart fieldPartDestination = new ArtifactLibraryFieldPart(environmentDestination);
Local localServerDestination = new Local("Server", K2FieldFactory.CreateK2Field(typeof(string), fieldPartDestination));
smfpDestination.Properties.Locals.Add("Server", localServerDestination);
Local localMothedDestination = new Local("MethodDisplayName", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart(Config.GetApproveUserNames)));
smfpDestination.Properties.Locals.Add("MethodDisplayName", localMothedDestination);
Local localMothedTypeDestination = new Local("MethodType", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart("list")));
smfpDestination.Properties.Locals.Add("MethodType", localMothedTypeDestination);
Local localDisplayNameDestination = new Local("DisplayName", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart(Config.SmartObjectName)));
smfpDestination.Properties.Locals.Add("DisplayName", localDisplayNameDestination);
smfpDestination.Properties.Locals["IsList"].Value = K2FieldFactory.CreateK2Field(typeof(bool), new ValueTypePart("False"));
Local localGuidDestination = new Local("Guid", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart(Guid.NewGuid().ToString())));
smfpDestination.Properties.Locals.Add("Guid", localGuidDestination);
Local localMethodNameDestination = new Local("MethodName", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart(Config.GetApproveUserNames)));
smfpDestination.Properties.Locals.Add("MethodName", localMethodNameDestination);
Local localTypeDestination = new Local("Type", K2FieldFactory.CreateK2Field(typeof(string), new ValueTypePart("SourceCode.SmartFunctions.Syntax.Expressions.SmartObjectExp")));
smfpDestination.Properties.Locals.Add("Type", localTypeDestination);
#endregion
#region 创建inputs
Input inputProIdDestination = new Input("procId", "procId", "Text", true, K2FieldFactory.CreateK2Field(typeof(string), new DataFieldPart("Process", "RequestID", DataTypes.String, FieldListOption.None)));
smfpDestination.Properties.Inputs.Add("procId", inputProIdDestination);
Input inputActivityNameDestination = new Input("activityName", "activityName", "Text", true, K2FieldFactory.CreateK2Field(typeof(string), new WorkflowContextFieldPart(WorkflowContextProperty.ActivityInstanceName)));
smfpDestination.Properties.Inputs.Add("activityName", inputActivityNameDestination);
#endregion
#region 创建returns
Return returnResultDestination = new Return("result");
returnResultDestination.DisplayName = "result";
returnResultDestination.IsSelected = true;
returnResultDestination.Type = "Text";
smfpDestination.Properties.Returns.Add("result", returnResultDestination);
#endregion
#region 创建properties
Property pResultDestination = new Property();
pResultDestination.Name = "result";
pResultDestination.DisplayName = "result";
pResultDestination.Type = "Text";
smfpDestination.Properties.Properties.Add("result", pResultDestination);
#endregion
SourceCode.Workflow.Authoring.K2Field destinationField = new K2Field();
destinationField.Parts.Add(smfpDestination);
destination.Value = destinationField;
defaultDestinationSet.Destinations.Add(destination);
destinationRule.DestinationSets.Add(defaultDestinationSet);
// Set the destination rule of the activity
activity.DestinationRule = destinationRule;
#endregion
#region 设置节点上的动作
var lines = BllRulInfo.Fetch(new CriteriaRulInfo { EqStartActId = node.ID });
foreach (var line in lines)
{
if (!string.IsNullOrEmpty(line.RulAct))
{
EventAction action = WorkflowFactory.CreateK2Object<EventAction>(line.RulAct);
action.Description = line.Id;
action.ActionItem = new DefaultOutcomeAction();
approvalClientEvent.Actions.Add(action);
}
}
#endregion
#region 设置节点上的规则导航
//相同动作不同规则如何设置
activity.SucceedingRule = new DefaultOutcomeSucceedingRule();
PropertyWizardDefinition propWizDefSimple = WorkflowHelpers.FindOfType<SimpleSucceedingRulePropertyWizardDefinition>(activity.WizardDefinition.PropertyWizardDefinitions);
PropertyWizardDefinition propWizDefOutcome = WorkflowHelpers.FindOfType<OutcomeSucceedingRulePropertyWizardDefinition>(activity.WizardDefinition.PropertyWizardDefinitions);
if (propWizDefSimple != null && propWizDefOutcome == null)
{
activity.WizardDefinition.PropertyWizardDefinitions.Remove(propWizDefSimple);
activity.WizardDefinition.PropertyWizardDefinitions.Add(
WorkflowFactory.CreatePropertyWizardDefinition(PropertyWizardNames.OutcomeSucceedingRule));
}
#endregion
#region 设置节点上的动作对应的outcome
//如何生成一个动作对应多个outcome
//SourceCode.Workflow.Design.Outcome.Common.GenerateDefaultOutcomesForActions(approvalClientEvent);
ProcessFactory.GenerateDefaultOutcomesForActions(approvalClientEvent, lines);
#endregion
#region 设置节点上的outcome对应的line
SourceCode.Workflow.Design.Outcome.Common.GenerateDefaultLinesForOutcomes(activity.SucceedingRule as DefaultOutcomeSucceedingRule);
foreach (var line in lines)
{
if (!string.IsNullOrEmpty(line.RulAct))
{
DefaultActivity finishActivity = FindActivity(process, nodeNameDic[line.EndActId]);
Line pl = FindLine(process, node.ApprovalNode_Name, line.RulAct);
if (pl != null)
pl.FinishActivity = finishActivity;
}
}
#endregion