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();
}
}
}
}
}