13

I have a page that is completely component based. All images, html, etc are individual components with datasources in a grid system. The datasources are in a folder under the page called pagedata or in a site level folder called sitedata.

What I want to accomplish is to have the components follow the same publishing date restrictions as the parent page. Right now if I want an image to switch out on June 1, I have to version the image datasource item and add publishing restrictions to it. Or I can make a new image item (separate from the current image datasource item). Then the presentation details of the page item, in the June 1 version, will switch out the image datasource on June 1st.

If I had a page with 10 images and 3 html components, it would be difficult to create 13 new items and then update the datasources for all the components. Or go to all 13 datasource items and set publishing restrictions on them all.

Anyone have a simpler pipeline that allows adding publishing restrictions to a component based page? I have not researched how SXA does it yet.

4
  • If you always want to use publishing restrictions from page, I would simply add item saved event handler and update all linked datasources/children. Commented May 24, 2017 at 14:45
  • 1
    The hard part is determining which datasource item version to associate with the given page version. So page version 5 is associated to image datasource item version 2 and html datasource item version 4. Since this could happen on each page version, tracking all item versions would be important.
    – Chris Auer
    Commented May 25, 2017 at 0:48
  • If I have understood well, you want your components to have the same publish restriction date as the parent item? Example, if parent page has restriction date to June 1st, its components also should be restricted to June 1st? Commented May 31, 2017 at 19:45
  • 4
    I see two options: add a item saved event handler like the others suggested with the complex logic or overwrite the publishing pipeline and create your own publishing processor that checks the restrictions on the item. Commented Jul 31, 2017 at 10:54

1 Answer 1

1

Here's some code that will let you synchronize the Publishing Settings on DataSources that are used on an Items Renderings. You would definitely want to limit it to occur only on specific template types. Plus, some other considerations is Workflow. This would definitely be something that you would need to do if these items are controlled by workflow. Here is a Marketplace module that will do it for you and there is a blog post with a few details about it - https://marketplace.sitecore.net/Modules/D/Data_Source_Workflow_Module.aspx?sc_lang=en.

Here is the config to patch in the following code:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
      <event name="item:saved">
        <handler type="SharedSource.Foundation.DataSourcePublishingRestrictions.Events.ItemSaved.DataSourcePublishingRestrictions, SharedSource.Foundation.DataSourcePublishingRestrictions" method="OnItemSaved"/>
      </event>
    </events>
    <settings>
      <setting name="SharedSource.Foundation.DataSourcePublishingRestrictions.AllowedTemplates" value="{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}|{00000000-0000-0000-0000-000000000000}"/>
    </settings>
  </sitecore>
</configuration>

Here is the code for patching into the SaveItem event that will synchronize the Publishing settings like you asked:

// Copyright (c) [2017] [David Walker] - MIT License - see License.txt
using System;
using System.Linq;
using Sitecore.Data.Items;
using Sitecore.Events;
using Sitecore.SecurityModel;

namespace SharedSource.Foundation.DataSourcePublishingRestrictions.Events.ItemSaved
{
    // Sychronize PublishingRestrictions across DataSources
    public class DataSourcePublishingRestrictions 
    {
        public void OnItemSaved(object sender, EventArgs args)
        {
            if (args == null) return;

            var savedItem = Event.ExtractParameter(args, 0) as Item;
            if (savedItem?.Database == null || savedItem.Database.Name.ToLower() != "master") return;

            // Only do it for specific templates
            if (!Common.Templates.Any() || !Common.Templates.Contains(savedItem.TemplateID.ToString())) return;

            //if (savedItem.Publishing.NeverPublish || savedItem.Publishing.IsPublishable()) return;

            if (savedItem.Publishing.ValidFrom == DateTime.MinValue) return;

            var masterDatabase = savedItem.Database;

            foreach (var rendering in savedItem.Visualization.GetRenderings(Sitecore.Context.Device, true))
            {
                var renderingDatasource = rendering.Settings.DataSource;
                if (renderingDatasource == null) continue;

                var renderingItem = masterDatabase.GetItem(renderingDatasource);
                if (renderingItem == null) continue;

                if (renderingItem.Publishing.ValidFrom == savedItem.Publishing.ValidFrom &&
                    renderingItem.Publishing.ValidTo == savedItem.Publishing.ValidTo) continue;

                // Publishing needs synched
                Sitecore.Diagnostics.Log.Info("Would have updated ValidFrom of " + renderingItem.ID + " to " + savedItem.Publishing.ValidFrom, this);
                Sitecore.Diagnostics.Log.Info("Would have updated ValidTo of " + renderingItem.ID + " to " + savedItem.Publishing.ValidTo, this);

                // Sync the Publihsing Settings
                using (new SecurityDisabler())
                {
                    renderingItem.Editing.BeginEdit();

                    renderingItem.Publishing.ValidFrom = savedItem.Publishing.ValidFrom;
                    renderingItem.Publishing.ValidTo = savedItem.Publishing.ValidTo;
                    renderingItem.Publishing.NeverPublish = savedItem.Publishing.NeverPublish;

                    renderingItem.Editing.EndEdit();
                }
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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