Reputation: 1320
I'm completely new to sitecore powershell. I have gone through some articles but nothing worked for me.
What I need is: in my Sitecore, I want to search for a specific keyword, and I want all the items listed in the output. The keyword may be contained in Single-Line text, Multiline Text, General Link, RichText or Image. I know this is achievable using sitecore powershell. Could any genius (of course for me they are) solve my problem?
Something I was trying out. If this is not making any sense, please ignore :)
cd 'master:/sitecore/content'
function FilterItemsToProcess($item)
{
Get-Item master:\content
}
$list = [System.Collections.ArrayList]@()
$itemsToProcess = Get-ChildItem -Recurse . | foreach {FilterItemsToProcess($_)}
if($itemsToProcess -ne $null)
{
$itemsToProcess | ForEach-Object {
$match = 0;
foreach($field in $_.Fields) {
# Only look within Single-line and Rich Text fields
# In this example, we look for english content within Chinese versions
if($field.Type -eq "Single-Line Text" -or $field.Type -eq "Rich Text") {
if($field -match "mykeyword") {
Write-Host "Found: '$field' within a" $field.Type "field on Item:" $_.ID
$match = 1;
}
}
}
if($match -eq 1){
[void]$list.Add($_)
}
}
}
Upvotes: 2
Views: 6039
Reputation: 4266
You really want to change the approach you have here. What you have is going to perform very badly. To do any kind of searching in Sitecore you want to use SearchAPI
- if you look at the Find-Item
command in SPE
Also you will want to add a custom computed field to the index to make this simpler.
In the custom field add all fields that are Single-line text
or Richtext
to the index:
So in the computed field you could do something like:
public class MyCustomField : IComputedIndexField
{
public object ComputeFieldValue(IIndexable indexable)
{
var item = (Item)(indexable as SitecoreIndexableItem);
if (item == null)
{
return string.Empty;
}
var value = new StringBuilder();
item.Fields.ReadAll();
foreach (Field field in item.Fields)
{
switch (field.Type.ToLowerInvariant())
{
case "single-line text":
case "richtext":
value.AppendLine(field.Value);
break;
}
}
return value;
}
}
Then add the field to your config via an include file:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<indexConfigurations>
<defaultLuceneIndexConfiguration>
<fields hint="raw:AddComputedIndexField">
<field fieldName="mycustomfield">MyProject.ComputedFields.MyCustomField, MyProject</field>
</fields>
</defaultLuceneIndexConfiguration>
</indexConfigurations>
</contentSearch>
</sitecore>
</configuration>
So you could change your powershell to:
Find-Item `
-Index sitecore_master_index `
-Criteria @{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content/" }
@{Filter = "Contains"; Field = "mycustomfield"; Value = "mykeyword"}
That would then give you all items that have your keyword in the required field types.
An alternative if you can't add the custom field would be:
Find-Item `
-Index sitecore_master_index `
-Criteria @{Filter = "StartsWith"; Field = "_fullpath"; Value = "/sitecore/content/" }
@{Filter = "Contains"; Field = "_content"; Value = "mykeyword"}
This would give you all instances where any field in the item contains the value mykeyword
. Because only Single-line Text
, Multi-line text
and Richtext
store the textual content, that should be pretty close to what you want. You may get some contamination from fields like link fields or image fields if that text is in any of the properties of the field.
Upvotes: 5