Sitecore Powershell Script to find items with an empty field

I came across the necessity to build a report based on some parameters. The end goal of the script is to retrieve items that have been edited during a certain time period and that have a certain field empty.

The UI will end up looking like this:


As it can be seen in the screenshot above, the user will be able to set 4 parameters:

  1. Root - Which is going to be the parent node we will be analyzing 
  2. From Date - The start updated date
  3. To Date - The end updated date
  4. Pages Only - When checked, only items having a layout will be present in the report
Now that we know what the parameters are, let's get into the code. First we need to create the UI, to do this we use the following: 

$fromdate = $null

$todate = $null

$pagesOnly = $false

$database = "master"

$root = Get-Item -Path "$($database):\content\Home"



$settings = @{

    Title = "Empty Field Report"

    Width = "600"

    Height = "600"

    OkButtonName = "Run"

    CancelButtonName = "Cancel"

    Description = "Retrieve items that have been updated during a certain time period and that have an specified empty field"

    Parameters = @(

        @{ Name = "root"; Title="Choose the report root";

        Source="DataSource=/sitecore/content&DatabaseName=master";

        editor="droptree"; Mandatory=$true;},

        @{ Name = "fromdate"; Title="From Date";

        editor="date"; Mandatory=$true;},

  @{ Name = "todate"; Title="To Date";

        editor="date"; Mandatory=$true;},

  @{ Name = "pagesOnly"; Title="Search for pages only";

        Value=$false; Editor="bool"; Mandatory=$false;}

    )

}

$result = Read-Variable @settings

if($result -ne "ok") {

    Exit

}

We can add as many parameters as we want, and with different types. The current version of PSE allows the following types:
  • bool
  • check
  • date
  • date time
  • droplist
  • droptree
  • info
  • multilist
  • multilist search
  • multiple user
  • multiple user role
  • multiple role
  • radio
  • rule
  • rule action
  • treelist
  • time
Once we have created the UI, we need to get into the actual logic of the script (which is really simple). We will create a function that will return an item if it has been edited between the specified dates and if the item has an empty field (which we specify in the code): 


function Is-ValidItem{

     [CmdletBinding()]

    param(

        [Parameter(Mandatory=$true, Position=0)]

        [Sitecore.Data.Items.Item]$Item,

  [datetime]$From=([datetime]::Today),

  [datetime]$To=([datetime]::Today),

  [bool]$PagesOnly=([bool]::$false)

    )

 if($PagesOnly){

  $layout = Get-Layout -FinalLayout -Item $Item

  if($layout){

    ## If we are checking for an empty DATE FIELD we will need to add the following condition -or $Item."DATEFIELD" -eq [DateTime]::MinValue
  if((([string]::IsNullOrEmpty($Item."ANYFIELDNAME")) ) -And ($Item."__Updated" -le $To -And $Item."__Updated" -ge $From)){
     return $Item;
   }
  }

 }

 else{
  if((([string]::IsNullOrEmpty($Item."ANYFIELDNAME")) ) -And ($Item."__Updated" -le $To -And $Item."__Updated" -ge $From)){

   return $Item;

  }

 }
    return "";
}

After creating the method we have everything we need to build the logic to actually get the items based on the criteria we need to match and display the results:

$items = Get-ChildItem -Path $root.ProviderPath -Recurse | Where-Object {  (Is-ValidItem $_ -To $todate -From $fromdate -PagesOnly $pagesOnly) -ne "" } |  Sort-Object __Updated -descending


if($items.Count -eq 0) {

    Show-Alert "No items found for the path provided"

} else {

    $props = @{

        Title = "Empty Field Report"

        InfoTitle = "Empty Field Report"

        InfoDescription = "Lists all items within the specified dates that have an empty field"

        PageSize = 25

    }

        $items |

        Show-ListView @props -Property @{Label="Name"; Expression={$_.DisplayName} },

            @{Label="Path"; Expression={$_.ItemPath} }

}

Close-Window

The complete script would look like this:


<# .SYNOPSIS Lists the items based on the workflow approved date #>

function Is-ValidItem{

     [CmdletBinding()]

    param(

        [Parameter(Mandatory=$true, Position=0)]

        [Sitecore.Data.Items.Item]$Item,

  [datetime]$From=([datetime]::Today),

  [datetime]$To=([datetime]::Today),

  [bool]$PagesOnly=([bool]::$false)

    )

 if($PagesOnly){

  $layout = Get-Layout -FinalLayout -Item $Item

  if($layout){

    ## If we are checking for an empty DATE FIELD we will need to add the following condition -or $Item."DATEFIELD" -eq [DateTime]::MinValue
  if((([string]::IsNullOrEmpty($Item."ANYFIELDNAME")) ) -And ($Item."__Updated" -le $To -And $Item."__Updated" -ge $From)){
     return $Item;
   }
  }

 }

 else{
  if((([string]::IsNullOrEmpty($Item."ANYFIELDNAME")) ) -And ($Item."__Updated" -le $To -And $Item."__Updated" -ge $From)){

   return $Item;

  }

 }
    return "";
}



$fromdate = $null

$todate = $null

$pagesOnly = $false

$database = "master"

$root = Get-Item -Path "$($database):\content\Home"



$settings = @{

    Title = "Empty Date Report"

    Width = "600"

    Height = "600"

    OkButtonName = "Run"

    CancelButtonName = "Cancel"

    Description = "Retrieve items that have been updated in the specified date and that have an empty field"

    Parameters = @(

       

        @{ Name = "root"; Title="Choose the report root";

        Source="DataSource=/sitecore/content&DatabaseName=master";

        editor="droptree"; Mandatory=$true;},

        @{ Name = "fromdate"; Title="From Date";

        editor="date"; Mandatory=$true;},

  @{ Name = "todate"; Title="To Date";

        editor="date"; Mandatory=$true;},

  @{ Name = "pagesOnly"; Title="Search for pages only";

        Value=$false; Editor="bool"; Mandatory=$false;}

       

    )

}

$result = Read-Variable @settings

if($result -ne "ok") {

    Exit

}



$items = Get-ChildItem -Path $root.ProviderPath -Recurse | Where-Object {  (Is-ValidItem $_ -To $todate -From $fromdate -PagesOnly $pagesOnly) -ne "" } |  Sort-Object __Updated -descending


if($items.Count -eq 0) {

    Show-Alert "No items found for the path provided"

} else {

    $props = @{

        Title = "Empty Date Report"

        InfoTitle = "Empty Date Report"

        InfoDescription = "Lists all items within the specified dates that have an empty field"

        PageSize = 25

    }

        $items |

        Show-ListView @props -Property @{Label="Name"; Expression={$_.DisplayName} },

            @{Label="Path"; Expression={$_.ItemPath} }

}

Close-Window

Happy Scripting!

Comments