Sitecore Publishable flag makes it impossible to G

2019-04-25 09:46发布

问题:

I'm using sitecore 6.6.0 (rev. 120918). From the sitecore admin portal, I go and turn off the Publishable flag of an item (see image).

After this, Sitecore.Data.Database.GetDatabase("master").GetItem("{itemID}") returns null.

If I turn ON the Publishable flag again, GetItem() returns the correct item. What's the reason for this behaviour? Publishable setting controls the ability to publish to the Web database. Why does it effect the GetItem() API call to the master database?

回答1:

I've found a way around this for cases where you really need to read from the ContentDatabase (i.e. master database) and don't want items to publish. This could be user generated content, for example. In Active Commerce, we run into this with content such as Wish Lists which are always read from master in preview or non-staged environments, and are accessed via web service in staged environments.

By setting Sitecore.Context.Site.DisableFiltering to true, the filtering of unpublishable items will be disabled. I've implemented a simple IDisposable that allows you to temporarily disable filtering, with an optional condition.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ActiveCommerce.SitecoreX
{
    public class ItemFilteringDisabler : IDisposable
    {
        private readonly bool _disableCondition = true;

        public ItemFilteringDisabler()
        {
            Sitecore.Context.Site.DisableFiltering = true;
        }

        public ItemFilteringDisabler(bool disableCondition)
        {
            _disableCondition = disableCondition;
            if (_disableCondition)
            {
                Sitecore.Context.Site.DisableFiltering = true;
            }
        }

        public void Dispose()
        {
            if (_disableCondition)
            {
                Sitecore.Context.Site.DisableFiltering = false;
            }
        }
    }
}

Example use:

using (new ItemFilteringDisabler(!Sitecore.Context.PageMode.IsNormal))
{
    Sitecore.Data.Database.GetDatabase("master").GetItem("{itemID}");
}


回答2:

I have now found that the reason is the sitecore preview feature. In my previous checks with GetItem() I happen to have used the preview feature earlier as a sitecore admin. After that my whole public site goes into preview mode (bit annoying) therefore the item ceases to be accessible (even via the master database API call).

From what I have discovered these are the rules:

  • If the item is NOT publishable and if the website is in preview mode, Sitecore.Data.Database.GetDatabase("master").GetItem("{itemID}") returns null.

  • The unpublishable item also cannot be previewed. Sitecore doesn't even seem to load the sublayout for the item.

I'm going to ask in a separate question on why the preview feature places such restrictions on items which are not publishable.



标签: sitecore