Preventing Publishing

Preventing Publishing

Preventing Publishing

This snippet uses a fields collection to reference the field
  • Returns null if the field does not exist or the field value is null
  • Does not generate inline editing controls for the Page Editor
  • Useful for field types other than simple field types
Code that publishes or imports data typically accesses the Master database by name rather than referencing the context database
  1. Use a security disabler to allow changes and place in a using block to ensure cleanup occurs
  2. Create an object for the database where the item is located - replace SitecoreDB with the name of a Sitecore database, such as “master”
  3. Create an object for the target item in the database- replace itempath with the relative path to the item in the Content Tree, such as “home/about/item1” or start the path with a backslash (/) to indicate an absolute path, such as “/sitecore/content/home/about/item1”– a null value will be set in either case if the item does not exist - Sitecore does compare case when evaluating item paths
  4. Verify the target item exists
  5. Open the edit state for the item
  6. Enable the never publish setting for the item
  7. Close the edit state for the item


C# Web Control Code

using (new Sitecore.SecurityModel.SecurityDisabler())

{

      Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("SitecoreDB");

      Sitecore.Data.Items.Item item = db.Items["itempath"];

      if (item != null)

      {

             item. ItemEditing.BeginEdit();

             item.Fields["__never publish"].Value = "1";

             item. ItemEditing.EndEdit();

      } }