0
votes

I MODIFIED THE CAML AND IT'S NOW WORKING AND DOES NOT LOOP THROUGH ALL THE ITEMS IN A LIST.

I am looking for list items where workflow status equal "Error Occurred". I have the following code and it's working fine. However, there are 124,000 items in my list and the code goes through each item in the list. Is there a way to limit this to items where workflow status (workflow name is "Archive Data") is "Error Occurred". Please suggest. I added following CAML and changed the code but it still returns all the records. I tested the CAML with u2u and it returns 144 records. I added write-host to get a count and commented out the foreach for test.

    # Terminates all workflow in a give list
    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

    $web = Get-SPWeb "http://inside.nov.com/sales/SouthEast"
    $web.AllowUnsafeUpdates = $true
    # $list name is list display name
    $list = $web.Lists["New Orders"]
    $CAML = '<Where>
              <Eq>
                 <FieldRef Name="ArchiveData" />
                 <Value Type="WorkflowStatus">3</Value>
              </Eq>
           </Where>'

$query = new-object Microsoft.SharePoint.SPQuery
$query.Query = $CAML
$listitems = $list.GetItems($query)
Write-Host "Count: " $listitems.Count

    #foreach ($item in $listItems) 
    #{
    #   foreach ($wf in $item.Workflows)
    #   {
    #       #Cancel Workflows        
    #       [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf)   
    #   }
    #}
    $web.Dispose()
1
You can't loop through item with a specific attribute until you know that attribute... Well, I guess. Therefore I see no way to do that, except maybe having a secondary list where you save your element's ID or something.Kilazur

1 Answers

0
votes

I updated the original code and it's working. The keyword "Query" dont need to be in CAML when using SPQuery object. The SPQuery automatically wraps the CAML with keyword.

Thanks