I am adding custom fields in solr schema in Sitecore 10.3. But it will be for specific index.
How can i get index name in the class which is inherited from IPopulateHelperFactory refer this link to add custom code
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityI am adding custom fields in solr schema in Sitecore 10.3. But it will be for specific index.
How can i get index name in the class which is inherited from IPopulateHelperFactory refer this link to add custom code
Index name is not passed OOTB to SchemaPopulateHelper
class.
However there is a workaround using Sitecore Switcher
class.
Switcher
class with PopulateManagedSchemaArgs
type. This class does not need any method. It should be like:public class PopulateManagedSchemaArgsSwitcher
: Switcher<Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema.PopulateManagedSchemaArgs>
{
}
PopulateManagedSchemaProcessor
, one that will enter the switcher with current PopulateManagedSchemaArgs
, and one that will exit the switcher:public class EnterPopulateManagedSchemaArgsSwitcher : PopulateManagedSchemaProcessor
{
public override void Process(PopulateManagedSchemaArgs args)
{
PopulateManagedSchemaArgsSwitcher.Enter(args);
}
}
public class ExitPopulateManagedSchemaArgsSwitcher : PopulateManagedSchemaProcessor
{
public override void Process(PopulateManagedSchemaArgs args)
{
PopulateManagedSchemaArgsSwitcher.Exit();
}
}
CustomPopulateHelper
class that will override default SchemaPopulateHelper
class, and in the GetAllFields
method it will read index and core names from the switcher:public class CustomPopulateHelper : SchemaPopulateHelper
{
public CustomPopulateHelper(SolrSchema solrSchema) : base(solrSchema)
{
}
public override IEnumerable<XElement> GetAllFields()
{
var args = PopulateManagedSchemaArgsSwitcher.CurrentValue;
var indexName = args?.Index?.Name;
var cores = args?.Cores?.ToList();
var baseFields = base.GetAllFields().ToList();
if (indexName == "my_custom_index")
{
baseFields.Add(CreateField("my_custom_field", "text_general", false, true, false, false, false, false, false, false));
}
return baseFields;
}
}
From what I can see, there is always single core in args.Cores
and that core name is equal to args.Index.Name
. I'm sure there may be specific case when it will be different, but I'm not sure when and why.
CustomPopulateHelper
instead of default one:public class CustomPopulateHelperFactory : IPopulateHelperFactory
{
public ISchemaPopulateHelper GetPopulateHelper(SolrSchema solrSchema)
{
return new CustomPopulateHelper(solrSchema);
}
}
And the last thing is config file which binds everything together:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
<sitecore role:require="Standalone or ContentManagement or ContentDelivery" search:require="solr">
<pipelines>
<contentSearch.PopulateSolrSchema>
<processor type="Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema.ResolveCoreNames, Sitecore.ContentSearch.SolrProvider" />
<processor
type="MyAssembly.MyNamespace.EnterPopulateManagedSchemaArgsSwitcher, MyAssembly"
patch:before="processor[@type='Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema.PopulateFields, Sitecore.ContentSearch.SolrProvider']">
</processor>
<processor type="Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema.PopulateFields, Sitecore.ContentSearch.SolrProvider">
<param
type="MyAssembly.MyNamespace.CustomPopulateHelperFactory, MyAssembly"
patch:instead="*[@type='Sitecore.ContentSearch.SolrProvider.Factories.DefaultPopulateHelperFactory']"/>
</processor>
<processor
type="MyAssembly.MyNamespace.ExitPopulateManagedSchemaArgsSwitcher, MyAssembly"
patch:after="processor[@type='Sitecore.ContentSearch.SolrProvider.Pipelines.PopulateSolrSchema.PopulateFields, Sitecore.ContentSearch.SolrProvider']">
</processor>
</contentSearch.PopulateSolrSchema>
</pipelines>
</sitecore>
</configuration>
managed-schema
of the index: