Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore Publishable flag makes it impossible to GetItem() from Master database

Tags:

sitecore

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).

enter image description here

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?

like image 270
ravinsp Avatar asked May 30 '13 14:05

ravinsp


People also ask

Where does master database publish items to?

When you publish new items or updated items, the items are copied from the Master database to a publishing target, and from the publishing target, the website is launched. The default publishing target is the Web database.

Which of the following is used to publish all child items?

Publish Subitems – publishes all child items (and further descendants) of a parent item.

What is publishing in Sitecore?

The default publishing target in Sitecore is the Web database. You can choose to publish the entire website or a single item: Site publishing – publishes your entire website starting from the root of the content tree. You can publish a site from the Content Editor or from the Sitecore Desktop.


2 Answers

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}");
}
like image 190
nickwesselman Avatar answered Jan 03 '23 04:01

nickwesselman


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.

like image 25
ravinsp Avatar answered Jan 03 '23 05:01

ravinsp