none
Powershell CAML Query for Empty Date Field returns 0 items RRS feed

  • Question

  • I'm trying to do a CAML query. I'm using a CAML builder tool and results seem to come back when using the tool.  Using the same query in Powershell returns no data.

    I'm trying a simple query like:

    <Where>
          <IsNull>
             <FieldRef Name='ReviewDate' />
          </IsNull>
    </Where>

    If I use other combinations for a specific date I do get items.  I'm just trying to find a way to find empty items without having to return all the data in the list first.


    David Jenkins

    Monday, May 1, 2017 4:36 PM

Answers

  • $sourceWebURL = "http://dev01:34911"
    $sourceListName = "Documents"
    $spSourceWeb = Get-SPWeb $sourceWebURL 
    $spSourceList = $spSourceWeb.Lists[$sourceListName]
    $spSubFolder = $spSourceList.RootFolder.SubFolders["/Documents"]
    
    $reviewdateCAML = "
        <Where>
          <IsNull>
             <FieldRef Name='ReviewDate' />
          </IsNull>
       </Where>
    "
    
    $reviewdateQuery = New-Object Microsoft.SharePoint.SPQuery
    $reviewdateQuery.ViewAttributes = "Scope='RecursiveAll'"
    $reviewdateQuery.Query = $reviewdateCAML
    $reviewdateQuery.Folder = $spSubFolder
    $reviewdateDocuments = $spSourceList.GetItems($reviewdateQuery)
    "Document Count:$($reviewdateDocuments.Count)"


    David Jenkins

    Monday, May 1, 2017 5:38 PM
  • Never mind.  It works.  The results aren't exactly the same.  I'll have to figure out why.

    David Jenkins

    Monday, May 1, 2017 6:14 PM

All replies

  • Can you please share your powershell script?
    Monday, May 1, 2017 5:14 PM
  • $sourceWebURL = "http://dev01:34911"
    $sourceListName = "Documents"
    $spSourceWeb = Get-SPWeb $sourceWebURL 
    $spSourceList = $spSourceWeb.Lists[$sourceListName]
    $spSubFolder = $spSourceList.RootFolder.SubFolders["/Documents"]
    
    $reviewdateCAML = "
        <Where>
          <IsNull>
             <FieldRef Name='ReviewDate' />
          </IsNull>
       </Where>
    "
    
    $reviewdateQuery = New-Object Microsoft.SharePoint.SPQuery
    $reviewdateQuery.ViewAttributes = "Scope='RecursiveAll'"
    $reviewdateQuery.Query = $reviewdateCAML
    $reviewdateQuery.Folder = $spSubFolder
    $reviewdateDocuments = $spSourceList.GetItems($reviewdateQuery)
    "Document Count:$($reviewdateDocuments.Count)"


    David Jenkins

    Monday, May 1, 2017 5:38 PM
  • Never mind.  It works.  The results aren't exactly the same.  I'll have to figure out why.

    David Jenkins

    Monday, May 1, 2017 6:14 PM