Building a Custom Experience Button to Add Items to the SxA Data Folder

One of the best things you can do to improve content author experiences is to add Custom Experience Buttons to your renderings. Whether defining in an Edit Frame or just assigning to the Rendering itself, it gives content authors an intuitive way to get things done in Experience Editor. While Sitecore comes of the out of the box buttons that handle common needs like inserting, moving and inserting items, it is extensible to support things that aren't out of the box.

If you're building custom components for SxA and using a Multilist field to reference Items in the data folder, it would be nice to give Content Authors a way to add a new Item and associate it with that Multilist field in one shot. A custom experience button would be the most convenient way to offer this functionality, however none of the out of the box buttons offer this functionality.

Since Sitecore is Extensible in almost all things, it's not difficult to support this need by creating a custom command and associating that command with your new Custom Experience Button. The code for this command is already located in the Sitecore Foundation SxA branch here.  Configuration to register it is here.  (Command section only)

To make this work, however you'll need three things:

  • The Template ID of the Type of Item you want to create.
  • The Name of the Data Folder you want to place the new Item is.
  • The Field ID of the Multilist or treelist you want to save the reference of the item to.

Then you can define your button by going to the Core Database and creating (or copying) a Custom Experience Button that inherits from "WebEdit Button", set the click to "webedit:addToDataFolder" which is what we registered our command for in the configuration file.

Then use the values mentioned to specify three parameters in the Parameters Name Value List field. Note that the keys: dataFolderName, fieldId, and templateId should be used as indicated.

CEB Parameters for Add to Data Folder.jpg

After that, assign your button to the rendering and you should be good to go. 

Side Note - SxA MultiSiteContext:

In the command, to get the Site Specific Data Folder that makes this truly a SxA feature, we use the SxA MultiSiteContext Service like this:

ServiceLocator.ServiceProvider.GetService<IMultisiteContext>().GetDataItem(item);

This service can be used to get other paths specific to the site, and will come in use. Be sure to add a reference to the SxA Foundation.Multisite assembly to make this work.

Sitecore Foundation for SxA - Cshtml View Variants

Sitecore Foundation - Auto Generate Design Documents