Need some C# help inside your SXA rendering variant? You’ve come to the right place. This blog post will explain how you can employ the use of a C# helper class in your rendering variant. You should only use this sparingly. If you find yourself overusing C# here, you should consider implementing your component as a custom SXA component rather than an SXA rendering variant. That being said, let’s walk through creating a nice example helper class you can use in Sitecore.
The helper class
namespace Sample.Foundation.Variants.NVelocityExtensions { public class ImageTool { public string GetImageAlt(Item item, string field) { ImageField imgField = item.Fields[field]; return imgField.Alt; } } }
The above class will be used to retrieve the alt text on an an uploaded image in an image field. The method inside it simply requires the datasource item being used and the field in which the image lives in to get the alt text.
Making our helper available in Sitecore
namespace Sample.Foundation.Variants.Pipelines.GetVelocityTemplateRenderers { public class SampleGetTemplateRenderersPipelineProcessor : IGetTemplateRenderersPipelineProcessor { private readonly IMultisiteContext _multisiteContext; public SampleGetTemplateRenderersPipelineProcessor(IMultisiteContext multisiteContext) { _multisiteContext = multisiteContext; } public void Process(GetTemplateRenderersPipelineArgs args) { var tenant = _multisiteContext.GetTenantItem(Sitecore.Context.Item); if (tenant.Name.ToLower() != "<your tenant name>") return; args.Context.Put("imageTool", new ImageTool()); } } }
You will need to create a class that implements SXA’s IGetTemplateRenderersPipelineProcessor interface. This interface requires you to implement the Process method. Use the args.Context.Put() method to specify the name you will want to use to reference your helper class in Sitecore. My example will reference the helper class as “imageTool”.
If you are in a multi-tenant solution, you may want to make your helper class only available in a specific tenant. If that is case, be sure to insert the name of your tenant in the first two lines of the Process() method that checks what tenant can use this helper code. If you want your code to be able to be used in the entire Sitecore solution regardless of tenant, simply remove those now unnecessary two lines. You can also remove the constructor since you will no longer need to inject the MultisiteContext used to check which tenant you are in.
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> <sitecore> <pipelines> <getVelocityTemplateRenderers> <processor type="Sample.Foundation.Variants.Pipelines.GetVelocityTemplateRenderers.SampleAddTemplateRenderers, Sample.Foundation.Variants" resolve="true" patch:after="processor[@type='Sitecore.XA.Foundation.Variants.Abstractions.Pipelines.GetVelocityTemplateRenderers.InitializeVelocityContext, Sitecore.XA.Foundation.Variants.Abstractions']" /> </getVelocityTemplateRenderers> </pipelines> </sitecore> </configuration>
After creating the two C# classes you need, you will then need to patch in your processor after the InitializeVelocityContext process. If you removed the constructor, you can remove the resolve attribute in the patch file above.
Using the helper in Sitecore
Inside your rendering variant, insert a “Template”.
Since I specified the name of the tool to be “imageTool” in the pipeline process, I can reference it by using “$imageTool” in the rendering variant. $item represents the datasource item being used. “Image” is the name of the Image Field that content authors are uploading their images into.
Conclusion
The ability to use C# helpers in your SXA rendering variants really extends the possibilities and use cases for rendering variants. Want to enforce internal links to open in the current window and enforce external links to open in a new window? Try writing a LinkTool class that can do this. I hope this blog post helps improve your SXA site!