cancel
Showing results for 
Search instead for 
Did you mean: 

How to Query a list with CAML via PowerShell

Workflow Hero
8 6 9,376

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.

        CAMLQuery.png

  • Change the Editor mode to be CAML editor.

        CAMLQuery2.png

  • 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.
6 Comments
Workflow Hero

I'm trying to query a list in another site using CAML query, however I'm not getting a result if I'm inserting references. while on the other hand when I enter the value manually it gives the result. anyone having this issue?

Workflow Hero

I haven't had this issue so far. Have you checked the content of the references that you are inserting (by using log in history list or by sending you an email with thoses references)?

It's maybe better to create a new discussion with this problem.

Workflow Hero

yes, I have checked. The references values are there and correct. I run the same caml query with value entered manually and it works fine. This is my CAML query:

<Query>

  <Lists>

    <List ID="{9D234801-4B45-4283-9F7C-6348BE9E2D55}" />

  </Lists>

  <ViewFields>

    <FieldRef Name="LetterRefNo" />

  </ViewFields>

  <Where>

    <Eq>

      <FieldRef Name="ID"  />

      <Value Type="Counter">{WorkflowVariable:test}</Value>

    </Eq>

  </Where>

</Query>

I'm trying to retrieve the LetterRefNo where ID is equal to {WorkflowVariable:test}

Workflow Hero

What is the type of the workflow variable "test" ?

Workflow Hero

It's a single text.

Workflow Hero

I've done this several times and it has always worked. Is it in O365 or in an OnPremise environment ?