Skip to content

Instantly share code, notes, and snippets.

@PNergard
Created November 9, 2017 12:23
Show Gist options
  • Save PNergard/eba36f8c585419f1b65e27c0bd5f43fe to your computer and use it in GitHub Desktop.
Save PNergard/eba36f8c585419f1b65e27c0bd5f43fe to your computer and use it in GitHub Desktop.
Episerver wastebasket plugin that let admins do simple filtering and delete a single content instead of emptying the whole wastebasket.
<%@ Page Language="c#" CodeBehind="Basket.aspx.cs" AutoEventWireup="False" Inherits="NergardPlayGround.modules.WasteBasket.Basket" Title="" %>
<%@ Import Namespace="EPiServer.Web.Mvc.Html" %>
<asp:content contentplaceholderid="HeaderContentRegion" runat="server">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#tabell').DataTable();
});
function SetSelected() {
var ids = [];
$('#tabell input[type="checkbox"]:checked').each(function () {
ids.push(this.value);
});
$('#SelectedContent').val(ids.join(','));
}
</script>
</asp:content>
<asp:content contentplaceholderid="MainRegion" runat="server">
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<asp:HiddenField runat="server" id="SelectedContent" ClientIDMode="static" />
<div class="epi-formArea epi-paddingVertical-small">
<fieldset>
<legend>Actions</legend>
<span class="epi-cmsButton">
<input id="btnDeleteAll" runat="server" type="button" value="Delete all" class="epi-cmsButton-text epi-cmsButton-tools epi-cmsButton-Save" onmouseover="EPi.ToolButton.MouseDownHandler(this)" onmouseout="EPi.ToolButton.ResetMouseDownHandler(this)">
</span>
<span class="epi-cmsButton">
<input id="btnDeleteSelected" runat="server" type="button" value="Delete selected" onclick="SetSelected();" class="epi-cmsButton-text epi-cmsButton-tools epi-cmsButton-Save" onmouseover="EPi.ToolButton.MouseDownHandler(this)" onmouseout="EPi.ToolButton.ResetMouseDownHandler(this)">
</span>
</fieldset>
<table id="tabell" class="blueTable">
<thead>
<tr>
<th>ContentLink</th>
<th>Type</th>
<th>Name</th>
<th>Choosen</th>
</tr>
</thead>
<tbody>
<asp:Repeater runat="server" ID="rptBasketContent">
<ItemTemplate>
<tr>
<td><%#BasketItem.ContentLink %></td>
<td><%# GetContentTypeDisplayName (BasketItem.ContentTypeID) %></td>
<td><%#BasketItem.Name %></td>
<td><input type="checkbox" value="<%#BasketItem.ContentLink %>" /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</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.Shell.WebForms;
using EPiServer;
using EPiServer.Core;
using EPiServer.ServiceLocation;
using System.Web.Mvc;
using System.Linq;
namespace NergardPlayGround.modules.WasteBasket
{
[GuiPlugIn(DisplayName = "Wastebasket", Description = "", Area = PlugInArea.AdminMenu, Url = "~/modules/WasteBasket/Basket.aspx")]
public partial class Basket : WebFormsBase
{
#region var and props
protected UrlHelper Url = ServiceLocator.Current.GetInstance<UrlHelper>();
protected IContent BasketItem { get { return Page.GetDataItem() as IContent; } }
#endregion
#region overrides
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
MasterPageFile = UriSupport.ResolveUrlFromUIBySettings("MasterPages/EPiServerUI.master");
SystemMessageContainer.Heading = "Wastebasket";
SystemMessageContainer.Description = "Simple plugin that displays the content of the wastebasket and let you perform simple filtering / searching, and deleting all or just selected content.";
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
btnDeleteAll.ServerClick += BtnDeleteAll_ServerClick;
btnDeleteSelected.ServerClick += BtnDeleteSelected_ServerClick;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
DataBindTable();
}
#endregion
#region Eventhandlers
private void BtnDeleteAll_ServerClick(object sender, EventArgs e)
{
var allContent = GetBasketContent();
foreach (var content in allContent)
{
ContentRepository.Delete(content.ContentLink, true, AccessLevel.NoAccess);
}
DataBindTable();
}
private void BtnDeleteSelected_ServerClick(object sender, EventArgs e)
{
var allContent = SelectedContent.Value.Split("'".ToCharArray());
foreach (var content in allContent)
{
ContentRepository.Delete(ContentReference.Parse(content), true, AccessLevel.NoAccess);
}
DataBindTable();
}
#endregion
#region Methods
private void DataBindTable()
{
rptBasketContent.DataSource = GetBasketContent();
rptBasketContent.DataBind();
}
private IEnumerable<IContent> GetBasketContent()
{
return ContentRepository.GetChildren<IContent>(ContentReference.WasteBasket);
}
protected string GetContentTypeDisplayName(int id)
{
return ContentTypeRepository.Load(id).Name;
}
#endregion
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace NergardPlayGround.modules.WasteBasket {
public partial class Basket {
/// <summary>
/// SelectedContent control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.HiddenField SelectedContent;
/// <summary>
/// btnDeleteAll control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlInputButton btnDeleteAll;
/// <summary>
/// btnDeleteSelected control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlInputButton btnDeleteSelected;
/// <summary>
/// rptBasketContent control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Repeater rptBasketContent;
}
}
<%@ Page Language="c#" CodeBehind="Basket.aspx.cs" AutoEventWireup="False" Inherits="NergardPlayGround.modules.WasteBasket.Basket" Title="" %>
<%@ Import Namespace="EPiServer.Web.Mvc.Html" %>
<asp:content contentplaceholderid="HeaderContentRegion" runat="server">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#tabell').DataTable();
});
function SetSelected() {
var ids = [];
$('#tabell input[type="checkbox"]:checked').each(function () {
ids.push(this.value);
});
$('#SelectedContent').val(ids.join(','));
}
</script>
</asp:content>
<asp:content contentplaceholderid="MainRegion" runat="server">
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<asp:HiddenField runat="server" id="SelectedContent" ClientIDMode="static" />
<div class="epi-formArea epi-paddingVertical-small">
<fieldset>
<legend>Actions</legend>
<span class="epi-cmsButton">
<input id="btnDeleteAll" runat="server" type="button" value="Delete all" class="epi-cmsButton-text epi-cmsButton-tools epi-cmsButton-Save" onmouseover="EPi.ToolButton.MouseDownHandler(this)" onmouseout="EPi.ToolButton.ResetMouseDownHandler(this)">
</span>
<span class="epi-cmsButton">
<input id="btnDeleteSelected" runat="server" type="button" value="Delete selected" onclick="SetSelected();" class="epi-cmsButton-text epi-cmsButton-tools epi-cmsButton-Save" onmouseover="EPi.ToolButton.MouseDownHandler(this)" onmouseout="EPi.ToolButton.ResetMouseDownHandler(this)">
</span>
</fieldset>
<table id="tabell" class="blueTable">
<thead>
<tr>
<th>ContentLink</th>
<th>Type</th>
<th>Name</th>
<th>Choosen</th>
</tr>
</thead>
<tbody>
<asp:Repeater runat="server" ID="rptBasketContent">
<ItemTemplate>
<tr>
<td><%#BasketItem.ContentLink %></td>
<td><%# GetContentTypeDisplayName (BasketItem.ContentTypeID) %></td>
<td><%#BasketItem.Name %></td>
<td><input type="checkbox" value="<%#BasketItem.ContentLink %>" /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</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.Shell.WebForms;
using EPiServer;
using EPiServer.Core;
using EPiServer.ServiceLocation;
using System.Web.Mvc;
using System.Linq;
namespace NergardPlayGround.modules.WasteBasket
{
[GuiPlugIn(DisplayName = "Wastebasket", Description = "", Area = PlugInArea.AdminMenu, Url = "~/modules/WasteBasket/Basket.aspx")]
public partial class Basket : WebFormsBase
{
#region var and props
protected UrlHelper Url = ServiceLocator.Current.GetInstance<UrlHelper>();
protected IContent BasketItem { get { return Page.GetDataItem() as IContent; } }
#endregion
#region overrides
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
MasterPageFile = UriSupport.ResolveUrlFromUIBySettings("MasterPages/EPiServerUI.master");
SystemMessageContainer.Heading = "Wastebasket";
SystemMessageContainer.Description = "Simple plugin that displays the content of the wastebasket and let you perform simple filtering / searching, and deleting all or just selected content.";
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
btnDeleteAll.ServerClick += BtnDeleteAll_ServerClick;
btnDeleteSelected.ServerClick += BtnDeleteSelected_ServerClick;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
DataBindTable();
}
#endregion
#region Eventhandlers
private void BtnDeleteAll_ServerClick(object sender, EventArgs e)
{
var allContent = GetBasketContent();
foreach (var content in allContent)
{
ContentRepository.Delete(content.ContentLink, true, AccessLevel.NoAccess);
}
DataBindTable();
}
private void BtnDeleteSelected_ServerClick(object sender, EventArgs e)
{
var allContent = SelectedContent.Value.Split("'".ToCharArray());
foreach (var content in allContent)
{
ContentRepository.Delete(ContentReference.Parse(content), true, AccessLevel.NoAccess);
}
DataBindTable();
}
#endregion
#region Methods
private void DataBindTable()
{
rptBasketContent.DataSource = GetBasketContent();
rptBasketContent.DataBind();
}
private IEnumerable<IContent> GetBasketContent()
{
return ContentRepository.GetChildren<IContent>(ContentReference.WasteBasket);
}
protected string GetContentTypeDisplayName(int id)
{
return ContentTypeRepository.Load(id).Name;
}
#endregion
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace NergardPlayGround.modules.WasteBasket {
public partial class Basket {
/// <summary>
/// SelectedContent control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.HiddenField SelectedContent;
/// <summary>
/// btnDeleteAll control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlInputButton btnDeleteAll;
/// <summary>
/// btnDeleteSelected control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlInputButton btnDeleteSelected;
/// <summary>
/// rptBasketContent control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Repeater rptBasketContent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment