Sitecore's Headless ASP.NET Core Rendering SDK provides a powerful platform for building content-rich web applications. In this blog-post, I'll explore how to leverage this SDK to create a custom Sitecore Edit Frame Tag Helper. This tag helper allows you to render an HTML edit frame for a Sitecore item, providing a seamless editing experience for content authors.
What is an Edit Frame?
Edit Frames are a valuable feature in Sitecore's Experience Editor. They offer content authors front-end access to fields that might not be directly accessible. These fields could be hidden from view or of a type that isn't directly editable from the front end. Edit Frames provide a user-friendly interface for making changes to such fields within the context of a webpage, ensuring a smooth content editing process.
It's worth noting that Edit Frames are not part of the default Sitecore's Headless ASP.NET Core Rendering SDK. However, by creating a custom Sitecore Edit Frame Tag Helper, you can integrate this feature seamlessly into your projects.
Prerequisites
Before I'll dive into creating the custom Sitecore Edit Frame Tag Helper, make sure you have the following prerequisites in place:
- A project that includes Sitecore's Headless ASP.NET Core Rendering SDK.
- Familiarity with Tag Helpers and Razor views in ASP.NET Core.
Creating the Custom Sitecore Edit Frame Tag Helper
The custom Sitecore Edit Frame Tag Helper is implemented as a C# class. Below is the complete code for this tag helper:
| 1 | using System.Collections.Generic; |
| 2 | using System.Linq; |
| 3 | using System.Reflection; |
| 4 | using Microsoft.AspNetCore.Mvc.Rendering; |
| 5 | using Microsoft.AspNetCore.Mvc.ViewFeatures; |
| 6 | using Microsoft.AspNetCore.Razor.TagHelpers; |
| 7 | using Newtonsoft.Json; |
| 8 | using Newtonsoft.Json.Serialization; |
| 9 | using Sitecore.AspNet.RenderingEngine; |
| 10 | using Sitecore.AspNet.RenderingEngine.Binding.Attributes; |
| 11 | using Sitecore.LayoutService.Client.Response.Model.Fields; |
| 12 | |
| 13 | namespace YourNamespace |
| 14 | { |
| 15 | [HtmlTargetElement("custom-sc-edit-frame", Attributes = "asp-commands", TagStructure = TagStructure.NormalOrSelfClosing)] |
| 16 | public class CustomEditFrameTagHelper : TagHelper |
| 17 | { |
| 18 | [HtmlAttributeNotBound] |
| 19 | [ViewContext] |
| 20 | public ViewContext? ViewContext { get; set; } |
| 21 | |
| 22 | [HtmlAttributeName("asp-model")] |
| 23 | public ModelExpression? ItemModel { get; set; } |
| 24 | |
| 25 | [HtmlAttributeName("asp-id")] |
| 26 | public string? DataId { get; set; } |
| 27 | |
| 28 | [HtmlAttributeName("asp-fields")] |
| 29 | public string? Fields { get; set; } |
| 30 | |
| 31 | [HtmlAttributeName("asp-commands")] |
| 32 | public string? Commands { get; set; } |
| 33 | |
| 34 | private string? ItemId { get; set; } |
| 35 | private ItemLinkField? Item { get; set; } |
| 36 | |
| 37 | public override void Process(TagHelperContext context, TagHelperOutput output) |
| 38 | { |
| 39 | output.TagName = string.Empty; |
| 40 | |
| 41 | var renderingContext = ViewContext?.HttpContext?.GetSitecoreRenderingContext()?.Response?.Content?.Sitecore; |
| 42 | if (renderingContext == null) |
| 43 | return; |
| 44 | |
| 45 | //Only add edit frame for experience editor. |
| 46 | if (!(renderingContext.Context?.IsEditing ?? false)) |
| 47 | return; |
| 48 | |
| 49 | if (string.IsNullOrWhiteSpace(Commands)) |
| 50 | return; |
| 51 | |
| 52 | //Can either pass in item id as data-id attribute or ItemLinkField model as data-model attribute |
| 53 | var item = (ItemLinkField?)ItemModel?.Model; |
| 54 | Item = item; |
| 55 | ItemId = !string.IsNullOrWhiteSpace(DataId) ? DataId : item?.Id.ToString(); |
| 56 | |
| 57 | if (string.IsNullOrWhiteSpace(ItemId)) return; |
| 58 | |
| 59 | var lang = renderingContext.Context?.Language; |
| 60 | var database = renderingContext.Route?.DatabaseName; |
| 61 | |
| 62 | var contextItemUri = $"sitecore://{database}/{ItemId}?lang={lang}"; |
| 63 | |
| 64 | output.Content.AppendHtml($"<div class='scLooseFrameZone scEnabledChrome' sc_item='{contextItemUri}' sc-part-of='editframe'>"); |
| 65 | output.Content.AppendHtml($"<span class='scChromeData'>{GetChromeContent(contextItemUri)}</span>"); |
| 66 | |
| 67 | var innerContent = output.GetChildContentAsync().Result.GetContent(); |
| 68 | output.Content.AppendHtml(innerContent); |
| 69 | |
| 70 | output.Content.AppendHtml("</div>"); |
| 71 | } |
| 72 | |
| 73 | private string GetChromeContent(string contextItemUri) |
| 74 | { |
| 75 | var commands = new List<ClickCommand?>(); |
| 76 | var requestedCommands = Commands?.Split("|").ToList() ?? new List<string>(); |
| 77 | |
| 78 | if (requestedCommands.Contains(Constants.Commands.Edit)) |
| 79 | commands.Add(GetEditFieldsCommand()); |
| 80 | |
| 81 | if (requestedCommands.Contains(Constants.Commands.Insert)) |
| 82 | commands.Add(GetInsertItemCommand()); |
| 83 | |
| 84 | if (requestedCommands.Contains(Constants.Commands.Delete)) |
| 85 | commands.Add(GetDeleteItemCommand()); |
| 86 | |
| 87 | if (requestedCommands.Contains(Constants.Commands.Move)) |
| 88 | { |
| 89 | commands.Add(GetMoveUpCommand()); |
| 90 | commands.Add(GetMoveDownCommand()); |
| 91 | } |
| 92 | |
| 93 | var chromeContent = new ChromeContent |
| 94 | { |
| 95 | Commands = commands, |
| 96 | ContextItemUrl = contextItemUri, |
| 97 | Custom = new ChromeContentCustomAttributes(), |
| 98 | DisplayName = "Edit Item Properties" |
| 99 | }; |
| 100 | |
| 101 | var content = JsonConvert.SerializeObject(chromeContent, new JsonSerializerSettings |
| 102 | { |
| 103 | ContractResolver = new DefaultContractResolver |
| 104 | { |
| 105 | NamingStrategy = new CamelCaseNamingStrategy() |
| 106 | } |
| 107 | }); |
| 108 | |
| 109 | return content; |
| 110 | } |
| 111 | |
| 112 | private ClickCommand? GetEditFieldsCommand() |
| 113 | { |
| 114 | //Can either provide pipe separated list of fields in data-fields attribute or get them from item passed in data-model |
| 115 | var fields = !string.IsNullOrWhiteSpace(Fields) ? Fields : GetFields(Item); |
| 116 | |
| 117 | if (string.IsNullOrWhiteSpace(fields)) |
| 118 | return null; |
| 119 | |
| 120 | var clickCommandAction = $"webedit:fieldeditor(id={ItemId},command={Constants.EditFrameCommandIds.EditComponentProperties},fields={fields})"; |
| 121 | var command = new ClickCommand |
| 122 | { |
| 123 | Click = $"javascript:Sitecore.PageModes.PageEditor.postRequest('{clickCommandAction}',null,false)", |
| 124 | Header = "Edit Fields", |
| 125 | Icon = "/temp/iconcache/office/16x16/pencil.png", |
| 126 | DisabledIcon = "/temp/pencil_disabled16x16.png", |
| 127 | IsDivider = false, |
| 128 | Tooltip = "", |
| 129 | Type = null |
| 130 | }; |
| 131 | |
| 132 | return command; |
| 133 | } |
| 134 | |
| 135 | private ClickCommand GetInsertItemCommand() |
| 136 | { |
| 137 | var action = $"webedit:new(Navigate=0,id={ItemId})"; |
| 138 | var command = new ClickCommand |
| 139 | { |
| 140 | Click = $"javascript:Sitecore.PageModes.PageEditor.postRequest('{action}',null,false)", |
| 141 | Header = "Insert New Item", |
| 142 | Icon = "/temp/iconcache/office/16x16/insert_from_template.png", |
| 143 | DisabledIcon = "/temp/insert_from_template_disabled16x16.png", |
| 144 | IsDivider = false, |
| 145 | Tooltip = "Insert a new item", |
| 146 | Type = null |
| 147 | }; |
| 148 | |
| 149 | return command; |
| 150 | } |
| 151 | |
| 152 | private ClickCommand GetDeleteItemCommand() |
| 153 | { |
| 154 | var action = $"webedit:delete(id={ItemId})"; |
| 155 | var command = new ClickCommand |
| 156 | { |
| 157 | Click = $"javascript:Sitecore.PageModes.PageEditor.postRequest('{action}',null,false)", |
| 158 | Header = "Delete Link", |
| 159 | Icon = "/temp/iconcache/office/16x16/delete.png", |
| 160 | DisabledIcon = "/temp/delete_disabled16x16.png", |
| 161 | IsDivider = false, |
| 162 | Tooltip = "Delete the item", |
| 163 | Type = null |
| 164 | }; |
| 165 | |
| 166 | return command; |
| 167 | } |
| 168 | |
| 169 | private ClickCommand GetMoveUpCommand() |
| 170 | { |
| 171 | var action = $"item:moveup(id={ItemId})"; |
| 172 | var command = new ClickCommand |
| 173 | { |
| 174 | Click = $"javascript:Sitecore.PageModes.PageEditor.postRequest('{action}',null,false)", |
| 175 | Header = "Move Up", |
| 176 | Icon = "/temp/iconcache/office/16x16/navigate_up.png", |
| 177 | DisabledIcon = "/temp/navigate_up_disabled16x16.png", |
| 178 | IsDivider = false, |
| 179 | Tooltip = "Move the item up", |
| 180 | Type = null |
| 181 | }; |
| 182 | |
| 183 | return command; |
| 184 | } |
| 185 | |
| 186 | private ClickCommand GetMoveDownCommand() |
| 187 | { |
| 188 | var action = $"item:movedown(id={ItemId})"; |
| 189 | var command = new ClickCommand |
| 190 | { |
| 191 | Click = $"javascript:Sitecore.PageModes.PageEditor.postRequest('{action}',null,false)", |
| 192 | Header = "Move Down", |
| 193 | Icon = "/temp/iconcache/office/16x16/navigate_down.png", |
| 194 | DisabledIcon = "/temp/navigate_down_disabled16x16.png", |
| 195 | IsDivider = false, |
| 196 | Tooltip = "Move the item down", |
| 197 | Type = null |
| 198 | }; |
| 199 | |
| 200 | return command; |
| 201 | } |
| 202 | |
| 203 | private string GetFields(ItemLinkField? item) |
| 204 | { |
| 205 | if (item == null) return string.Empty; |
| 206 | |
| 207 | var type = item.GetType().GetGenericArguments()[0]; |
| 208 | |
| 209 | var fields = type.GetProperties().Select(GetPropertyName).Where(i => !string.IsNullOrWhiteSpace(i)); |
| 210 | |
| 211 | return string.Join("|", fields); |
| 212 | } |
| 213 | |
| 214 | private string GetPropertyName(PropertyInfo? propertyInfo) |
| 215 | { |
| 216 | if (propertyInfo == null) return string.Empty; |
| 217 | |
| 218 | var attribute = propertyInfo.GetCustomAttribute<SitecoreComponentFieldAttribute>(); |
| 219 | if (!string.IsNullOrWhiteSpace(attribute?.Name)) |
| 220 | return attribute.Name; |
| 221 | |
| 222 | return propertyInfo.Name; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | public class ChromeContent |
| 227 | { |
| 228 | public List<ClickCommand?>? Commands { get; set; } |
| 229 | public string? ContextItemUrl { get; set; } |
| 230 | public ChromeContentCustomAttributes? Custom { get; set; } |
| 231 | public string? DisplayName { get; set; } |
| 232 | public string? ExpandedDisplayName { get; set; } |
| 233 | } |
| 234 | |
| 235 | public class ClickCommand |
| 236 | { |
| 237 | public string? Click { get; set; } |
| 238 | public string? Header { get; set; } |
| 239 | public string? Icon { get; set; } |
| 240 | public string? DisabledIcon { get; set; } |
| 241 | public bool IsDivider { get; set; } |
| 242 | public string? Tooltip { get; set; } |
| 243 | public string? Type { get; set; } |
| 244 | } |
| 245 | |
| 246 | public class ChromeContentCustomAttributes |
| 247 | { |
| 248 | } |
| 249 | } |
This code defines a 'CustomEditFrameTagHelper' class that inherits from the _'TagHelper'_base class. The tag helper has several attributes and properties, including 'asp-commands', 'asp-model', 'asp-id', 'asp-fields', and 'asp-commands', which allow you to customize the Edit Frame's behavior and appearance.
Inside the 'Process' method, the code checks if the page is being edited in the Sitecore Experience Editor, and if so, it generates the HTML for the Edit Frame.
The 'GetChromeContent' method is used to create the Edit Frame's configuration, specifying which commands should be available in the frame. The_'ClickCommand'_ class defines individual commands within the Edit Frame, such as "Edit Fields", "Insert New Item", "Delete Link" and "Move Up/Down".
Using the Custom Sitecore Edit Frame Tag Helper
To use the custom Sitecore Edit Frame Tag Helper in your views, follow these steps:
- Make sure the 'CustomEditFrameTagHelper' class is included in your project.
- In your Razor view, add the 'custom-sc-edit-frame' tag and set the necessary attributes, such as 'asp-commands', 'asp-model', and 'asp-fields'. For example:
| 1 | <custom-sc-edit-frame asp-commands="Edit|Insert|Delete|Move" asp-model="yourModel" asp-fields="field1|field2"></custom-sc-edit-frame> |
Ensure that you're running the application in the Sitecore Experience Editor to see the Edit Frame in action.
What Happens When You Don't Pass Field Names?
If you choose not to pass field names in the 'asp-fields' attribute, the custom Sitecore Edit Frame Tag Helper will automatically generate a list of fields to be edited based on the 'asp-model' provided. This is a convenient way to include all available fields for editing without explicitly specifying them.
Here's an example without the 'asp-fields' attribute:
| 1 | <custom-sc-edit-frame asp-commands="Edit|Insert|Delete|Move" asp-model="yourModel"></custom-sc-edit-frame> |
In this case, the Edit Frame will dynamically include all fields from the yourModel object for editing, simplifying the configuration process.
Conclusion
Creating a custom Sitecore Edit Frame Tag Helper with Sitecore's Headless ASP.NET Core Rendering SDK can enhance your content editing experience by providing a tailored interface for managing content within the Sitecore platform. This tag helper allows you to define which commands should be available for editing and, if needed, automatically include all fields for editing when not explicitly specified.
By following the steps outlined in this blog post, you can integrate the custom Sitecore Edit Frame Tag Helper into your application and take advantage of Sitecore's powerful content management features.
Happy Sitecore-ing!
–Robbert








