-
Index Searching - Find-Item using custom fields with Sitecore Powershell
July 5, 2019 Rodrigo Peplau 0
UPDATE: Michael West likes this solution and confirmed that it will be part of the next release of SPE (6.0), becoming my first contribution to SPE! Here is the ticket where this implementation is being tracked.
If you are a big fan of Sitecore Powershell Extensions like me, you probably know the Find-Item command, which allows searching your search index in Powershell scripts. This command, however, has an important limitation: you cannot use custom fields to filter and sort from the index. Due to this limitation, extended usage of Find-Item with custom code is usually not possible.
Internally Find-Item uses the base class “Sitecore.ContentSearch.SearchTypes.SearchResultItem“, which does not include any custom fields. What we usually do with C# is to create a class that inherits from “SearchResultItem“, adding our custom fields, such as below:
public class CustomSearchResultItem : SearchResultItem { [IndexField("_templates")] [DataMember] public virtual List<ID> TemplateIds { get; set; } }
The Find-Item command, however, does not allow using this field. For instance, the following script:
$props = @{ Index = "sitecore_master_index" Where = "Paths.Contains(@0) And TemplateIds.Contains(@1)" WhereValues = [ID]::Parse("{D5B8857B-DE30-4616-84F5-812A7129ACD5}"), [ID]::Parse("{50FFE147-4F48-428B-A417-4D96DD2048FF}") } Find-Item @props
Will give the following error:
Find-Item : No property or field 'TemplateIds' exists in type 'SearchResultItem'
Because, yeah… that property is only defined at our custom class, not at SearchResultItem. To circumvent this limitation I built this special command.
Find-CustomItem
This command extends the original Find-Item, allowing you to pass a type that will be used instead of the base SearchResultItem class:
$props = @{ Index = "sitecore_master_index" Type = [PSWebsite.Foundation.Indexing.SearchTypes.BaseSearchResultItem] Where = "Paths.Contains(@0) And TemplateIds.Contains(@1)" WhereValues = [ID]::Parse("{D5B8857B-DE30-4616-84F5-812A7129ACD5}"), [ID]::Parse("{50FFE147-4F48-428B-A417-4D96DD2048FF}") } Find-CustomItem @props
Create the custom command
In order to use Find-CustomItem in your project, you should:
- Add NuGet references to Sitecore.ContextSearch and Sitecore.ContextSearch.Linq;
- Add a reference to Cognifide.Powershell.dll;
- Create a custom FindItemCommand class extending Cognifide.PowerShell.Commandlets.Data.Search.FindItemCommand;
You can download the whole class here - Register your new command with an include patch – make sure the content is according to the following
<?xml version="1.0"?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <powershell> <commandlets> <add Name="Find Item" type="PSWebsite.Foundation.Indexing.Powershell.Commandlets.FindItemCommand, PSWebsite.Foundation.Indexing" /> </commandlets> </powershell> </sitecore> </configuration>
Limitations
Not all parameters from Find-Item are supported by Find-CustomItem. If you use any of the unsupported parameters along with any custom properties, it will cast the query to the base SearchResultItem, and give the same error as the original Find-Item command.
Supported parameters:
- Index
- Where
- WhereValues
- OrderBy
- First
- Last
- Skip
Unsupported parameters:
- Criteria
- Predicate
- ScopeQuery
Categories: Powershell, SPE
Part 2 - XConnect Avatar Facet breaking Experience Profile (Follow Up) Programmatically "Visit" a page (and all xDB-related)
Proudly 9x Sitecore MVP
(2016-2024)
Localization
Recent Posts
Recent Comments
- navan on Meet MVPinny: the AI-Powered Sitecore Assistant
- Adriana on Content generation with Sitecore Connect and ChatGPT
- NAVAN on Automatic Sitecore NuGet upgrades with Powershell
- Hedipo S Menezes on Corey Peplau wrote this - WFFM conflict with Unity DI and a lesson on how Sitecore community is so amazing
- Rodrigo Peplau on ERROR [Content Testing]: Cannot find PhantomJS executable at ' (...) /data/tools/phantomjs/phantomjs.exe'. Aborting screenshot generation.
Archives
- June 2024
- April 2024
- February 2024
- December 2023
- November 2023
- August 2023
- July 2023
- January 2023
- February 2022
- December 2021
- November 2021
- March 2021
- July 2020
- February 2020
- September 2019
- July 2019
- April 2019
- March 2019
- December 2018
- February 2018
- January 2018
- November 2017
- September 2017
- August 2017
- July 2017
- March 2017
- February 2017
- November 2016
- September 2016
- August 2016
- July 2016
- April 2016
- November 2015
- September 2015
- July 2015
- April 2015
- March 2015
- February 2015
Categories
- Actions
- Active Directory
- Analytics
- Architecture
- Bug fixing
- CDP/Personalize
- ChatGPT
- Content Edition Experience
- Content Hub
- Continuous Integration
- Dev
- Development
- Environments
- Experience Editor
- Experience Forms
- Front-end
- Health Check builds
- Helix
- How To
- LDAP
- MVP
- MVP Summit
- MVPinny
- Phantom JS
- Powershell
- QA
- Richtext Editor
- Rules
- Security Provider
- SIF
- Sitecore 9
- Sitecore API
- Sitecore Community
- SItecore Connect
- Sitecore Modules
- Sitecore Rocks
- Sitecore Rule Processor
- Sitecore Symposium
- SPE
- SPEAK
- SUG
- Support Ticket
- TDS
- Team City
- Uncategorized
- Upgrades
- Visual Studio
- WFFM
- Workflow
- XConnect
- xDB
- XM Cloud