Skip to content

Instantly share code, notes, and snippets.

@abjerner
Created October 11, 2016 21:17
Show Gist options
  • Save abjerner/a62e5df4663a98c699b72ecd1e07e229 to your computer and use it in GitHub Desktop.
Save abjerner/a62e5df4663a98c699b72ecd1e07e229 to your computer and use it in GitHub Desktop.
Convert SearchResult into IPublishedContent
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Examine;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
namespace Skybrud.ExamineDevStuff {
public class ExaminePublishedContent : IPublishedContent {
private readonly List<IPublishedProperty> _properties = new List<IPublishedProperty>();
public ExaminePublishedContent(SearchResult result) {
if (result == null) throw new ArgumentNullException("result");
DocumentTypeAlias = result.Fields["nodeTypeAlias"];
Id = result.Id;
ContentType = PublishedContentType.Get(PublishedItemType.Content, DocumentTypeAlias);
foreach (KeyValuePair<string, string> field in result.Fields) {
switch (field.Key) {
case "parentID": /* IPublishedContent doesn't have a property for the parent ID */ break;
case "level": Level = Int32.Parse(field.Value); break;
case "writerID": WriterId = Int32.Parse(field.Value); break;
case "writerName": WriterName = field.Value; break;
case "creatorID": CreatorId = Int32.Parse(field.Value); break;
case "creatorName": CreatorName = field.Value; break;
case "nodeType": DocumentTypeId = Int32.Parse(field.Value); break;
case "template": TemplateId = Int32.Parse(field.Value); break;
case "sortOrder": SortOrder = Int32.Parse(field.Value); break;
case "createDate": CreateDate = DateTime.ParseExact(field.Value.Substring(0, 14), "yyyyMMddHHmmss", CultureInfo.CurrentCulture); break;
case "updateDate": UpdateDate = DateTime.ParseExact(field.Value.Substring(0, 14), "yyyyMMddHHmmss", CultureInfo.CurrentCulture); break;
case "nodeName": Name = field.Value; break;
case "urlName": UrlName = field.Value; break;
case "__Path": Path = field.Value; break;
}
// Get the type of the field/property (not all fields is a property)
PublishedPropertyType type = ContentType.GetPropertyType(field.Key);
if (type == null) continue;
// Append the property to the list of properties
_properties.Add(new ExaminePublishedProperty(type, field.Value));
}
}
public int GetIndex() {
throw new NotImplementedException();
}
public IPublishedProperty GetProperty(string alias) {
return _properties.FirstOrDefault(x => x.PropertyTypeAlias.InvariantEquals(alias));
}
public IPublishedProperty GetProperty(string alias, bool recurse) {
if (recurse) throw new NotSupportedException();
return GetProperty(alias);
}
public IEnumerable<IPublishedContent> ContentSet { get; private set; }
public PublishedContentType ContentType { get; private set; }
public int Id { get; private set; }
public int TemplateId { get; private set; }
public int SortOrder { get; private set; }
public string Name { get; private set; }
public string UrlName { get; private set; }
public string DocumentTypeAlias { get; private set; }
public int DocumentTypeId { get; private set; }
public string WriterName { get; private set; }
public string CreatorName { get; private set; }
public int WriterId { get; private set; }
public int CreatorId { get; private set; }
public string Path { get; private set; }
public DateTime CreateDate { get; private set; }
public DateTime UpdateDate { get; private set; }
public Guid Version { get; private set; }
public int Level { get; private set; }
public string Url {
get { throw new NotImplementedException(); }
}
public PublishedItemType ItemType { get; private set; }
public bool IsDraft {
get { return false; }
}
public IPublishedContent Parent {
get { throw new NotImplementedException(); }
}
public IEnumerable<IPublishedContent> Children {
get { return new IPublishedContent[0]; }
}
public ICollection<IPublishedProperty> Properties {
get { return _properties; }
}
public object this[string alias] {
get { throw new NotImplementedException(); }
}
public T Select<T>(Func<IPublishedContent, T> func) {
return func(this);
}
}
}
@using Examine.Providers
@using Examine.SearchCriteria
@using Skybrud.ExamineDevStuff
@{
// Get a reference to the external searcher
BaseSearchProvider externalSearcher = ExamineManager.Instance.SearchProviderCollection["ExternalSearcher"];
// Create a new search criteria and set our query
ISearchCriteria criteria = externalSearcher.CreateSearchCriteria();
criteria = criteria.RawQuery("nodeTypeAlias:MyNodeAlias");
// Make the search in Examine
ISearchResults results = externalSearcher.Search(criteria);
foreach (SearchResult result in results) {
// Get a new IPublishedContent from the SearchResult
IPublishedContent content = new ExaminePublishedContent(result);
<h4 style="font-weight: bold; margin-top: 15px;">IPublishedContent</h4>
<pre>Id => @content.Id</pre>
<pre>Level => @content.Level</pre>
<pre>WriterId => @content.WriterId</pre>
<pre>CreatorId => @content.CreatorId</pre>
<pre>TemplateId => @content.TemplateId</pre>
<pre>SortOrder => @content.SortOrder</pre>
<pre>CreateDate => @content.CreateDate</pre>
<pre>UpdateDate => @content.UpdateDate</pre>
<pre>Name => @content.Name</pre>
<pre>UrlName => @content.UrlName</pre>
<pre>WriterName => @content.WriterName</pre>
<pre>CreatorName => @content.CreatorName</pre>
<pre>Path => @content.Path</pre>
<h4 style="font-weight: bold; margin-top: 15px;">Properties</h4>
foreach (var property in content.Properties) {
<pre>@property.PropertyTypeAlias => @property.Value</pre>
}
<h4 style="font-weight: bold; margin-top: 15px;">Fields</h4>
foreach (var field in result.Fields) {
<pre>@field.Key => @field.Value</pre>
}
break;
}
}
using System;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
namespace Skybrud.ExamineDevStuff {
public class ExaminePublishedProperty : IPublishedProperty {
private readonly PublishedPropertyType _propertyType;
private readonly object _rawValue;
private readonly Lazy<object> _sourceValue;
private readonly Lazy<object> _objectValue;
private readonly Lazy<object> _xpathValue;
private readonly bool _isPreview;
public ExaminePublishedProperty(PublishedPropertyType propertyType, object value)
: this(propertyType, value, false) {
}
public ExaminePublishedProperty(PublishedPropertyType propertyType, object value, bool isPreview) {
_propertyType = propertyType;
_isPreview = isPreview;
_rawValue = value;
_sourceValue = new Lazy<object>(() => _propertyType.ConvertDataToSource(_rawValue, _isPreview));
_objectValue = new Lazy<object>(() => _propertyType.ConvertSourceToObject(_sourceValue.Value, _isPreview));
_xpathValue = new Lazy<object>(() => _propertyType.ConvertSourceToXPath(_sourceValue.Value, _isPreview));
}
public string PropertyTypeAlias {
get {
return _propertyType.PropertyTypeAlias;
}
}
public bool HasValue {
get { return DataValue != null && DataValue.ToString().Trim().Length > 0; }
}
public object DataValue { get { return _rawValue; } }
public object Value { get { return _objectValue.Value; } }
public object XPathValue { get { return _xpathValue.Value; } }
}
}
@kipusoep
Copy link

kipusoep commented Oct 12, 2016

Isn't it much easier to use the result.Id to get the IPublishedContent object via UmbracoHelper.TypedContent(id)?

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