In the previous post, it was shown how to create a custom rendering with it's rendering variants. This post is extension of it, now on the same rendering new Experience Editor buttons will be added.

Why would you need those?

Experience Editor buttons are used by Content Editors and they give more flexibility in arranging the content: Move, Edit, Insert, etc. But let's say that you have a field on your template for which you don't have the output on the page itself. With Custom Experience Editor Button, you can give your Content Editors direct access to it without having them to leave Experience Editor page.

So how to add Experience Editor Buttons?

They are located on the rendering itself.
/sitecore/layout/Renderings/Feature/Experience Accelerator/Page Content/Article

default experience editor buttons

As an example for a custom button, I used Alex Shyba's Colour Picker which you can read up on and download here. The previously created template is now expanded with one more field: Background Colour

sitecore template

This is how our Article item looks now

item content

Now we need to create a button which will enable the editing of this field. All the buttons are stored in Core database.

You can find them at the following location: /sitecore/content/Applications/WebEdit

The custom ones should be created here: /sitecore/content/Applications/WebEdit/Custom Experience Buttons

Here is how our Background Colour button will look:'

custom experience editor button

Fields section must have the same name as field on the template, in our case that will be Background Colour

Now we can go back to our Master database and add newly created button on our Rendering

sxa rendering experience editor buttons

When we open the page in Experience Editor, we can see our button on the left side.

sxa rendering with custom button

When we click on it, a dialog for the setting of the colour will appear (it comes OOTB with the installation of the Custom Field)

sxa rendering with custom button

Now time to map it all together with some code!

coding hedgehog

In case that you don't have the code from the previous post, you can just reuse the one listed here.

IRepository

public interface IArticleRepository : IVariantsRepository
{
}

Repository

public class ArticleRepository : VariantsRepository, IArticleRepository
{
    private const string BACKGROUND_COLOR_FIELD = "Background Colour";
    private const string DEFAULT_COLOUR = "white";
    public override IRenderingModelBase GetModel()
    {
        ArticleModel model = new ArticleModel();
        FillBaseProperties(model);
        model.BackgroundColour = GetBackgroundColour(Rendering.Item, BACKGROUND_COLOR_FIELD);
        return model;
    }

    private string GetBackgroundColour(Item item, string fieldName)
    {
        InternalLinkField colorField = item.Fields[fieldName];
        if (colorField != null)
        {
            string colour = colorField.Value;
            if (!System.String.IsNullOrEmpty(colour))
            {
                return colour;
            }
        }
        return DEFAULT_COLOUR;
    }
}

Controller

public class ArticleController : StandardController
{
    protected IArticleRepository ArticleRep
    { get; set; }

    public ArticleController()
    {
        ArticleRep = ServiceLocator.Current.Resolve<IArticleRepository>();
    }
    public ActionResult DataSource()
    {
        return PartialView("~/Views/Article/Article.cshtml", GetDataSourceModel());
    }
    protected object GetDataSourceModel()
    {
        return ArticleRep.GetModel();
    }
}

Model

public class ArticleModel : VariantListsRenderingModel
{
    public string BackgroundColour { get; set; }
}

View

@if (Model.DataSourceItem != null || Html.Sxa().IsEdit)
{
<div class="component article @Model.CssClasses.Aggregate()" style="background-color:@Model.BackgroundColour" )>
    <div class="component-content">
        @if (Model.DataSourceItem == null)
        {
            @Model.MessageIsEmpty
        }
        else
        {
            foreach (VariantFieldBase variantField in Model.VariantFields)
            {                    @Html.RenderingVariants().RenderVariant(variantField, Model.Item, Model.RenderingWebEditingParams)
            }
        }
    </div>
</div>
}

Here, the action is defined as DataSource which means that you will need to change it on your SXA Rendering (in previous post it was Index).

When you build your solution and go to Experience Editor, now you will be able to select a colour of your choice which will be automatically rendered on the page.

In this example, the Background Colour is rendered on the Article container level and this is how the output looks:

This is the example of how you can add any type of field on your template and display it to your Content Editors.

Next post will cover manipulation with the content containers and additional customization of the renderings.