Sitecore Content Search Computed Field Pass Custom Variables
One requirement of Content Search i've required recently, is the ability to pass in custom variables from the computed field config element. I needed this so multiple sites can use the same computed fields, but inject settings unique to that site. For example passing in the site name to generate site specific urls.
Out of the box Sitecore allows you to specify a fieldName in the computed field and this populates a property within you computed field class. This is the feature I wanted to extend to pass in my own properties into the class.
So below is an example computed field class that uses this method. The computed field generates url's for items using the LinkManager class but takes in the site context from the computed field config.
The class implements the AbstractComputedIndexField base class. On the constructor, the XmlNode of the computed field is passed in and using XmlUtil.GetAttribute("siteName", configurationNode); we can read XML elements of the config and populate our properties in our computed field.
I'm tapping into the standard urllink property within the index configuration, this means when inheriting from SearchResultItem base class the property Url is populated. This is normally null by default, Ehab ElGindy also talks about the Url being null.