Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

IContentLoader returns unpublished content

Vote:
 

Bit of a noob quesiton.

I have the following in a view used to render my page:

@{
    var contentLoader = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance();
    var imageBlock = contentLoader.Get(Model.CurrentPage.ImageBlock);

    if (imageBlock != null)
    {
     //  .....      
    }
}

And the following property in my page model:

 [AllowedTypes(new[] { typeof(ImageBlock) })]
 public virtual ContentReference ImageBlock { get; set; }

I add a new, unpublished ImageBlock block to the page, and publish the page. Why does IContentLoader return my unpublished block - shuoldn't it return null?

Or is published status something you need to check for in addition?

Cheers

#139971
Oct 12, 2015 14:25
Vote:
 

If your're signed in as an editor you will also see unpublished content.

And yes, you have to check publish status and access rights if you're working against IContentLoader directly.

#140183
Oct 12, 2015 15:53
Vote:
 

You would generally filter the content returned from IContentLoader to make sure it's suitable for site visitors. In most projects I work on we'll have a content filtering interface and service like the example below:

using System.Collections.Generic;
using EPiServer.Core;

public interface IContentFilterService
{
    IEnumerable<T> Filter<T>(IList<T> contentItems) where T : IContent;
    IEnumerable<T> Filter<T>(IList<T> contentItems, bool checkPublished, bool checkAccess) where T : IContent;
}

using System.Collections.Generic;
using System.Linq;
using EPiServer.Core;
using EPiServer.Filters;

public class ContentFilterService : IContentFilterService
{
    private readonly IPublishedStateAssessor publishedStateAssessor;

    public ContentFilterService(IPublishedStateAssessor publishedStateAssessor)
    {
        this.publishedStateAssessor = publishedStateAssessor;
    }

    public IEnumerable<T> Filter<T>(IList<T> contentItems) where T : IContent
    {
        return Filter(contentItems, true, true);
    }

    public IEnumerable<T> Filter<T>(IList<T> contentItems, bool checkPublished, bool checkAccess) where T : IContent
    {
        var list = contentItems.Cast<IContent>().ToList();

        if (checkPublished)
        {
            new FilterPublished(publishedStateAssessor).Filter(list);    
        }

        if (checkAccess)
        {
            new FilterAccess().Filter(list);    
        }

        return list.Cast<T>();
    }
}
#140187
Edited, Oct 12, 2015 16:25
Vote:
 

Hi,

Yes, as Johan said, the content returned will depend on the current user's access right. 

We have a static class (EPiServer.Filters.FilterForVisitor) which provides some methods to help us quickly filter contents which are not supposed to be shown to current end user.

Regards.

/Q

#140188
Oct 12, 2015 16:26
* 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.