Skip to content

Instantly share code, notes, and snippets.

@skttl
Created September 12, 2019 06:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save skttl/b70c09250e9b640b76573efc4ea61ae3 to your computer and use it in GitHub Desktop.
Save skttl/b70c09250e9b640b76573efc4ea61ae3 to your computer and use it in GitHub Desktop.
Implement property searching in Umbraco (7) list views
using System;
using System.Linq;
using AutoMapper;
using Umbraco.Web.PublishedContentModels;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
using Umbraco.Web.Editors;
using Umbraco.Web.Models.ContentEditing;
using Umbraco.Web.WebApi;
namespace Something.Controllers.BackofficeControllers
{
public class ListViewSearchController : UmbracoAuthorizedApiController
{
public PagedResult<ContentItemBasic<ContentPropertyBasic, IContent>> GetChildren(
int id,
int pageNumber = 0,
int pageSize = 0,
string orderBy = "SortOrder",
Direction orderDirection = Direction.Ascending,
bool orderBySystemField = true,
string filter = "")
{
// get the parent node, and its doctype alias from the content cache
var parentNode = this.Services.ContentService.GetById(id);
var parentNodeDocTypeAliases = parentNode != null ? parentNode.ContentType.Alias : null;
// set doctype aliases that uses the custom logic
var enabledDocTypeAliases = new string[] { CategoryConsumer.ModelTypeAlias, CategoryExport.ModelTypeAlias, CategoryProfessional.ModelTypeAlias };
// if the parent node is not one of the enabled types, redirect to the core GetChildren() method
if (!enabledDocTypeAliases.InvariantContains(parentNodeDocTypeAliases))
{
var umbController = new ContentController(this.UmbracoContext);
return umbController.GetChildren(id, pageNumber, pageSize, orderBy, orderDirection, orderBySystemField, filter);
}
// find children using Examine
var searcher = Examine.ExamineManager.Instance.SearchProviderCollection["InternalSearcher"];
// build raw lucenequery
var luceneQuery = string.Format("+parentID:{0}", id);
if (!filter.IsNullOrWhiteSpace())
{
luceneQuery = luceneQuery + string.Format(" +(nodeName:{0}* productData:ItemNumber\\: {0}*)", filter);
}
// create search criteria
var searchCriteria = searcher.CreateSearchCriteria();
var searchQuery = searchCriteria.RawQuery(luceneQuery);
// do the search, but limit the results to the current page 👉 https://shazwazza.com/post/paging-with-examine/
// pageNumber is not zero indexed in this, so just multiply pageSize by pageNumber
var searchResults = searcher.Search(searchQuery, pageSize * pageNumber);
// get the results on the current page
// pageNumber is not zero indexed in this, so subtract 1 from the pageNumber
var pagedResultIds = searchResults.Skip((pageNumber > 0 ? pageNumber - 1 : 0) * pageSize).Select(x => x.Id).ToList();
// init the pagedResult object
var pagedResult = new PagedResult<ContentItemBasic<ContentPropertyBasic, IContent>>(searchResults.TotalItemCount, pageNumber, pageSize);
// get nodes from the content service and add them to the result
pagedResult.Items = this.Services.ContentService.GetByIds(pagedResultIds).Select(content =>
Mapper.Map<IContent, ContentItemBasic<ContentPropertyBasic, IContent>>(content));
if (!pagedResult.Items.Any())
{
return new PagedResult<ContentItemBasic<ContentPropertyBasic, IContent>>(0, 0, 0);
}
return pagedResult;
}
}
}
angular.module('umbraco.services').config([
'$httpProvider',
function ($httpProvider) {
$httpProvider.interceptors.push(function ($q) {
return {
'request': function (request) {
// Redirect any requests for the listview to our custom list view UI
if (request.url.indexOf("backoffice/UmbracoApi/Content/GetChildren?id=") > -1)
request.url = request.url.replace("backoffice/UmbracoApi/Content", "backoffice/api/ListViewSearch");
return request || $q.when(request);
}
};
});
}]);
{
javascript: [
'~/App_Plugins/ListViewSearch/ListViewSearchInterceptor.js'
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment