Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ross Brooker 6 posts 76 karma points
    Oct 05, 2016 @ 12:31
    Ross Brooker
    0

    Filtering Searches - how to retrieve a Generic / Content property from a Folder?

    Hi,

    Here's some background: I'm building our site such that we can content-target parts of our site... if the user comes in and declares they're a Financial Adviser, then they see different content to the general public.

    I've had success with page content etc, but I'm now struggling with amending the site search function, in the case where I set the content targeting at the folder level, and I want to include / exclude search hits dependent on the setting for the parent folder.

    I have this code which checks the content targeting of the search result, and then goes through all the parent items and checks theirs too:

                <ul class="list searchResults">
    
                @foreach (var item in searchResults)
                {
                    @* Check to see if the content should be filtered out by content targeting *@
    
                    thisTarget = (item.Fields.ContainsKey("contentTargeting") ? Convert.ToInt32(item.Fields["contentTargeting"]) : -1);
                    thisUrl = Umbraco.Content(item.Fields["id"]).Url.ToLower();
                    excludeThis = ContentHelper.checkContentTargeting(userType, thisTarget, thisUrl);
    
                    if (!excludeThis && item.Fields.ContainsKey("path"))
                    {
                        string[] parents = item.Fields["path"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                        foreach (var parentID in parents)
                        {
                            if (!parentID.Equals("-1"))
                            {
                                var parentItem = Umbraco.Content(parentID);
                                if (parentItem.HasProperty("contentTargeting")) {
                                    thisTarget = (parentItem.GetPropertyValue("contentTargeting") != null ? Convert.ToInt32(parentItem.GetPropertyValue("contentTargeting").ToString()) : -1);
                                } else {
                                    thisTarget = -1;
                                }
                                thisUrl = parentItem.Fields["id"].Url.ToLower();
    
                                excludeThis = ContentHelper.checkContentTargeting(userType, thisTarget, thisUrl);
                                if (excludeThis)
                                {
                                    break;
                                }
                            }
                        }
                    }
    
                    if (!excludeThis)
                    { ....
    

    So to reiterate - the problem is this isn't working when the parent item is a Folder or Literature Folder. I think this line:

    var parentItem = Umbraco.Content(parentID);
    

    ... isn't working working - the parentItem variable does not contain the ContentTargeting value.

    Does anyone have any suggestions on how to get the data?

    (I'm also open to suggestions on ways to clean this code up, I'm sure there are better ways to do this)

    Edit 05/10/16 15:24:
    Ok... a bit of progress... and a bit of a "D'oh" moment... I think I need to get the parentItem from either Umbraco.Content or Umbraco.Media depending on the indexType of the found item. Now I'm not sure how I get that.

Please Sign in or register to post replies

Write your reply to:

Draft