<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sitecore Xperiences &#187; SPE</title>
	<atom:link href="http://blog.peplau.com.br/category/spe/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.peplau.com.br</link>
	<description>The things I&#039;ve seen as a Sitecore Professional</description>
	<lastBuildDate>Sun, 13 Feb 2022 16:58:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.34</generator>
	<item>
		<title>Index Searching - Find-Item using custom fields with Sitecore Powershell</title>
		<link>http://blog.peplau.com.br/index-searching-find-item-using-custom-fields-with-sitecore-powershell/</link>
		<comments>http://blog.peplau.com.br/index-searching-find-item-using-custom-fields-with-sitecore-powershell/#comments</comments>
		<pubDate>Fri, 05 Jul 2019 17:21:50 +0000</pubDate>
		<dc:creator><![CDATA[Rodrigo Peplau]]></dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[SPE]]></category>

		<guid isPermaLink="false">http://blog.peplau.com.br/?p=768</guid>
		<description><![CDATA[<div class="lr_horizontal_share" data-share-url="http://blog.peplau.com.br/index-searching-find-item-using-custom-fields-with-sitecore-powershell/"></div>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 [&#8230;]]]></description>
				<content:encoded><![CDATA[<hr />
<p><em><strong>UPDATE:</strong> <a href="https://github.com/michaellwest" target="_blank">Michael West</a> likes this solution and confirmed that it will be part of the next release of SPE (6.0), becoming my first contribution to SPE! </em><em><a href="https://github.com/SitecorePowerShell/Console/issues/1120" target="_blank">Here is the ticket</a> where this implementation is being tracked.</em></p>
<hr />
<p>If you are a big fan of <a href="https://marketplace.sitecore.net/Modules/Sitecore_PowerShell_console.aspx" target="_blank">Sitecore Powershell Extensions</a> like me, you probably know the <a href="https://doc.sitecorepowershell.com/appendix/indexing/find-item" target="_blank">Find-Item</a> 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 <em>Find-Item</em> with custom code is usually not possible.</p>
<p>Internally <em>Find-Item</em> uses the base class &#8220;<em>Sitecore.ContentSearch.SearchTypes.SearchResultItem</em>&#8220;, which does not include any custom fields. What we usually do with C# is to create a class that inherits from &#8220;<em>SearchResultItem</em>&#8220;, adding our custom fields, such as below:</p>
<pre>public class CustomSearchResultItem : SearchResultItem
{
   [IndexField("_templates")]
   [DataMember]
   public virtual List&lt;ID&gt; TemplateIds { get; set; }
}</pre>
<p>The <em>Find-Item</em> command, however, does not allow using this field. For instance, the following script:</p>
<pre>$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</pre>
<p>Will give the following error:</p>
<pre>Find-Item : No property or field 'TemplateIds' exists in type 'SearchResultItem'</pre>
<p>Because, yeah&#8230; that property is only defined at our custom class, not at <em>SearchResultItem</em>. To circumvent this limitation I built this special command.</p>
<h2>Find-CustomItem</h2>
<p>This command extends the original <em>Find-Item</em>, allowing you to pass a type that will be used instead of the base <em>SearchResultItem</em> class:</p>
<pre>$props = @{
 Index = "sitecore_master_index"
<strong> Type = [PSWebsite.Foundation.Indexing.SearchTypes.BaseSearchResultItem]</strong>
 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</pre>
<h2>Create the custom command</h2>
<p>In order to use <em>Find-CustomItem</em> in your project, you should:</p>
<ol>
<li>Add NuGet references to <em>Sitecore.ContextSearch</em> and <em>Sitecore.ContextSearch.Linq</em>;</li>
<li>Add a reference to <em>Cognifide.Powershell.dll</em>;</li>
<li>Create a custom <em>FindItemCommand</em> class extending <em>Cognifide.PowerShell.Commandlets.Data.Search.FindItemCommand</em>;<br />
You can <a href="https://github.com/peplau/PSWebsite/blob/master/src/Foundation/Indexing/code/Powershell/Commandlets/FindItemCommand.cs" target="_blank">download the whole class here</a></li>
<li>Register your new command with an include patch &#8211; make sure the content is according to the following</li>
</ol>
<pre>&lt;?xml version="1.0"?&gt;
&lt;configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"&gt;
   &lt;sitecore&gt;
      &lt;powershell&gt;
         &lt;commandlets&gt;
            &lt;add Name="Find Item" type="PSWebsite.Foundation.Indexing.Powershell.Commandlets.FindItemCommand, PSWebsite.Foundation.Indexing" /&gt;
         &lt;/commandlets&gt;
      &lt;/powershell&gt;
   &lt;/sitecore&gt;
&lt;/configuration&gt;</pre>
<h2>Limitations</h2>
<p>Not all parameters from <em>Find-Item</em> are supported by <em>Find-CustomItem</em>. If you use any of the unsupported parameters along with any custom properties, it will cast the query to the base <em>SearchResultItem</em>, and give the same error as the original <em>Find-Item</em> command.</p>
<p><strong>Supported parameters:</strong></p>
<ul>
<li>Index</li>
<li>Where</li>
<li>WhereValues</li>
<li>OrderBy</li>
<li>First</li>
<li>Last</li>
<li>Skip</li>
</ul>
<p><strong>Unsupported parameters:</strong></p>
<ul>
<li>Criteria</li>
<li>Predicate</li>
<li>ScopeQuery</li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.peplau.com.br/index-searching-find-item-using-custom-fields-with-sitecore-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
