EPiServer has a builtin Content References list property which is used to collect list of pages, blocks, or media. To add new reference, editor could D&D content or click Browse. Clicking Browse button will show dialog with content tree.
By default tree root is set to system Root page. In some scenarios it could be better to show only pages related with the content type. For example in multisite solution we would like to show tree related with the single site. I tried to prepare solution for this.
The change is very simple. We just need to declare new attribute that implements IMetadataAware interface. In OnMetadataCreated method we need to assign roots property for EditorConfiguration settings. The roots will be configured in the AppSettings section of web.config file. The key to the settings will be passed as the attribute constructor parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class CustomContentReferencesRootAttribute : Attribute, IMetadataAware { private readonly string _configurationKeyName; public CustomContentReferencesRootAttribute(string configurationKeyName) { _configurationKeyName = configurationKeyName; } public void OnMetadataCreated(ModelMetadata metadata) { var extendedMetadata = metadata as ExtendedMetadata; if (extendedMetadata == null) { return; } var references = ConfigurationManager.AppSettings[this._configurationKeyName] ?.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries) ?.Select(ContentReference.Parse) ?.Select(r=>r.ToReferenceWithoutVersion()); extendedMetadata.EditorConfiguration["roots"] = references; } } |
Below is the sample web.conig configuration:
1 2 3 4 5 6 7 8 9 10 11 |
<configuration> <!--...--> <appSettings> <add key="SiteOneRoots" value="9,13"/> <add key="SiteTwoRoots" value="40"/> </appSettings> <!--...--> </configuration> |
To use new feature we need to add CustomContentReferencesRoot on the model property.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class TestPage : StandardPage { //... [Display(Name = "List site 1", GroupName = SystemTabNames.Content, Order = 1000)] [CustomContentReferencesRoot("SiteOneRoots")] public virtual IList<ContentReference> TestContentReferences { get; set; } [Display(Name = "List site 2", GroupName = SystemTabNames.Content, Order = 1000)] [CustomContentReferencesRoot("SiteTwoRoots")] public virtual IList<ContentReference> TestContentReferences2 { get; set; } //... } |
And here are dialog windows showed for those two properties: