November Happy Hour will be moved to Thursday December 5th.

How to fetch draft/preview content?

Vote:
 

Hi. I want to fetch the draft content to be able to preview before publishing.
I've tried using this approach: https://docs.developers.optimizely.com/content-management-system/v1.5.0-content-delivery-api/docs/customizing-content-delivery-api-for-edit-view
However, that code is for CMS 11(?).
When I registered my custom ContentLoaderService, a completely empty one that just overrides ContentLoaderService, I get a generic 500 response for whatever content I try to fetch 😞.

#302404
May 26, 2023 12:25
Vote:
 

I managed to find out how to do it. The custom content loader service should look like this:

public class CustomContentLoaderService : ContentLoaderService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        protected CustomContentLoaderService() : base()
        {
        }

        public CustomContentLoaderService(IHttpContextAccessor httpContextAccessor, IContentLoader contentLoader, IPermanentLinkMapper permanentLinkMapper, IContentProviderManager providerManager) : base(contentLoader, permanentLinkMapper, providerManager)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        protected override bool ShouldContentBeExposed(IContent content)
        {
            if (_httpContextAccessor.HttpContext.Request.Query.TryGetValue("draft_content", out var _))
            {
                return true;
            }
            return base.ShouldContentBeExposed(content);
        }
 }
#302716
May 31, 2023 12:59
Vote:
 

Hi Eric

ContentLoadedService has been moved to an Internal namespace now. So is there a workaround for these classes that alow us to skip calling internal namespaces directly and avoid future code breakages? Or should we request for these to be moved to public API instead?

Regards

Ritu

#303041
Jun 06, 2023 15:24
Erik Henningson - Jun 07, 2023 6:14
I have the same question. It feels odd that the documentation says to use the internal namespace.
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.