How to Query a list with CAML via PowerShell

  • 26 November 2014
  • 6 replies
  • 88 views

Userlevel 7
Badge +10

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.  
  3. $QueryObject = New-Object Microsoft.SharePoint.SPQuery
  4.  
  5. $QueryObject.Query = '
  6. <Lists>
  7.    <List ID="{00000000-0000-0000-0000-000000000000}" />
  8. </Lists>
  9. <ViewFields>
  10.    <FieldRef Name="Title" />
  11. </ViewFields>
  12. <Where>
  13.    <Gt>
  14.      <FieldRef Name="ID"  />
  15.      <Value Type="Counter">1</Value>
  16.    </Gt>
  17. </Where>
  18. '
  19.  
  20. $($(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 replies

Badge +8

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?

Userlevel 5
Badge +9

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.

Badge +8

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}

Userlevel 5
Badge +9

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

Badge +8

It's a single text.

Userlevel 5
Badge +9

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

Reply