Skip to content

Instantly share code, notes, and snippets.

@AThraen
Created October 11, 2018 18:43
Show Gist options
  • Select an option

  • Save AThraen/7bbc7957a86c61b351b2ad20ebab4eef to your computer and use it in GitHub Desktop.

Select an option

Save AThraen/7bbc7957a86c61b351b2ad20ebab4eef to your computer and use it in GitHub Desktop.
Manage Content Type Suggestions in Admin Mode in Episerver
using EPiServer;
using EPiServer.Cms.Shell.UI.Rest;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.Personalization;
using EPiServer.ServiceLocation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CodeArt.Addons.ContentTypeSuggestions
{
[ServiceConfiguration(typeof(IContentTypeAdvisor))]
public class CustomContentRecommendations : IContentTypeAdvisor
{
public IContentRepository repo { get; set; }
public IContentTypeRepository typerepo { get; set; }
public CustomContentRecommendations(IContentRepository repo, IContentTypeRepository trepo)
{
this.repo = repo;
typerepo = trepo;
}
//Add custom recommendations where needed.
public IEnumerable<int> GetSuggestions(IContent parent, bool contentFolder, IEnumerable<string> requestedTypes)
{
foreach(var s in TypeSuggestion.List(parent.ContentTypeID, parent.ContentLink.ToReferenceWithoutVersion(), EPiServerProfile.Current.UserName))
{
if (contentFolder == s.ContentFolder)
{
yield return s.TypeToSuggest;
}
}
}
}
}
<%@ Page Language="c#" Codebehind="SuggestedTypesAdmin.aspx.cs" AutoEventWireup="False" EnableViewState="true" Inherits="CodeArt.Addons.ContentTypeSuggestions.SuggestedTypesAdmin" Title="" %>
<%@ Register TagPrefix="EPiServer" Namespace="EPiServer.Web.PropertyControls" Assembly="EPiServer.Cms.AspNet" %>
<%@ Register TagPrefix="EPiServerUI" Namespace="EPiServer.UI.WebControls" Assembly="EPiServer.UI" %>
<asp:Content ContentPlaceHolderID="MainRegion" runat="server">
<div class="epi-formArea">
<fieldset>
<legend>
<asp:Literal Text="New Custom Type Suggestion" runat="server"/>
</legend>
<div class="epi-size10">
<div>
<asp:Label runat="server" AssociatedControlID="pageTypeList" Text="Parent Content Type" />
<asp:DropDownList ID="pageTypeList" runat="server" />
</div>
<div>
<asp:label runat="server" Text="Select specific page" AssociatedControlID="parentRef" />
<EPiServer:InputPageReference CssClass="epiinlineinputcontrol" ID="parentRef" runat="server" DisableCurrentPageOption="true"/><br />
</div>
<div>
<asp:Label runat="server" AssociatedControlID="justForMe" Text="Only suggest this for me" />
<asp:CheckBox runat="server" ID="justForMe"/>
</div>
<div>
<asp:Label runat="server" AssociatedControlID="contentFolder" Text="Is this a 'For This Content' folder?" />
<asp:CheckBox runat="server" ID="contentFolder"/>
</div>
<div>
<asp:Label runat="server" AssociatedControlID="TypesToSuggest" Text="Content Type to Suggest" />
<asp:DropDownList runat="server" ID="TypesToSuggest" />
</div>
</div>
</fieldset>
<div class="epitoolbuttonrow">
<EPiServerUI:ToolButton runat="server" Text="Add" ToolTip="" OnClick="AddBtn_Click" Id="AddBtn" />
</div>
</div>
<div class="epi-formArea" ID="TypeListDiv" runat="server">
<asp:GridView
ID="TypeList"
Runat="server"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Parent Content Type">
<ItemTemplate>
<%#: GetContentTypeName((int)DataBinder.Eval(Container.DataItem, "ParentType"))%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Parent">
<ItemTemplate>
<%# GetContentName((EPiServer.Core.ContentReference)DataBinder.Eval(Container.DataItem, "ParentReference"))%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Only for user">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "User")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Content Folder">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ContentFolder")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Content Type to Suggest">
<ItemTemplate>
<%# GetContentTypeName((int)DataBinder.Eval(Container.DataItem, "TypeToSuggest"))%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<EPiServerUI:ToolButton EnableClientConfirm="true" ID="DeleteToolButton" CommandName="Delete" SkinID="Delete"
ToolTip="<%$ Resources: EPiServer, button.delete %>" Enabled="true" OnCommand="DeleteToolButton_Command" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"Id") %>' runat="server" ConfirmMessage="Do you really want to delete this type suggestion?" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Web.Security;
using System.Web.UI.WebControls;
using EPiServer.Personalization;
using EPiServer.PlugIn;
using EPiServer.Security;
using EPiServer.Util.PlugIns;
using System.Web.UI;
using EPiServer.UI;
using EPiServer.ServiceLocation;
using EPiServer.DataAbstraction;
using System.Linq;
using EPiServer.Core;
using EPiServer;
using EPiServer.Data;
namespace CodeArt.Addons.ContentTypeSuggestions
{
[GuiPlugIn(DisplayName = "Suggested Types Manager", Description = "", Area = PlugInArea.AdminMenu, UrlFromUi ="../CodeArt.Addons.ContentTypeSuggestions/SuggestedTypesAdmin.aspx")]
public partial class SuggestedTypesAdmin : EPiServer.Shell.WebForms.WebFormsBase
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.EnableViewState = true;
this.Title = "Suggested Types Manager";
this.SystemMessageContainer.Heading = "Suggested Types Manager";
if (!IsPostBack)
{
var trepo = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
this.pageTypeList.DataSource = new ListItem[] { new ListItem("(All types)", "-1") }
.Union(trepo.List().OrderBy(c => c.FullName).Select(pt => new ListItem(pt.FullName, pt.ID.ToString())));
this.pageTypeList.DataValueField = "Value";
this.pageTypeList.DataTextField = "Text";
this.TypesToSuggest.DataSource = trepo.List().OrderBy(c => c.FullName).Select(pt => new ListItem(pt.FullName, pt.ID.ToString()));
this.TypesToSuggest.DataValueField = "Value";
this.TypesToSuggest.DataTextField = "Text";
//Show existing list
this.TypeList.DataSource = TypeSuggestion.ListForUser(EPiServerProfile.Current.UserName);
this.DataBind();
}
}
public string GetContentTypeName(int ID)
{
if (ID == -1) return "";
var trepo = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
return trepo.Load(ID).FullName;
}
public string GetContentName(ContentReference cref)
{
if (cref == ContentReference.EmptyReference) return "";
var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
return repo.Get<IContent>(cref).Name;
}
public void AddBtn_Click(object sender, EventArgs e)
{
//Validate and Add to DDS
TypeSuggestion ts = new TypeSuggestion();
ts.ParentType = int.Parse(pageTypeList.SelectedValue);
ts.ParentReference = this.parentRef.PageLink ?? ContentReference.EmptyReference;
ts.ContentFolder = this.contentFolder.Checked;
ts.User = (this.justForMe.Checked) ? EPiServerProfile.Current.UserName : null;
ts.TypeToSuggest = int.Parse(this.TypesToSuggest.SelectedValue);
TypeSuggestion.Save(ts);
Response.Redirect(Request.Url.ToString());
}
protected void DeleteToolButton_Command(object sender, CommandEventArgs e)
{
TypeSuggestion.Delete(Identity.Parse((string) e.CommandArgument));
Response.Redirect(Request.Url.ToString());
}
}
}
using EPiServer.Core;
using EPiServer.Data;
using EPiServer.Data.Dynamic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CodeArt.Addons.ContentTypeSuggestions
{
[EPiServerDataStore(AutomaticallyCreateStore = true, AutomaticallyRemapStore = true)]
public class TypeSuggestion : IDynamicData
{
public Identity Id { get; set; }
public int ParentType { get; set; }
public ContentReference ParentReference { get; set; }
public int TypeToSuggest { get; set; }
public string User { get; set; }
public bool ContentFolder { get; set; }
public static IEnumerable<TypeSuggestion> ListForUser(string user)
{
var store = DynamicDataStoreFactory.Instance.CreateStore(typeof(TypeSuggestion));
return store.Items<TypeSuggestion>().Where(ts => ts.User==user || ts.User==null);
}
public static void Delete(Identity ID)
{
var store = DynamicDataStoreFactory.Instance.CreateStore(typeof(TypeSuggestion));
store.Delete(ID);
}
public static IEnumerable<TypeSuggestion> List(int TypeId,ContentReference Parent, string User)
{
var store = DynamicDataStoreFactory.Instance.CreateStore(typeof(TypeSuggestion));
return store.Items<TypeSuggestion>().Where(ts => (ts.User == User || ts.User == null)
&& (ts.ParentType == TypeId || ts.ParentType == -1)
&& (ts.ParentReference==Parent.ToReferenceWithoutVersion() || ts.ParentReference==ContentReference.EmptyReference));
}
public static void Save(TypeSuggestion suggestion)
{
var store = DynamicDataStoreFactory.Instance.CreateStore(typeof(TypeSuggestion));
store.Save(suggestion);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment