解决自定义代码启动Approver - Sharepoint 2010 Workflow,出现Failed on Start
Posted on 2012-06-07 22:52 SkyPurple 阅读(745) 评论(1) 编辑 收藏 举报将一个webservice项目add到 sharepoint webapplication中,通过外部程序调用这个webservice进行简要的sharepoint常规操作。
在Site collection future中启用workflow. 使用sharepoint 自带的两个workflow.
1.feedback workflow
2. Approver - Sharepoint 2010 workflow
代码片段:
var replaceSpace = new Regex(@"\s{1,}", RegexOptions.IgnoreCase);
var list = common.GetList(rootweb, listName);//rootweb.Lists.TryGetList(listName);
if (null == list)
{
workflowResultEnum = WorkflowResultEnum.HaveNotWorkflow;
return;
}
var currentUserName = replaceSpace.Replace(userprofile.DisplayName, ".");
currentUserName = currentUserName + "_Approver-Sharepoint2010";
var currentUserWorkflowAssociation = list.WorkflowAssociations.GetAssociationByName(currentUserName,
System.Globalization
.CultureInfo.
CurrentCulture);
if (null == currentUserWorkflowAssociation)
{
var rootWebWorkflowTemplateCollection = rootweb.WorkflowTemplates;
var approverTemplate = rootWebWorkflowTemplateCollection.GetTemplateByName(approverWorkflowName,
System.Globalization.CultureInfo.CurrentCulture);
if (null == approverTemplate)
{
workflowResultEnum = WorkflowResultEnum.HaveNotWorkflow;
return;
}
currentUserWorkflowAssociation = SPWorkflowAssociation.CreateListAssociation(approverTemplate,
currentUserName,
tasksList,
workflowHistory);
list.WorkflowAssociations.Add(currentUserWorkflowAssociation);
currentUserWorkflowAssociation.Enabled = true;
list.Update();
}
var buildApproverWorkflowXml = new BuildApproverWorkflowXml(currentUserWorkflowAssociation.AssociationData);
var personaSharepoints = new List<PersonaSharepoint>();
foreach (var spUserId in selectUsers)
{
var spUser = rootSiteUsers.GetByID(spUserId);
if (null == spUser) continue;
var personaSharepoint = new PersonaSharepoint
{
DisplayName = spUser.Name,
AccountId = spUser.LoginName,
AccountType = "User"
};
personaSharepoints.Add(personaSharepoint);
}
currentUserWorkflowAssociation.AssociationData = buildApproverWorkflowXml.Build(personaSharepoints, requestText, customerDateTime);
var item = list.GetItemByUniqueId(itemUniqueId);
if (null == item)
{
workflowResultEnum = WorkflowResultEnum.HaveNotWorkflow;
return;
}
item.Web.Site.WorkflowManager.StartWorkflow(item, currentUserWorkflowAssociation,
currentUserWorkflowAssociation.AssociationData,true);
rootweb.AllowUnsafeUpdates = false;
其中SP代码做的操作是获取workflowAssociation,未找到便根据相应模版创建workflowAssociation.以及创建和检查task List和workflowhistory List;
该workflow的associationdata的XML格式为:
View Code
<dfs:queryFields></dfs:queryFields>
<dfs:dataFields>
<d:SharePointListItem_RW>
<d:Approvers>
<d:Assignment>
<d:Assignee />
<d:Stage xsi:nil="true" />
<d:AssignmentType>Serial</d:AssignmentType>
</d:Assignment>
</d:Approvers>
<d:ExpandGroups>true</d:ExpandGroups>
<d:NotificationMessage />
<d:DueDateforAllTasks xsi:nil="true" />
<d:DurationforSerialTasks xsi:nil="true" />
<d:DurationUnits>Day</d:DurationUnits>
<d:CC />
<d:CancelonRejection>false</d:CancelonRejection>
<d:CancelonChange>false</d:CancelonChange>
<d:EnableContentApproval>false</d:EnableContentApproval>
</d:SharePointListItem_RW>
</dfs:dataFields>
将该workflow绑定在一个listitem中,并启动。启动方式为自动启动,当调用webservice后未发现异常信息,但是在sharepoint界面发现启动失败。
查看LOGS日志后发现该日志信息:
搜索之后发现在微软网站有如下解释:
大致意思是有问题的话是System account替你Cancelled的。接下来,无论我提高任何权限都无法调用自己的webservice启动workflow.
后来在这个帖子下发现有回复说需要在config文件中增加sectiongroup节点;