Reputation: 751
I have implemented a computed field index in Sitecore 7.2. However, the index manager is broken now and I get the following message
Invalid cast from 'System.String' to 'Sitecore.ContentSearch.ProviderIndexConfiguration'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Invalid cast from 'System.String' to 'Sitecore.ContentSearch.ProviderIndexConfiguration'.
I have implemented the following class for my computed field:
namespace Computed.Search
{
public class OfficeLocationComputedField : IComputedIndexField
{
public string FieldName { get; set; }
public string ReturnType { get; set; }
public object ComputeFieldValue(IIndexable indexable)
{
Assert.ArgumentNotNull(indexable, nameof(indexable));
try
{
var result = new List<string>();
var indexableItem = indexable as SitecoreIndexableItem;
if (indexableItem == null)
return null;
var item = (Item) indexableItem;
// Treelist fields map to the MultilistField type as per ~/App_Config/FieldTypes.config
MultilistField field = item?.Fields["officelocation"];
if (field == null)
return null;
var items = field.GetItems();
if (items == null || items.Length == 0)
return result;
foreach (var locationItem in items)
{
//result.Add(locationItem.Name); // if you want the name of the item
result.Add(locationItem.DisplayName); // if you want the display name of the item
//result.Add(locationItem["Title"]); // if you want the value of a field on the item
}
return result;
}
catch (Exception exception)
{
Log.Error($"An Error occured in custom computed index. {exception.Message}", exception);
}
return null;
}
}
}
And the configuration file is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<indexConfigurations>
<defaultLuceneIndexConfiguration>
<fields hint="raw:AddComputedIndexField">
<field fieldName="officelocation" storageType="YES" indexType="TOKENIZED">Computed.Search.OfficeLocationComputedField, Computed</field>
</fields>
</defaultLuceneIndexConfiguration>
</indexConfigurations>
</contentSearch>
</sitecore>
</configuration>
I don't know what mistake I am making or what else I need to change?
Upvotes: 2
Views: 2327
Reputation: 751
The older patch file that I had created and was alphabetically before the Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config was still in the Website\App_Config\Include folder. I forgot to remove it. Removing that old file fixed the problem. Everything is working now.
Upvotes: 0
Reputation: 27142
The Sitecore Invalid cast from 'System.String' to ...
exception in 90% cases is cause by the configuration mistakes.
In your case Sitecore tries to cast string to ProviderIndexConfiguration
. It's LuceneIndexConfiguration
which inherits from ProviderIndexConfiguration
.
It looks like Sitecore cannot match you patch file content with default Lucene index configuration.
Make sure that your patch file name is alphabetically after Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config
.
It the problem persists, add type
attribute to defaultLuceneIndexConfiguration
tag and set it to type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider"
:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<indexConfigurations>
<defaultLuceneIndexConfiguration type="Sitecore.ContentSearch.LuceneProvider.LuceneIndexConfiguration, Sitecore.ContentSearch.LuceneProvider">
<fields hint="raw:AddComputedIndexField">
<field fieldName="officelocation" storageType="YES" indexType="TOKENIZED">Computed.Search.OfficeLocationComputedField, Computed</field>
</fields>
</defaultLuceneIndexConfiguration>
</indexConfigurations>
</contentSearch>
</sitecore>
</configuration>
Upvotes: 2