/MemberProfileContentFinder.cs
Forked from warrenbuckley/MemberProfileContentFinder.cs
Last active Aug 29, 2015
Update for Umbraco 7
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using Umbraco.Core; | |
using Umbraco.Core.Models; | |
using Umbraco.Core.Persistence.Querying; | |
using Umbraco.Web.Routing; | |
namespace FEE.Domain.MemberProfile | |
{ | |
// From http://creativewebspecialist.co.uk/2013/12/03/using-umbraco-pipeline-for-member-profile-urls/ | |
public class MemberProfileContentFinder : IContentFinder | |
{ | |
public bool TryFindContent(PublishedContentRequest contentRequest) | |
{ | |
var urlParts = contentRequest.Uri.GetAbsolutePathDecoded() | |
.Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries); | |
//Check if the Url Parts | |
// Starts with /profile/* | |
if (urlParts.Length > 1 && urlParts[0].ToLower() == "profile") | |
{ | |
//Lets try & find the member | |
var memberName = urlParts[1]; | |
//Try and find a member where the property matches the memberName | |
List<IMember> tryFindMember = | |
ApplicationContext.Current.Services.MemberService.GetMembersByPropertyValue("umbracoUrlName", | |
memberName).ToList(); | |
if (!tryFindMember.Any()) // try a partial match just in case | |
{ | |
tryFindMember = ApplicationContext.Current.Services.MemberService.GetMembersByPropertyValue("umbracoUrlName", | |
memberName,StringPropertyMatchType.Contains).ToList(); | |
} | |
//See if tryFindMember is not null | |
if (tryFindMember.Any()) | |
{ | |
//Need to set the member ID or pass member object to published content | |
HttpContext.Current.Items["memberProfile"] = tryFindMember.FirstOrDefault(); | |
//Set the Published Content Node to be the /Profile node - can get properties off it & my member profile in the view | |
contentRequest.PublishedContent = | |
contentRequest.RoutingContext.UmbracoContext.ContentCache.GetByRoute("/people/member-profile"); | |
} | |
//Return true to say found something & stop pipeline & other contentFinder's from running | |
return true; | |
} | |
//Not found any content node to display/match - so run next ContentFinder in Pipeline | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment