0

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 = '<Query>
           <Where>
              <Eq>
                 <FieldRef Name="ArchiveData" />
                 <Value Type="WorkflowStatus">3</Value>
              </Eq>
           </Where>
        </Query>'

$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()
2

When you use an SPQuery object, the Query property automatically adds the outer < Query>..< /Query> to your CAML. Try building your query without that, like:

$CAML = '<Where>
            <Eq>
               <FieldRef Name="ArchiveData" />
               <Value Type="WorkflowStatus">3</Value>
            </Eq>
         </Where>'

$query = new-object Microsoft.SharePoint.SPQuery
$query.Query = $CAML
  • Worked like charm. Thanks Dylan and everyone else. – Edward Jones May 23 '14 at 15:17
2

When using this to query your list in PowerShell (and also c#) you don't include the tags on the end. So what your looking for would be..

 $CAML = '<Where>
            <Eq>
               <FieldRef Name="ArchiveData" />
               <Value Type="WorkflowStatus">3</Value>
            </Eq>
          </Where>'
  • Beat me by 20 seconds... :) – Dylan Cristy May 23 '14 at 15:13
  • well you did write out the $query=.. part – user13186 May 23 '14 at 15:14
0

Do you know there is a CAML Query Helper, maybe you can use it to test your queries: http://spcamlqueryhelper.codeplex.com/ I think the problem is the value type beeing "Workflowstatus". If I understand you right your query should look like this:

 <Query>
       <Where>
          <Eq>
             <FieldRef Name="WorkflowStatus" />
             <Value Type="Number">3</Value>
          </Eq>
       </Where>
 </Query>
  • I changed the value type to Number but the count is still 124,000. – Edward Jones May 23 '14 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.