Copied to clipboard

Flag this post as spam?

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


  • Jason Espin 368 posts 1335 karma points
    Apr 28, 2015 @ 12:07
    Jason Espin
    0

    Get ancestor at level one using the UmbracoHelper method

    Hi all,

    I am currently in the process of writing a surface controller to validate the input on a search form before redirecting to the page where the logic is handled and processed.

    The main issue I have is that this is a multi-language site so there is obviously a search page for each of the different languages.

    The idea I had was to obtain the current context of the application to get the page from which the referral to surface controller came and then traverse up the tree to find the correct homepage before then finding the descendant of the homepage that matched the document type of "search" however there does not seem to be a way of doing this in the Umbraco helper.

    Can anyone shed any light on this?

    My code can be found below:

    public class TourFinderController : SurfaceController
        {
            [HttpPost]
            public ActionResult Submit(TourFinderModel model)
            {
                if (!ModelState.IsValid)
                {
                    return CurrentUmbracoPage();
                }
    
                UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
    
    
                // This will contain the IPublishedContent object or ID of the correct search page obtained by traversing the tree
                return RedirectToUmbracoPage();
            }
        }
    

    Cheers, J

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Apr 28, 2015 @ 13:51
    Sebastiaan Janssen
    0

    You should be able to get the content using the helper pretty easily, here's some example code:

    var currentPageId = UmbracoContext.PageId.Value;
    var currentPage = helper.TypedContent(currentPageId);
    // Traverse up the tree to level == 1
    var home = currentPage.AncestorOrSelf(1);
    var searchPage = home.Children.FirstOrDefault(x => x.ContentType.Alias == "searchPage");
    if (searchPage != null)
    {
        // Do your thing
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft