如何取消SharePoint(2010)列表中所有正在运行的工作流程?
我通过technet找到了这个脚本。
CODE:
using (SPSite oSite = new SPSite("<your url>"))
{
foreach (SPWeb oWeb in oSite.AllWebs)
{
oWeb.AllowUnsafeUpdates = true;
// stop list workflows
foreach (SPList list in oWeb.Lists)
{
foreach (SPListItem oItem in list.Items)
{
foreach (SPWorkflow workflow in oItem.Workflows)
{
SPWorkflowManager.CancelWorkflow(workflow);
}
}
}
// stop site workflows
foreach (SPWorkflow workflow in oWeb.Workflows)
{
SPWorkflowManager.CancelWorkflow(workflow);
}
oWeb.AllowUnsafeUpdates = false;
oWeb.Dispose();
}
}
非常感谢你的帮助。
答案 0 :(得分:12)
给这个人一个机会。
我对其进行了修改,以便在特定网络上的特定列表中完成。
#Site URL
$web = Get-SPWeb "http://urlforsite.com";
$web.AllowUnsafeUpdates = $true;
#List Name
$list = $web.Lists["ListName"];
# Iterate through all Items in List and all Workflows on Items.
foreach ($item in $list.Items) {
foreach ($wf in $item.Workflows) {
#Cancel Workflows
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
}
}
$web.Dispose();
对我来说工作得很好。让我知道它是否适合你。
答案 1 :(得分:2)
这个脚本对我来说非常有用。我做了一个修改,允许我取消一个列表中的所有工作流程或只取消一个工作流程,并认为我也会在这里发布:
#Parameters
param($listToCancel,$WfToCancel)
#Site URL
$web = Get-SPWeb "http://mydomain.com";
$web.AllowUnsafeUpdates = $true;
#List Name
$list = $web.Lists[$listToCancel];
#Add wildcards to Wf variable
$WildcardWfToCancel = "*"+$WfToCancel+"*";
# Iterate through all Items in List and all Workflows on Items.
foreach ($item in $list.Items) {
foreach ($wf in $item.Workflows) {
#Test for workflow complete and match criteria
if (($wf.ParentAssociation.InternalName -like $WildcardWfToCancel) -and ($wf.IsCompleted -ne $true)) {
#Show status and cancel Workflows
write-Host $wf.ItemName -nonewline;
write-host " " -nonewline;
write-host $wf.ParentAssociation.InternalName;
Write-Host " Status " -nonewline;
Write-host $wf.InternalState;
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
}
}
}
$web.Dispose();
答案 2 :(得分:0)
试试这个(C#到powershell的简单“翻译”):
$site = Get-SPSite "<your url>";
$site.AllWebs | foreach {
$web = $_;
$web.AllowUnsafeUpdates = $true;
# stop list workflows
$web.Lists | foreach {
$list = $_;
$list.Items | foreach {
$item = $_;
$item.Workflows | foreach {
$wf = $_;
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
}
}
}
# stop site workflows
$web.Workflows | foreach {
$wf = $_;
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
}
$web.AllowUnsafeUpdates = $false;
$web.Dispose();
}
$site.Dispose();
答案 3 :(得分:0)
这是我修改过的脚本:
$web = Get-SPWeb "http://site/subsite";
$web.allowUnsafeUpdates = 'true';
# stop list workflows
$web.Lists | foreach {
$list = $_;
$list.Items | foreach {
$item = $_;
$item.Workflows | foreach {
$wf = $_;
[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf);
}
}
}
$web.allowUnsafeUpdates = 'false';
$web.Dispose();
这是我得到的错误(我正在使用服务器和共享点的德语本地化版本):
Fehler bei der Enumeration einer Auflistung: Collection was modified; enumeration operation may not execute..
Bei C:\stopwf.ps1:7 Zeichen:3
+ <<<< $web.Lists | foreach {
+ CategoryInfo : InvalidOperation: (Microsoft.Share...on+SPEnumerator:SPEnumerator) [], RuntimeException
+ FullyQualifiedErrorId : BadEnumeration
用户runnig脚本是自己的机器上的admin,sitecollection admin和sql-admin。我还用这个帐户创建了子网站,所以我不认为使用会成为问题。
我找到了这个答案(Differences Between PowerShell and C# when Enumerating a Collection),但我不知道如何为我使用这些信息?!