I'm trying to hook into the published event so I can index a node in Elastic Search. However, I would like to store the url to be consumed on the front end.
It works fine on existing content that I am updating, but not on new content.
When I use the UmbracoHelper to get TypedContent it returns null because it isn't in the cache and umbraco.library.NiceUrl(id) doesn't work either.
Please help - this is a real deal breaker for integrating a 3rd party search solution.
It's pretty basic (I've omitted the 2 unused methods from the interface). I'm using 7.1.4.
The issue is that "content" below returns null when it's a newly created item and I can't use IContent either (I don't really want to since my indexer consumes IPublishedContent) since it hasn't got a url.
public class ContentEvents : IApplicationEventHandler
{
private readonly SearchService searchService = new SearchService();
That is strange, in my v7.1.4 it seems to be there.
Anyhow, I had a deeper look and the problem is that the publish events happens before the published content is updated. So I have found that there is a legacy event called AfterUpdateDocumentCache which is called after the published content has been updated, there doesn't appear to be a current equivalent. I think this should do what you need:
using umbraco;
using umbraco.cms.businesslogic.web;
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web;
public class ContentEvents : ApplicationEventHandler
{
//private readonly SearchService searchService = new SearchService();
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Deleted += ContentServiceDeleted;
content.AfterUpdateDocumentCache += content_AfterUpdateDocumentCache;
}
void content_AfterUpdateDocumentCache(Document sender, umbraco.cms.businesslogic.DocumentCacheEventArgs e)
{
// if you want to use Content Service instead of legacy Document Api
var cs = ApplicationContext.Current.Services.ContentService;
var item = cs.GetById(sender.Id);
var contentUrl = new UmbracoHelper(UmbracoContext.Current).TypedContent(item.Id);
}
private void ContentServiceDeleted(IContentService sender, DeleteEventArgs<IContent> e)
{
foreach (var item in e.DeletedEntities)
{
// searchService.Delete(item.Id);
}
}
}
That's great! There is ongoing work on a new object cache for Umbraco vNext which will hopefully also come with new cache events, in the meantime this should be fine.
Please mark the above correct solution I think it will be useful to others :)
I am having the same problem (in 7.4.1), ContentService.Published fires before the cache is updated, meaning Umbraco.TypedContent gets the previous version.
how i can get the current page & its content on the publish event , in my case i have multiple pages , so which page has been published i want to know & get its Id not the whole pages or blocks .
Published Event - How to get Url? Elastic Search
Hello,
I'm trying to hook into the published event so I can index a node in Elastic Search. However, I would like to store the url to be consumed on the front end.
It works fine on existing content that I am updating, but not on new content.
When I use the UmbracoHelper to get TypedContent it returns null because it isn't in the cache and umbraco.library.NiceUrl(id) doesn't work either.
Please help - this is a real deal breaker for integrating a 3rd party search solution.
Hi Valerie,
Unpublished nodes don't have a URL, it's the publishing that creates it.
Could you share your event code so we can see what's going on?
Jeavon
Hi there,
It's pretty basic (I've omitted the 2 unused methods from the interface). I'm using 7.1.4.
The issue is that "content" below returns null when it's a newly created item and I can't use IContent either (I don't really want to since my indexer consumes IPublishedContent) since it hasn't got a url.
public class ContentEvents : IApplicationEventHandler
{
private readonly SearchService searchService = new SearchService();
void ContentServicePublished(Umbraco.Core.Publishing.IPublishingStrategy sender, PublishEventArgs<IContent> e)
{
foreach (var item in e.PublishedEntities)
{
//this is null for a newly created item
var content = new UmbracoHelper(UmbracoContext.Current).TypedContent(item.Id);
searchService.Index(content);
}
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Published += ContentServicePublished;
ContentService.Deleted += ContentServiceDeleted;
}
private void ContentServiceDeleted(IContentService sender, DeleteEventArgs<IContent> e)
{
foreach (var item in e.DeletedEntities)
{
searchService.Delete(item.Id);
}
}
}
Thanks,
Valerie
Hmm, strangely NiceUrl does return while TypedContent doesn't. Should work for you though...?
It just returns a "#" when I do it :(
Hi Valerie,
That is strange, in my v7.1.4 it seems to be there.
Anyhow, I had a deeper look and the problem is that the publish events happens before the published content is updated. So I have found that there is a legacy event called AfterUpdateDocumentCache which is called after the published content has been updated, there doesn't appear to be a current equivalent. I think this should do what you need:
Jeavon
Thanks a lot Jeavon that solves my problem completely! So happy ;)
That's great! There is ongoing work on a new object cache for Umbraco vNext which will hopefully also come with new cache events, in the meantime this should be fine.
Please mark the above correct solution I think it will be useful to others :)
Sorry for bringing up an old topic.
I am having the same problem (in 7.4.1), ContentService.Published fires before the cache is updated, meaning Umbraco.TypedContent gets the previous version.
Is there an issue created for this?
I can't see an issue yet for this. Have you posted one?
Issue posted: http://issues.umbraco.org/issue/U4-8973
how i can get the current page & its content on the publish event , in my case i have multiple pages , so which page has been published i want to know & get its Id not the whole pages or blocks .
Please Help me i am new to Umbraco
is working on a reply...