Try our conversational search powered by Generative AI!

Get language from SaveContentEventArgs

Vote:
 

I have a custom function which is called on publish, similar to this article

public class EventInitializationModule : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var contentEvents = ServiceLocator.Current.GetInstance();
            contentEvents.PublishingContent += PublishContent;
        }
        void PublishContent(object sender, ContentEventArgs e)
        {
            myClass c = new myClass();
            c.PublishEvent(sender, e);
        }
        public void Uninitialize(InitializationEngine context)
        {
            var contentEvents = ServiceLocator.Current.GetInstance();
            contentEvents.PublishingContent -= PublishContent;
        }
    }

When PublishContent is hit, it is being passed SaveContentEventArgs instead of the parent ContentEventArgs.

Inside myClass.PublishEvent() I want to be able to get the language of the content being published. Is this possible? All the documentation points to using ContentLanguageEventArgs but there seems to be no way to pass that type instead of SaveContentEventArgs.

Using EPiServer 9.8

#180502
Jul 12, 2017 21:44
Vote:
 

If you look at your variable e it has a property called Content. This is the content that is currently being saved.

Try casting it to ILocalizable and you can see which Language it is published in.

private void PublishContent(object sender, ContentEventArgs e)
{
    var localizable = e.Content as ILocalizable;
    if (localizable == null)
        return;

    CultureInfo currentLanguage = localizable.Language;
}



#180503
Jul 12, 2017 23:08
Vote:
 

This was perfect, thanks!

#180548
Jul 13, 2017 21:38
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.