I want to add the insert web edit button but only when the current rendering has a datasource item of a specific type. I see two ways to do this. I can add an edit frame to the view that shows up if the datasource item is not null. This would mean that there would basically be 2 frames for the rendering though, the normal one that goes around renderings and the one I'm adding manually. The other option is to create a custom insert button. Currently I'm attempting to do this and overriding the QueryState however no matter what the CommandContext passed in seems to have items. Is this even a good idea?

The ultimate goal is that I want to use the insert button but if a content editor removes the datasource then the insert option will target the current page. If I can change the insert button to behave like the button that adds or changes associated content that would be even better. If this is something that can be done out of the box I'd like to know the way to do that.

1 Answer 1


Here's my current implementation. It consists of a command, patch and web edit command button.

Current command button code:

public class NewIfDatasourceExists : global::Sitecore.Shell.Applications.WebEdit.Commands.New
    public override CommandState QueryState(CommandContext context)
        var db = context.Items?.FirstOrDefault()?.Database;

        if (db == null)
            return CommandState.Hidden;

        var renderingId = context.Parameters["renderingId"];

        if (!ID.IsID(renderingId))
            return CommandState.Hidden;

        var rendering = db.GetItem(new ID(renderingId));

        if (rendering == null)
            return CommandState.Hidden;

        var datasourceTemplateField = FieldTypeManager.GetField(rendering.Fields["Datasource Template"]) as global::Sitecore.Data.Fields.InternalLinkField;

        if (datasourceTemplateField?.TargetItem == null)
            return CommandState.Hidden;

        var hasItemOfDatasourceType = context.Items.Any(item => item.InheritsFrom(datasourceTemplateField.TargetID));

        if (!hasItemOfDatasourceType)
            return CommandState.Hidden;

        return base.QueryState(context);

Config with custom command:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <command name="custom:newifdatasourceexists" type="CustomCode.Sitecore.Shell.Application.WebEdit.Commands.NewIfDatasourceExists, CustomCode" />

Lastly, I duplicated the normal insert button into the custom experience editor button folder and changed the command on that to use my command and added that to my rendering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.