Last active
October 5, 2021 14:05
-
-
Save AThraen/9219e3410e43c248e6d6733a6ca5dafc to your computer and use it in GitHub Desktop.
Split up Blocks and Media folder roots in Optimizely (Episerver)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using EPiServer.Cms.Shell.UI.UIDescriptors; | |
| using EPiServer.Framework; | |
| using EPiServer.Framework.Initialization; | |
| using EPiServer.ServiceLocation; | |
| using EPiServer.ServiceLocation.Compatibility; | |
| using EPiServer.Shell; | |
| using System; | |
| using System.Linq; | |
| namespace ExperimentSite.Widgets | |
| { | |
| [InitializableModule] | |
| [ModuleDependency(typeof(EPiServer.Web.InitializationModule))] | |
| public class InterceptorInit : IConfigurableModule | |
| { | |
| public void ConfigureContainer(ServiceConfigurationContext context) | |
| { | |
| //Override / remove | |
| context.Services.Intercept<IContentRepositoryDescriptor>( | |
| (locator, def) => (def is BlockRepositoryDescriptor) ? | |
| locator.GetInstance<MyBlockRepositoryDescriptor>() : | |
| (def is MediaRepositoryDescriptor) ? locator.GetInstance<MyMediaRepositoryDescriptor>() : def | |
| ); | |
| } | |
| public void Initialize(InitializationEngine context) | |
| { | |
| //Add initialization logic, this method is called once after CMS has been initialized | |
| } | |
| public void Uninitialize(InitializationEngine context) | |
| { | |
| //Add uninitialization logic | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Web; | |
| using EPiServer; | |
| using EPiServer.Cms.Shell.UI.CompositeViews.Internal; | |
| using EPiServer.Cms.Shell.UI.UIDescriptors; | |
| using EPiServer.Core; | |
| using EPiServer.Framework.Localization; | |
| using EPiServer.ServiceLocation; | |
| using EPiServer.Shell; | |
| using EPiServer.Web; | |
| namespace ExperimentSite.Widgets | |
| { | |
| public class MyBlockRepositoryDescriptor : BlockRepositoryDescriptor | |
| { | |
| private const string NAME = "Blocks"; | |
| private readonly IContentRepository _repo; | |
| private ContentReference _root; | |
| public MyBlockRepositoryDescriptor(IContentRepository repo) | |
| { | |
| _repo = repo; | |
| //Get or create root | |
| ContentFolder rootfolder = _repo.GetChildren<ContentFolder>(ContentReference.GlobalBlockFolder).Where(f => f.Name == NAME).FirstOrDefault(); | |
| if (rootfolder == null) | |
| { | |
| rootfolder = _repo.GetDefault<ContentFolder>(ContentReference.GlobalBlockFolder); | |
| rootfolder.Name = NAME; | |
| _root = _repo.Save(rootfolder, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess); | |
| } | |
| else _root = rootfolder.ContentLink; | |
| } | |
| public override IEnumerable<ContentReference> Roots | |
| { | |
| get | |
| { | |
| yield return _root; | |
| } | |
| } | |
| } | |
| public class MyMediaRepositoryDescriptor : MediaRepositoryDescriptor | |
| { | |
| private const string NAME = "Media"; | |
| private readonly IContentRepository _repo; | |
| private ContentReference _root; | |
| public MyMediaRepositoryDescriptor(IContentRepository repo) | |
| { | |
| _repo = repo; | |
| //Get or create root | |
| ContentFolder rootfolder = _repo.GetChildren<ContentFolder>(ContentReference.GlobalBlockFolder).Where(f => f.Name == NAME).FirstOrDefault(); | |
| if (rootfolder == null) | |
| { | |
| rootfolder = _repo.GetDefault<ContentFolder>(ContentReference.GlobalBlockFolder); | |
| rootfolder.Name = NAME; | |
| _root = _repo.Save(rootfolder, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess); | |
| } | |
| else _root = rootfolder.ContentLink; | |
| } | |
| public override IEnumerable<ContentReference> Roots | |
| { | |
| get | |
| { | |
| yield return _root; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment