Copied to clipboard

Flag this post as spam?

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


  • Thomas Lee 38 posts 79 karma points
    Jun 04, 2015 @ 14:08
    Thomas Lee
    0

    Using Umbraco.ContentType(id) in C# Custom Class

    Hi guys,

    I am trying to do this

    var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    string id = e.Fields["id"].ToString();
    var parent = umbracoHelper.TypedContent(id);

    on my custom class. My aim is to load the parent Name into my indexer.

    But it's not working. My examine indexer stopped working after this... 

    I am using the following extension:

    using Examine;
    using System;
    using System.Text;
    using umbraco.BusinessLogic;
    using umbraco.providers.members;
    using Umbraco.Core;
    using Umbraco.Web;

    I also tried var currentItem = Umbraco.TypedContent(e.NodeId);

    It doesnt work. Can someone help me?

  • Maff 141 posts 466 karma points
    Jun 04, 2015 @ 14:25
    Maff
    0

    Hi Thomas,

    Can you post the error message that you're seeing? Also the TypedContent() method takes an int, not a string, so you could try that?

    Thanks,

    Maff

  • TikTakToe 60 posts 102 karma points
    Jun 04, 2015 @ 14:29
    TikTakToe
    0

    Hi

    if i understand you correctly, you're trying to get the parent name of the node being indexed?

    Try:

    via the content service (database, so slower)

    private void GatheringNodeDataHandler_YourEventName(object sender, IndexingNodeDataEventArgs e)
    {
         var currentItem  = ApplicationContext.Current.Services.ContentService.GetById(e.NodeId);
         e.Fields.Add("ParentName", currentItem.Parent.Name);
    }
    

    OR

    *xml cache, with a hacky way to get the current context - works, but is it ok?*

    private static UmbracoHelper Helper
    {
        get
        {
            var context = new HttpContext(new HttpRequest("", "http://mysite.local/", ""), new HttpResponse(null));
            UmbracoContext.EnsureContext(new HttpContextWrapper(context), ApplicationContext.Current);
            return new UmbracoHelper(UmbracoContext.Current);
        }
    }
    
    private void GatheringNodeDataHandler_YourEventName(object sender, IndexingNodeDataEventArgs e)
    {
         var currentItem = Helper.TypedContent(e.NodeId);
         e.Fields.Add("ParentName", currentItem.Parent.Name);
    }
    

    OR

    xml cache, as you've tried, but last time i tried, context was null as it's not available on the indexing thread (see: https://groups.google.com/forum/#!topic/umbraco-dev/-xto6IIPXrA)

    private void GatheringNodeDataHandler_YourEventName(object sender, IndexingNodeDataEventArgs e)
    {
         var helper = new UmbracoHelper(UmbracoContext.Current);
         var currentItem = helper.TypedContent(e.NodeId);
         e.Fields.Add("ParentName", currentItem.Parent.Name);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft