2

When an item is set to unpublishable, all methods to get the item for a rendering with GlassMapper return null.

public class PageHeaderController : GlassController
{
    public override ActionResult Header()
    {
        BaseItem model = SitecoreContext.GetCurrentItem<BasePage>(); // null
        model = GetLayoutItem<BasePage>(); // null
        model = SitecoreContext.GetItem<BasePage>(RenderingContext.Current.Rendering.DataSource); // null
        return View(model);
    }
}

How can we get the item in that case to be able to provide a publishing preview for editors for items that are set to be published in the future?

The problem occurs whether we use the "Never Publish" setting or set the version to unpublishable or even when the version is set to be published in the future.

The preview is really running with the master database.enter image description here

  • How do you obtain SitecoreContext? From what I know there is no static method SitecoreContext.GetCurrentItem? – Marek Musielak Mar 27 '17 at 14:12
  • @MarekMusielak SitecoreContext is a member of Glass.Mapper.Sc.Web.Mvc.GlassController. – Residuum Mar 27 '17 at 14:29
  • Right, I missed that. – Marek Musielak Mar 27 '17 at 14:37
  • I just want to make sure that I am understanding your question correctly. The business case is that your authors create an item, but said item is not yet ready for publishing. Either via custom workflow, Standard Values, manual setting by the authors, etc. the "Never Publish" field is checked to prevent accidental/premature publishing. However, the authors want to be able to view the rendered page for the item using the "Preview" feature. Is that correct? – Zachary Kniebel Mar 27 '17 at 15:12
  • 1
    Just out of curiosity, is there any reason why you are using the "Never Publish" setting for this rather than using Workflow? I can post this with further detail as an answer but only if you request it, since it doesn't technically answer your original question. doc.sitecore.net/sitecore_experience_platform/content_authoring/… – Zachary Kniebel Mar 27 '17 at 15:45
3

This is normal behavior for the Preview feature. The purpose of this feature is to show you what the page will look like once published. Therefore the behavior you're encountering sort of makes sense, considering the page would simply not be published. To be fair, a more graceful approach (e.g. warning message) would be appreciated.

There are a couple of ways to get around it.

(1) If it's just for a one-off preview, you can change the preview URL like so. If Sitecore generates:

http://mysite.local/?sc_itemid=%7BFC3FE8C5-FAE6-4C67-8F21-B3757D143D4A%7D&sc_mode=preview&sc_lang=en-US&sc_site=mysite

Change it to:

http://mysite.local/?sc_itemid=%7BFC3FE8C5-FAE6-4C67-8F21-B3757D143D4A%7D&sc_mode=normal&sc_lang=en-US&sc_site=mysite&sc_database=master

Note the sc_mode and sc_database parameters.

(2) In code, you could disable site filtering by setting Sitecore.Context.Site.DisableFiltering to true. More details in this answer here: https://stackoverflow.com/a/20390596/1327412

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.