Skip to content

Instantly share code, notes, and snippets.

@markeverard
Last active December 15, 2022 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markeverard/7605620426bbb9599a73 to your computer and use it in GitHub Desktop.
Save markeverard/7605620426bbb9599a73 to your computer and use it in GitHub Desktop.
Unused Media Slice
using System.Diagnostics;
using System.Linq;
using EPiServer;
using EPiServer.Cms.Shell.UI.Rest.ContentQuery;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.Find;
using EPiServer.ServiceLocation;
using EPiServer.Shell.ContentQuery;
using EPiServer.Shell.Rest;
using EPiServer.Shell.Services.Rest;
using PowerSlice;
namespace Scenarios.Business
{
[ServiceConfiguration(typeof(IContentQuery)), ServiceConfiguration(typeof(IContentSlice))]
public class UnusedMediaSlice : ContentSliceBase<MediaData>
{
protected IContentRepository ContentRepository;
public UnusedMediaSlice(IClient searchClient, IContentTypeRepository contentTypeRepository, IContentLoader contentLoader, IContentRepository contentRepository)
: base(searchClient, contentTypeRepository, contentLoader)
{
ContentRepository = contentRepository;
}
public override string Name
{
get { return "Unused Media"; }
}
public override QueryRange<IContent> ExecuteQuery(IQueryParameters parameters)
{
var originalContentRange = base.ExecuteQuery(parameters);
var filteredResults = originalContentRange.Items.Where(IsNotReferenced).ToList();
var itemRange = new ItemRange
{
Total = filteredResults.Count,
Start = parameters.Range.Start,
End = parameters.Range.End
};
return new ContentRange(filteredResults, itemRange);
}
protected bool IsNotReferenced(IContent content)
{
return !ContentRepository.GetReferencesToContent(content.ContentLink, false).Any();
}
}
}
@ZachGraceffa
Copy link

Thank you for the code! While using it I found out paging does not work with this slice. Line 39 must be changed to:

Total = originalContentRange.Range.Total,

By using the total from the original query, and not the newly filtered results, you let the UI know that there are more results to page through.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment