Try our conversational search powered by Generative AI!

Bartosz Sekula
Sep 28, 2021
  2608
(2 votes)

How to resolve current content context

With the release of CMS UI 11.36.2 it is now possible to resolve the current loaded content context from anywhere you may need.

It might be useful if you need any custom logic in any of the Content Events:

[ModuleDependency(typeof(EPiServer.Shell.UI.InitializationModule))]
public class CustomModule : IConfigurableModule
{
    public void Initialize(InitializationEngine context)
    {
        context.Locate.ContentEvents().CreatedContent += CreatedContent;
        context.Locate.ContentEvents().CreatingContent += CreatingContent;
    }

    private void CreatingContent(object sender, ContentEventArgs e)
    {   
        var currentContentContext = ServiceLocator.Current.GetInstance<CurrentContentContext>();
        // we know which content context initiated the CREATE operation        
        var currentContentLink = currentContentContext.ContentLink;

        var newlyCreatedContentLink = e.ContentLink; // --> null as at this point the ContentReference was not assigned yet
    }

    private void CreatedContent(object sender, ContentEventArgs e)
    {
        var newlyCreatedContentLink = e.ContentLink; // this is the ContentReference of the newly created content item
        // but we might need the ContentReference of the content that was being viewed when the CREATE operation was run
        var currentContentContext = ServiceLocator.Current.GetInstance<CurrentContentContext>();
        // this gives us the content context the editor was in
        var currentContentLink = currentContentContext.ContentLink;
    }

    public void Uninitialize(InitializationEngine context)
    {
        context.Locate.ContentEvents().CreatedContent -= CreatedContent;
        context.Locate.ContentEvents().CreatingContent -= CreatingContent;
    }

    public void ConfigureContainer(ServiceConfigurationContext context)
    {

    }
}

You might also need to know the current context in metadata aware components, like for example in this Editor Descriptor:

public class FooPageType : PageData 
{
    [EditorDescriptor(EditorDescriptorType = typeof(CollectionEditorDescriptor<OfferDetailsItem>))]
    public virtual IList<OfferDetailsItem> OfferDetailsItems { get; set; }
}

public class OfferDetailsItem
{
    [Display(Order = 1)]
    [UIHint("test")]
    public string Label { get; set; }
}

[PropertyDefinitionTypePlugIn]
public class OfferDetailsItemPropertyList : PropertyList<OfferDetailsItem>
{
}

[EditorDescriptorRegistration(TargetType = typeof(string), UIHint = "test")]
public class CustomXhtmlDescriptor : StringEditorDescriptor
{
    private readonly CurrentContentContext _currentContent;

    public CustomXhtmlDescriptor(CurrentContentContext currentContent)
    {
        _currentContent = currentContent;
    }

    public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
    {
        base.ModifyMetadata(metadata, attributes);

        var ownerContent = metadata.FindOwnerContent(); // --> will return null because the object we annotate is not ContentData but simple POCO
        // https://world.optimizely.com/forum/developer-forum/CMS/Thread-Container/2016/1/current-content-for-propertylist-item-selection-factory/
        // and
        // https://world.optimizely.com/forum/developer-forum/CMS/Thread-Container/2021/9/tinymce-customization-depending-on-page-context/
        var currentContent = _currentContent.ContentLink; // --> will return correct page id
    }
}

Related forum posts that describe the problem:

https://world.optimizely.com/forum/developer-forum/CMS/Thread-Container/2021/9/tinymce-customization-depending-on-page-context/

https://world.optimizely.com/forum/developer-forum/CMS/Thread-Container/2020/12/get-value-of-contextepi-cms-contentdata/#234848

https://world.optimizely.com/forum/developer-forum/CMS/Thread-Container/2016/1/current-content-for-propertylist-item-selection-factory/

Sep 28, 2021

Comments

Tomas Hensrud Gulla
Tomas Hensrud Gulla Sep 28, 2021 11:54 AM

Will there be release notes describing this?

Bartosz Sekula
Bartosz Sekula Sep 28, 2021 11:55 AM

Yes, documentation is scheduled to be published. It will be available soon.

Tomas Hensrud Gulla
Tomas Hensrud Gulla Sep 28, 2021 11:59 AM

Excellent work, by the way. Thanks! :-)

Ravindra S. Rathore
Ravindra S. Rathore Sep 30, 2021 07:11 AM

👍

Please login to comment.
Latest blogs
How to add a custom property in Optimizely Graph

In the Optimizely CMS content can be synchronized to the Optimizely Graph service for it then to be exposed by the GraphQL API. In some cases, you...

Ynze | May 9, 2024 | Syndicated blog

New Security Improvement released for Optimizely CMS 11

A new security improvement has been released for Optimizely CMS 11. You should update now!

Tomas Hensrud Gulla | May 7, 2024 | Syndicated blog

Azure AI Language – Key Phrase Extraction in Optimizely CMS

In this article, I demonstrate how the key phrase extraction feature, offered by the Azure AI Language service, can be used to generate a list of k...

Anil Patel | May 7, 2024 | Syndicated blog

Webinar: Get Started with AI within Optimizely CMS

Join us for the webinar "Get Started with AI in Optimizely CMS" on Wednesday, May 8th. Don't forget to register!

Luc Gosso (MVP) | May 7, 2024 | Syndicated blog

Search & Navigation: Indexing job new features

From Episerver.Find version 16.1.0, we introduced some new features that make the indexing job in CMS more flexible and efficient: Support continuo...

Vinh Cao | May 7, 2024

Exclude content from search engines

Best practices for excluding your Optimizely CMS test content from search engines like Google, along with some tips on what to do—and what not to...

Tomas Hensrud Gulla | May 7, 2024 | Syndicated blog