How to Query a list with CAML via PowerShell

Blog Post created by aaron.labiosa@nintex.com Champion on Nov 26, 2014

Products: Nintex Workflow 2013, Nintex Workflow 2010


Sometimes it can be helpful to replicate the behavior of the Nintex Workflow Query List action outside of the Nintex Workflow environment. After UI is stripped away, Nintex Workflow executes a CAML query using the SharePoint API to return the values specified in the Query List action.


Using this PowerShell script you can replicate this behavior to help narrow down issues with your query (such as List View Threshold violations, Permissions issues, etc.).


To replicate the Query List action in PowerShell do the following:


  • Open the Query List action in your workflow.


  • Change the Editor mode to be CAML editor.


  • Copy the CAML/XML between the <Query> tags.
  • Paste the CAML/XML into the script replacing the example.



Note: The <Query> tags must be removed for the query to function correctly.


Run the below (attached as well as a *.txt file for your convenience) PowerShell script (PowerShell ISE works well) from a SharePoint Server:



PowerShell Script
  1. Add-PSSnapin Microsoft.SharePoint.Powershell
  2. $QueryObject = New-Object Microsoft.SharePoint.SPQuery
  3. $QueryObject.Query = '
  4. <Lists>
  5.    <List ID="{00000000-0000-0000-0000-000000000000}" />
  6. </Lists>
  7. <ViewFields>
  8.    <FieldRef Name="Title" />
  9. </ViewFields>
  10. <Where>
  11.    <Gt>
  12.      <FieldRef Name="ID"  />
  13.      <Value Type="Counter">1</Value>
  14.    </Gt>
  15. </Where>
  16. '
  17. $($(Get-SPWeb http://contoso.com).Lists[[Guid]'{00000000-0000-0000-0000-000000000000}']).GetItems($QueryObject)


To use the script do the following:

  • Replace http://contoso.com with the URL of the site you wish to execute the script against.
  • Replace the Guid in the last line of the script with the Guid found in the CAML query extracted from Nintex.