Created
December 7, 2018 10:35
-
-
Save alasvant/1804f2afc307b1792946272ae77e5247 to your computer and use it in GitHub Desktop.
Sample code, Episerver initialization module to hide content types which you don't own.
This file contains 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 EPiServer.DataAbstraction; | |
using EPiServer.Framework; | |
using EPiServer.Framework.Initialization; | |
using EPiServer.Logging; | |
namespace AlloyWithFind.Business.Initialization | |
{ | |
[InitializableModule] | |
[ModuleDependency(typeof(EPiServer.Web.InitializationModule), typeof(EPiServer.Forms.InitializationModule))] | |
public class HideContentTypesInitialization : IInitializableModule | |
{ | |
private static readonly ILogger Logger = LogManager.GetLogger(typeof(HideContentTypesInitialization)); | |
/// <summary> | |
/// Customized search block Guid. | |
/// </summary> | |
public static readonly Guid SearchBlockGuid = new Guid("3a263af2-79d9-4275-ad15-3c3187c89870"); | |
/// <summary> | |
/// Episerver Forms captcha element Guid. | |
/// </summary> | |
public static readonly Guid CaptchaBlockGuid = new Guid("0cffa8b6-3c20-4b97-914f-75a624b19bff"); | |
public void Initialize(InitializationEngine context) | |
{ | |
// get content types repository | |
IContentTypeRepository ctRepo = context.Locate.ContentTypeRepository(); | |
HideFromEditors(ctRepo, SearchBlockGuid); | |
HideFromEditors(ctRepo, CaptchaBlockGuid); | |
} | |
private static void HideFromEditors(IContentTypeRepository repo, Guid contentGuid) | |
{ | |
if (repo == null) | |
{ | |
throw new ArgumentNullException(nameof(repo)); | |
} | |
// get content with Guid (returns null if not found) | |
ContentType content = repo.Load(contentGuid); | |
if (content != null) | |
{ | |
if (content.IsAvailable) | |
{ | |
try | |
{ | |
// make writable clone, hide and save it | |
ContentType writable = content.CreateWritableClone() as ContentType; | |
writable.IsAvailable = false; | |
repo.Save(writable); | |
Logger.Information($"Content type with guid '{contentGuid}' (name: {writable.Name}) hidden from editors."); | |
} | |
catch (Exception ex) | |
{ | |
Logger.Error($"Failed to hide content type with guid '{contentGuid}' (name: {content.Name}).", ex); | |
} | |
} | |
else | |
{ | |
Logger.Information($"Content type with guid '{contentGuid}' (name: {content.Name}) is already hidden from editors."); | |
} | |
} | |
else | |
{ | |
Logger.Warning($"Cannot hide content type with guid '{contentGuid}' because it was not found from content type repository."); | |
} | |
} | |
public void Uninitialize(InitializationEngine context) | |
{ | |
//Add uninitialization logic | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment