Try our conversational search powered by Generative AI!

PublishedContent event

Vote:
 

Hi everyone,

I'm trying to attach to IContentEvents.PublishedContent event, get external URL of the page, and do something with HTML.
ContentEventArgs.Content contains updated values, but when I try to parse http://localhost:65316/about-us/news-events/events/ using System.Net.Http.HttpClient, I get the old values.

The code looks like this:

[InitializableModule]
[ModuleDependency(typeof(ServiceContainerInitialization))]
public class DependencyResolution : IConfigurableModule
{
    private IContentEvents _contentEvents;

    public void ConfigureContainer(ServiceConfigurationContext context)
    {
        ...
    }

    public void Initialize(InitializationEngine context)
    {
        if (_contentEvents == null)
        {
            _contentEvents = ServiceLocator.Current.GetInstance();
        }

        _contentEvents.PublishedContent += contentEvents_PublishedContent;
    }
        

    void contentEvents_PublishedContent(object sender, ContentEventArgs e)
    {
        if (e.Content is PageData)
        {                
            var myService = ServiceLocator.Current.GetInstance();
            searchService.DoSomething((PageData)e.Content);
        }
    }

    public void Uninitialize(InitializationEngine context)
    {
        if (_contentEvents == null)
        {
            _contentEvents = ServiceLocator.Current.GetInstance();
        }

        _contentEvents.PublishedContent -= contentEvents_PublishedContent;
    }
}

I tested this with Alloy, versions 8.0 and 8.8

Any help would be greatly appreciated!

#122374
Jun 01, 2015 15:16
Vote:
 

There's no edit button for the first post...

Here's the modified code:

[InitializableModule]
[ModuleDependency(typeof(ServiceContainerInitialization))]
public class DependencyResolution : IConfigurableModule
{
    private IContentEvents _contentEvents;

    public void ConfigureContainer(ServiceConfigurationContext context)
    {
	   ...
    }

    public void Initialize(InitializationEngine context)
    {
        if (_contentEvents == null)
        {
            _contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
        }

        _contentEvents.PublishedContent += contentEvents_PublishedContent;
    }

    void contentEvents_PublishedContent(object sender, ContentEventArgs e)
    {
        if (e.Content is PageData)
        {
            var externalUrl = GetExternalUrl(e.ContentLink);
            using (var client = new HttpClient())
            {
                var html = client.GetStringAsync(externalUrl).Result;
            }
        }
    }

    public static string GetExternalUrl(ContentReference content)
    {
        var internalUrl = UrlResolver.Current.GetUrl(content);
        var url = new UrlBuilder(internalUrl);
        Global.UrlRewriteProvider.ConvertToExternal(url, null, Encoding.UTF8);

        string externalUrl = HttpContext.Current == null
                                 ? UriSupport.AbsoluteUrlBySettings(url.ToString())
                                 : HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + url;

        return externalUrl;
    }

    public void Uninitialize(InitializationEngine context)
    {
        if (_contentEvents == null)
        {
            _contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
        }

        _contentEvents.PublishedContent -= contentEvents_PublishedContent;
    }
}

client.GetStringAsync() inside contentEvents_PublishedContent method returns old HTML.

#122375
Jun 01, 2015 15:29
Vote:
 

I've managed to get correct html like this:

private void contentEvents_PublishedContent(object sender, ContentEventArgs e)
{
    if (e.Content is PageData)
    {
        new Task(() => { ParsePage(e.ContentLink); }).Start();
    }
}

Is there a better solution? Thanks!

#122382
Edited, Jun 01, 2015 16:13
Vote:
 

I would guess that the reason you see "old" data is that your event handler is executed before the event handler that evicts the page from cache. Try to change your type in ModuleDependency attribute to EPiServer.Web.InitializationModule

#122433
Jun 02, 2015 15:18
Vote:
 

Thank you!

I thought I could use one module for both StructureMap and content events.

#122436
Jun 02, 2015 15:33
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.