Skip to content

Instantly share code, notes, and snippets.

@karlstal
Last active May 27, 2020 12:25
Show Gist options
  • Save karlstal/1e66a129b37d29563e02f8cbc9564daa to your computer and use it in GitHub Desktop.
Save karlstal/1e66a129b37d29563e02f8cbc9564daa to your computer and use it in GitHub Desktop.
using EPiServer;
using EPiServer.Core;
using EPiServer.Core.Html.StringParsing;
using EPiServer.DataAbstraction;
using EPiServer.Find;
using EPiServer.Find.Helpers.Text;
using EPiServer.ServiceLocation;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
namespace VisitorgroupsTest
{
public static class MyContentExtensions
{
public static IDictionary<string, string> PersonalizedFragments(this IContent content)
{
var propertyDefinitionRepository = ServiceLocator.Current.GetInstance<IPropertyDefinitionRepository>();
var personalizedFragments = new Dictionary<string, string>();
// Find all top level XHtmlString fields
foreach (var property in content.Property)
{
var xhtml = property.Value as XhtmlString;
if (xhtml != null)
{
// Check if the property is marked with <see cref="JsonIgnoreAttribute"/> and must be excluded when indexing
var typeProperty = content.GetOriginalType().GetProperty(property.Name);
if (typeProperty == null ||
typeProperty.GetCustomAttribute<JsonIgnoreAttribute>() != null)
{
continue;
}
var propertyDefinition = propertyDefinitionRepository.Load(property.PropertyDefinitionID);
if (propertyDefinition.Searchable)
{
personalizedFragments.addPersonalizedFragments(xhtml);
}
}
}
return personalizedFragments;
}
private static void addPersonalizedFragments(this IDictionary<string, string> personalizedFragments, XhtmlString xhtml)
{
// Note, that we will not find nested data. A complete parser is out of scope for this example.
foreach (var fragment in xhtml.Fragments.OfType<PersonalizedContentFragment>())
{
var text = string.Join(" ", fragment.Fragments.Where(x => x is StaticFragment).Select(x => x.InternalFormat)).StripHtml();
foreach (var role in fragment.GetRoles())
{
if (!personalizedFragments.ContainsKey(role))
{
personalizedFragments[role] = text;
}
else
{
personalizedFragments[role] += " " + text;
}
}
}
}
}
}
using EPiServer.Core;
using EPiServer.Find;
using EPiServer.Find.Api.Querying.Queries;
using EPiServer.Find.UnifiedSearch;
using EPiServer.Personalization.VisitorGroups;
using EPiServer.ServiceLocation;
using System.Collections.Generic;
using System.Security.Principal;
namespace VisitorgroupsTest
{
public static class MyFindSearchExtensions
{
// This extension method is targeted for UnifiedSearch (hence the ISearchContent) used in a CMS context (i.e. the cast to IContent)
public static IQueriedSearch<ISearchContent, QueryStringQuery> AddPersonalizedFields(
this IQueriedSearch<ISearchContent, QueryStringQuery> search, IPrincipal user)
{
var visitorgroupIds = GetVisitorGroupIdsByCurrentUser(user);
// I might be good to put a cap here with a warnig if users suddenly qualify for a lot of visitor groups.
foreach (var id in visitorgroupIds)
{
search = search.InField(x => (x as IContent).PersonalizedFragments()[id]);
}
return search;
}
private static List<string> GetVisitorGroupIdsByCurrentUser(IPrincipal user)
{
var visitorGroupId = new List<string>();
var visitorGroupRepository = ServiceLocator.Current.GetInstance<IVisitorGroupRepository>();
var visitorGroupRoleRepository = ServiceLocator.Current.GetInstance<IVisitorGroupRoleRepository>();
var helper = new VisitorGroupHelper(visitorGroupRoleRepository);
var groups = visitorGroupRepository.List();
foreach (var visitorGroup in groups)
{
if (helper.IsPrincipalInGroup(user, visitorGroup.Name))
{
visitorGroupId.Add(visitorGroup.Id.ToString());
}
}
return visitorGroupId;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment