Copied to clipboard

Flag this post as spam?

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


  • Mila Pandurska 75 posts 353 karma points
    Jun 06, 2019 @ 14:18
    Mila Pandurska
    0

    Add values to indexset on TransformingIndexValues

    Hi, I have a blog and I have custom index which keeps information for my articles. Article has a property of type MNTP which needs to be searchable. In the index I see that the values of the MNTP looks like "umb://document/917ac780d3bd4208bd1001c8838c92a8". I have TransformingIndexValues(object sender, IndexingItemEventArgs e) method which needs to get the content item for this udi and save its name in the index. I used to work with umbracoHelper in Umbraco 7, but now in Umbraco 8 I receive error when I call:

     Umbraco.Web.Composing.Current.UmbracoHelper;
    

    Thanks Mila

  • Mila Pandurska 75 posts 353 karma points
    Jun 07, 2019 @ 03:03
    Mila Pandurska
    100

    Hi I found the solution. Mainly the problem was getting item from ContentCache by UDI. I have blog and my articles have MNTP which value I need to store in the index. I implement my TransformingIndexValues method:

     public class CustomExamineComponent : IComponent
        {
            private readonly IExamineManager _examineManager;
            private readonly CustomIndexCreator _indexCreator;
            private readonly IUmbracoContextFactory _contextFactory;
    
            public CustomExamineComponent(IExamineManager examineManager, CustomIndexCreator indexCreator, IUmbracoContextFactory contextFactory)
            {
                _examineManager = examineManager;
                _indexCreator = indexCreator;
                _contextFactory = contextFactory;
            }
    
    
            public void Initialize()
            {
                DirectoryFactory.DefaultLockFactory = d =>
                {
                    var simpleFsLockFactory = new NoPrefixSimpleFsLockFactory(d);
                    return simpleFsLockFactory;
                };
                foreach (var index in _indexCreator.Create())
                {
                    _examineManager.AddIndex(index);
                    if (index.Name == "NewsSearchIndex")
                    {
                        ((BaseIndexProvider)index).TransformingIndexValues += CustomExamineComponent_TransformingIndexValues; 
                    }
    
                }
    
            }
    
            private void CustomExamineComponent_TransformingIndexValues(object sender, IndexingItemEventArgs e)
            {
                using (var ctx = _contextFactory.EnsureUmbracoContext())
                {
                    var cache = ctx.UmbracoContext.ContentCache;
                    if (e.ValueSet.ItemType == "articleDetailsPage")
                    {
                        var categoryItem = e.ValueSet.Values["category"];
                        if (categoryItem != null)
                        {                       
                            Udi udi;
                            if (Udi.TryParse(categoryItem[0].ToString(), out udi))
                            {
                                //Get node by UDI
                                 var guid = GuidUdi.Parse(udi.ToString());
                                 var node = cache.GetById(guid.Guid);
                            }
                        }
                    }
                }
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft