Find-Item
Finds items using the Sitecore Content Search API.

Syntax

1
Find-Item [-Index] <String> [-Criteria <SearchCriteria[]>] [-QueryType <Type>] [-OrderBy <String>] [-First <Int32>] [-Last <Int32>] [-Skip <Int32>] [<CommonParameters>]
2
Find-Item [-Index] <String> [-Where <String>] [-WhereValues <Object[]>] [-Filter <String>] [-FilterValues <Object[]>] [-FacetOn <String[]>] [-FacetMinCount <Int32>] [-QueryType <Type>] [-OrderBy <String>] [-First <Int32>] [-Last <Int32>] [-Skip <Int32>] [<CommonParameters>]
3
Find-Item [-Index] <String> [-WherePredicate <Expression<Func<SearchResultItem, bool>>>] [-FilterPredicate <Expression<Func<SearchResultItem, bool>>>] [-QueryType <Type>] [-OrderBy <String>] [-First <Int32>] [-Last <Int32>] [-Skip <Int32>] [<CommonParameters>]
4
Find-Item [-Index] <String> [-ScopeQuery <String>] [-QueryType <Type>] [-OrderBy <String>] [-First <Int32>] [-Last <Int32>] [-Skip <Int32>] [<CommonParameters>]
Copied!

Detailed Description

The Find-Item command searches for items using the Sitecore Content Search API. The type SearchResultItem is used as the type when working with IQueryable.
© 2010-2020 Adam Najmanowicz, Michael West. All rights reserved. Sitecore PowerShell Extensions

Parameters

-Index <String>

Name of the Index that will be used for the search:
Find-Item -Index sitecore_master_index -First 10
Aliases
Required?
true
Position?
1
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-Criteria <SearchCriteria[]>

Simple form of search in which logical "AND" operations are needed.
1
@{ Filter = "Equals"; Field = "_templatename"; Value = "Sample Item"; },
2
@{ Filter = "DescendantOf"; Value = (Get-Item "master:/content/") }
Copied!
Where "Filter" is one of the following values:
  • Equals
  • StartsWith
  • Contains
  • ContainsAny
  • ContainsAll
  • EndsWith
  • DescendantOf - performs a Contains with the field _path
  • Fuzzy
  • InclusiveRange - performs a Between using int, double, datetime, and stringtypes
  • ExclusiveRange - same as InclusiveRange
  • MatchesRegex - use something like ^.*$
  • MatchesWildcard - use something like H?li*m
  • LessThan
  • GreaterThan
Where "Field" is the Index Field name found on the SearchResultItem such as the following:
  • __smallcreateddate - CreatedDate
  • __smallupdateddate - Updated
  • _group - ID
  • _template - TemplateId
  • _templatename - TemplateName
  • _fullpath - Path
Where "Value" is one of the following:
  • string
  • string[]
  • Sitecore.Data.ID
  • Sitecore.Data.Items.Item
  • Sitecore.Data.Items.Item[]
  • object[] - when using @() you'll get this type, which will be treated as an array of strings
  • PSObject[]
  • System.Collections.ArrayList
  • System.Collections.Generics.List<string>
Where "Boost" is a positive number greater than 0
Fields by which you can filter can be discovered using the following script:
1
$criteria = @(
2
@{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content/" }
3
)
4
$props = @{
5
Index = "sitecore_master_index"
6
Criteria = $criteria
7
}
8
9
Find-Item @props -First 1 | Select-Object -Expand "Fields"
Copied!
Where "Invert" is a boolean to indicate the following:
  • $false - This is the default value. Do exactly as the query is defined.
  • $true - Reverse the logic. For example, "Contains" is treated like "NotContains", "Equals" is treated like "NotEquals".
Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-Where <String>

Where the "Where" is the Dynamic Linq query and "WhereValues" includes the array of values to be replaced in the query.
1
$props = @{
2
Index = "sitecore_master_index"
3
Where = 'TemplateName = @0 And [email protected]'
4
WhereValues = "Sample Item", "en"
5
}
6
7
Find-Item @props
Copied!
Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-WhereValues <Object[]>

An Array of objects for Dynamic Linq "-Where" parameter as explained in: https://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library
Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-Filter <String>

Where the "Filter" is the Dynamic Linq query and "FilterValues" includes the array of values to be replaced in the query.
Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-FilterValues <Object[]>

Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-WherePredicate <Expression<Func<SearchResultItem,bool>>>

Use the New-SearchPredicate command to build the appropriate predicates.
Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-FilterPredicate <Expression<Func<SearchResultItem,bool>>>

Use the New-SearchPredicate command to build the appropriate predicates.
Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-ScopeQuery <String>

When combined with the Query Builder field, a simple query can be crafted to return search results.
Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-OrderBy <String>

Field by which the search results sorting should be performed. This is the .Net Property name as see on the SearchResultItem class. Dynamic Linq ordering syntax used. https://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library
Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-First <Int32>

Number of returned search results.
Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-Last <Int32>

Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-Skip <Int32>

Number of search results to be skipped skip before returning the results commences.
Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

-Property <String[]>

An array of property names which match with the SearchResultItem type.
Note: The use of Initialize-Item is not supported because the object returned is no longer a SearchResultItem and therefore unable to guarantee that Item objects can be returned. #1123
Aliases
Required?
false
Position?
named
Default Value
Accept Pipeline Input?
false
Accept Wildcard Characters?
false

Outputs

The output type is the type of the objects that the cmdlet emits.
  • Sitecore.ContentSearch.SearchTypes.SearchResultItem

Notes

Help Author: Adam Najmanowicz, Michael West

Examples

EXAMPLE 1

Fields by which filtering can be performed using the -Criteria parameter.
1
$criteria = @(
2
@{Filter = "Equals"; Field = "_templatename"; Value = "Sample Item"},
3
@{Filter = "Contains"; Field = "Title"; Value = "Sitecore"},
4
@{Filter = "Contains"; Field = "Title"; Value = "Powerful ways"; "Invert" = $true}
5
)
6
$props = @{
7
Index = "sitecore_master_index"
8
Criteria = $criteria
9
}
10
11
Find-Item @props
Copied!

EXAMPLE 2

Find items using a search built by the Query Builder field.
1
$props = @{
2
Index = "sitecore_master_index"
3
ScopeQuery = "location:{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9};custom:title|Sitecore"
4
}
5
6
Find-Item @props
Copied!

EXAMPLE 3

Find all items of template "Sample Item" which are in "English" under the "Home" item using Dynamic LINQ syntax.
1
$templateId = [ID]::Parse("{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}")
2
$homeId = [ID]::Parse("{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}")
3
$language = "en"
4
5
$props = @{
6
Index = "sitecore_master_index"
7
Where = 'TemplateId = @0 And [email protected] And Paths.Contains(@2)'
8
WhereValues = $templateId, $language, $homeId
9
}
10
11
Find-Item @props
Copied!

EXAMPLE 4

Find items using a complex search predicate.
1
$criteriaTemplate = @(
2
@{Filter = "Equals"; Field = "_templatename"; Value = "Template Field"; },
3
@{Filter = "Equals"; Field = "_templatename"; Value = "Sample Item"; Boost=25; },
4
@{Filter = "Equals"; Field = "_templatename"; Value = "Sample Content"; }
5
)
6
7
$predicateTemplate = New-SearchPredicate -Operation Or -Criteria $criteriaTemplate
8
9
$criteriaContent = @{Filter = "Contains"; Field = "Title"; Value = 'Sitecore'}
10
$predicateTitle = New-SearchPredicate -Criteria $criteriaContent
11
12
$predicateTemplateAndTitle = New-SearchPredicate -First $predicateTemplate -Second $predicateTitle -Operation And
13
14
$root = Get-Item -Path "master:" -ID "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"
15
$criteriaRoot = @{Filter = "DescendantOf"; Value = $root }
16
$predicateRoot = New-SearchPredicate -Criteria $criteriaRoot
17
18
$predicate = New-SearchPredicate -First $predicateRoot -Second $predicateTemplateAndTitle -Operation And
19
20
$props = @{
21
Index = "sitecore_master_index"
22
WherePredicate = $predicate
23
}
24
25
Find-Item @props
Copied!

EXAMPLE 5

Find items using logical AND conditions with ContainsAny. Demonstrates that different array types are handled.
Note: When searching for IDs you can use the proper type like [ID[]]@("{C852E80E-ED49-4354-A397-6F66487F0E26}") so SPE will handle the conversion to ShortID.
1
$criteria = @(
2
@{Filter = "Equals"; Field = "_templatename"; Value = "Sample Content"; Boost=25; },
3
@{Filter = "ContainsAny"; Field = "Title"; Value = [string[]]@('$name','$date')},
4
@{Filter = "ContainsAny"; Field = "Title"; Value = @('$name','$date')},
5
@{Filter = "ContainsAny"; Field = "Title"; Value = [System.Collections.ArrayList]@('$name','$date')},
6
@{Filter = "ContainsAny"; Field = "Title"; Value = [System.Collections.Generic.List[string]]@('$name','$date')}
7
)
8
$props = @{
9
Index = "sitecore_master_index"
10
Criteria = $criteria
11
}
12
13
Find-Item @props
Copied!

EXAMPLE 6

Find items using logical AND with ContainsAll. Demonstrates looking in multilist fields.
1
$criteria = @(
2
@{Filter = "Equals"; Field = "_templatename"; Value = "Sample Content"; Boost=25; },
3
@{Filter = "ContainsAll"; Field = "RelatedImages"; Value = @('{4D427A1D-312D-4EEE-A519-1F5700675BAF}','{4B603402-62AB-4ECB-9CAE-98790DDBC35A}')}
4
)
5
$props = @{
6
Index = "sitecore_master_index"
7
Criteria = $criteria
8
}
9
10
Find-Item @props
Copied!

EXAMPLE 7

Find an item by ID.
1
$criteria = @(
2
@{Filter = "Equals"; Field = "_group"; Value = "{C89D37FF-3919-4D69-9925-943B67BD22D6}"}
3
)
4
$props = @{
5
Index = "sitecore_master_index"
6
Criteria = $criteria
7
}
8
Find-Item @props
Copied!

EXAMPLE 8

Find items within a data range. Possible filters are InclusiveRange and ExclusiveRange . When using dates, only yyyyMMdd is considered in the comparison so no need to get too precise.
1
$props = @{
2
Index = "sitecore_master_index"
3
Criteria = @{
4
Field = "__smallcreateddate"
5
Filter = "InclusiveRange"
6
Value = [datetime[]]@([datetime]"01/05/2015", [datetime]::Today)
7
}, @{
8
Field = "_name"
9
Filter = "Fuzzy"
10
Value = "sample"
11
}
12
}
13
14
Find-Item @props
Copied!

EXAMPLE 9

Find and count all items beneath a root item using the item path.
1
$criteria = @(
2
@{Filter = "Contains"; Field = "_fullpath"; Value = "/sitecore/content/home"},
3
@{Filter = "Equals"; Field = "_latestversion"; Value = "1"}
4
)
5
$props = @{
6
Index = "sitecore_master_index"
7
Criteria = $criteria
8
}
9
10
Find-Item @props | Measure-Object
Copied!

EXAMPLE 10

Find and count all items beneath a root item using the item id.
1
$criteria = @(
2
@{Filter = "Contains"; Field = "_path"; Value = "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"},
3
@{Filter = "Equals"; Field = "_latestversion"; Value = "1"}
4
)
5
$props = @{
6
Index = "sitecore_master_index"
7
Criteria = $criteria
8
}
9
10
Find-Item @props | Measure-Object
Copied!

EXAMPLE 11

Find items and sort (boost) based on the date field. If this were used on a field containing future dates you should expect to see them mixed with past dates. This example demonstrates using Solr functions.
1
$criteria = @(
2
@{Filter = "Equals"; Field = "_val_"; Value = "recip(abs(ms(NOW/HOUR,__smallcreateddate_tdt)),3.16e-11,4,.4)"}
3
)
4
$props = @{
5
Index = "sitecore_master_index"
6
Criteria = $criteria
7
}
8
9
Find-Item @props | Select-Object Name,CreatedDate
Copied!

EXAMPLE 12

Find items where the created date is older than the specified time in UTC.
1
$date = New-Object DateTime 2019, 8, 1, 0, 0, 0, ([DateTimeKind]::Utc)
2
3
$criteria = @(
4
@{Filter = "LessThan"; Field = "__smallupdateddate"; Value = $date}
5
)
6
$props = @{
7
Index = "sitecore_master_index"
8
Criteria = $criteria
9
}
10
11
Find-Item @props
Copied!

EXAMPLE 13

Find items where the title contains the specified value. A custom implementation of SearchResultItem is used to enable the use of the property Title in the Dynamic Query.
1
class TitleSearchResultItem : SearchResultItem
2
{
3
[Sitecore.ContentSearch.IndexField("title")]
4
[string]$Title
5
}
6
7
$props = @{
8
Index = "sitecore_master_index"
9
Where = "Title.Contains(@0)"
10
WhereValues = "great"
11
QueryType = [TitleSearchResultItem]
12
}
13
14
Find-Item @props
Copied!

EXAMPLE 14

Find items where the path contains the specified Id and base templates contain the specified Id using a Dynamic Query. A custom implementation of SearchResultItem is used to enable the use of the property TemplateIds in the Dynamic Query.
1
class TemplatesSearchResultItem : SearchResultItem
2
{
3
# For items contained within an SXA index try using the name "inheritance_sm".
4
[Sitecore.ContentSearch.IndexField("_templates")]
5
[System.Collections.Generic.List[ID]]$TemplateIds
6
}
7
8
$props = @{
9
Index = "sitecore_master_index"
10
Where = "Paths.Contains(@0) And TemplateIds.Contains(@1)"
11
WhereValues = [ID]::Parse("{371EEE15-B6F3-423A-BB25-0B5CED860EEA}"), [ID]::Parse("{B0B6FB08-6BBE-43F2-8E36-FCE228325B63}")
12
QueryType = [TemplatesSearchResultItem]
13
}
14
15
Find-Item @props
Copied!

EXAMPLE 15

Find items where the title contains "Sitecore" using a Scope Query. A custom implementation of SearchResultItem is used to enable the use of the property Title in the Scope Query.
1
class TitleSearchResultItem : SearchResultItem
2
{
3
[Sitecore.ContentSearch.IndexField("title")]
4
[string]$Title
5
}
6
7
$props = @{
8
Index = "sitecore_master_index"
9
ScopeQuery = "location:{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9};custom:title|Sitecore"
10
QueryType = [TitleSearchResultItem]
11
}
12
13
Find-Item @props | Select-Object -Property Title
Copied!

EXAMPLE 16

Find items where the template is "Sample Content" and the title contains "Sitecore" and created by "admin" using the Criteria Query. A custom implementation of SearchResultItem is used to enable the use of the property Title and Creator in the Dynamic Query.
1
class TitleSearchResultItem : SearchResultItem
2
{
3
[Sitecore.ContentSearch.IndexField("title")]
4
[string]$Title
5
[Sitecore.ContentSearch.IndexField("_creator")]
6
[string]$Creator
7
}
8
9
$criteria = @(
10
@{Filter = "Equals"; Field = "_templatename"; Value = "Sample Content"},
11
@{Filter = "Contains"; Field = "Title"; Value = "Sitecore"},
12
@{Filter = "Contains"; Field = "_creator"; Value = "admin"}
13
)
14
$props = @{
15
Index = "sitecore_master_index"
16
Criteria = $criteria
17
QueryType = [TitleSearchResultItem]
18
}
19
20
Find-Item @props
Copied!

EXAMPLE 17

Find items matching a complex query. A custom implementation of SearchResultItem is used to enable the use of the property Title in the Predicate Query.
1
class TitleSearchResultItem : SearchResultItem
2
{
3
[Sitecore.ContentSearch.IndexField("title")]
4
[string]$Title
5
}
6
7
$criteriaTemplate = @{Filter = "Equals"; Field = "_templatename"; Value = "Template Field"; }, @{Filter = "Equals"; Field = "_templatename"; Value = "Sample Item"; Boost=25; }, @{Filter = "Equals"; Field = "_templatename"; Value = "Sample Content"; }
8
$predicateTemplate = New-SearchPredicate -Operation Or -Criteria $criteriaTemplate -QueryType ([TitleSearchResultItem])
9
10
$criteriaContent = @{Filter = "Contains"; Field = "Title"; Value = 'Sitecore'}
11
$predicateTitle = New-SearchPredicate -Criteria $criteriaContent -QueryType ([TitleSearchResultItem])
12
13
$predicateTemplateAndTitle = New-SearchPredicate -First $predicateTemplate -Second $predicateTitle -Operation And -QueryType ([TitleSearchResultItem])
14
15
$root = Get-Item -Path "master:" -ID "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}"
16
$criteriaRoot = @{Filter = "DescendantOf"; Value = $root }
17
$predicateRoot = New-SearchPredicate -Criteria $criteriaRoot -QueryType ([TitleSearchResultItem])
18
19
$predicate = New-SearchPredicate -First $predicateRoot -Second $predicateTemplateAndTitle -Operation And -QueryType ([TitleSearchResultItem])
20
21
$props = @{
22
Index = "sitecore_master_index"
23
WherePredicate = $predicate
24
QueryType = [TitleSearchResultItem]
25
}
26
27
Find-Item @props
Copied!

EXAMPLE 18

Find items under the Content tree where the language is "en" and there are more than two occurrences. This could be used to find duplicate item names at the same path.
1
$props = @{
2
Index = "sitecore_master_index"
3
Where = 'Paths.Contains(@0)'
4
WhereValues = [ID]::Parse("{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}")
5
Filter = 'Language = @0'
6
FilterValues = "en"
7
FacetOn = "Path"
8
FacetMinCount = 2
9
}
10
11
Find-Item @props | Select-Object -Expand Categories | Select-Object -Expand Values
Copied!

EXAMPLE 19

Find the most recently updated item.
1
$templateId = "{C382C7B8-7567-40CB-AE89-7F5680735D4E}"
2
$criteria = @(
3
@{Filter = "Equals"; Field = "_template"; Value = $templateId}
4
)
5
$props = @{
6
Index = "sitecore_master_index"
7
Criteria = $criteria
8
OrderBy = "Updated"
9
Last = 1
10
}
11
12
Find-Item @props | Select-Object -Property ItemId, Name, Path, Updated
Copied!

EXAMPLE 20

Find items where the expiration date has not passed (now to the future) or the expiration date is empty (never expires).
1
$props = @{
2
Index = "sitecore_sxa_master_index"
3
ScopeQuery = "+location:{447D82A5-BDBD-4898-8598-D79B3EB9BE6D};+template:{51ED5851-1A61-4DAE-B803-6C7FAE6B43D8};custom:EventEndDate|[* TO NOW-100YEARS];custom:EventEndDate|[NOW TO *];"
4
}
5
6
Find-Item @props
Copied!

Related Topics

Last modified 6mo ago