Publishing issue with subitems of
"related items" from Experience Editor
Hello
friends, I was trying to publish item from experience editor with publishing
option “related item” but subitems of related item was not getting published. I
would like to explain scenario and solution here.
A) Publishing “Related Items”
To publish
all the related items to the selected item, select Publish related items. Out of the
box pipeline cover below “Related items” while publishing:
·
Clones
– items that are clones of the selected item.
·
Media
items – media items that are referenced by the selected item.
·
Content
items - data sources that are referenced by the selected item.
·
Design
items - renderings, layouts and templates used by the selected item.
·
Aliases
– items that are aliases of the selected item.
B)
It does not cover subitems of “related
item”
I had below requirement where subitems of selected data source item was
not getting published.
·
Promos
o
Home
Page Promos => Data source Item
§ Promo Subitems-1
§ Promo Subitems-2
§ Promo Subitems-3
§ Promo Subitems-4
C)
Customized the pipeline and replaced
as below
1. I made a custom template to identify
the data source item for which I need to consider subitems. Ex : “Promo Folder”
2. Kept all child items under created
template. Ex : “Promo Folder”
a. Home Page Promos => Data source Item created by template “Promo Folder”
· Promo Subitems-1
· Promo Subitems-2
· Promo Subitems-3
· Promo Subitems-4
3. Created a custom processor based on Sitecore.Publishing.Pipelines.GetItemReferences.AddItemLinkReferences
4. Modified the GetItemReferences () and implemented our logic there to consider
subitems of data source item
5. Replaced predefined processor with
custom one
D) Customized code
namespace AddSubItemsOfRelatedItem
{
public class AddSubItemsOfRelatedItem : AddItemLinkReferences
{
protected override List<Item> GetItemReferences(PublishItemContext context)
{
Assert.ArgumentNotNull((object)context, nameof(context));
List<Item> objList = new List<Item>();
IEnumerable<Item>
itemReferences;
Item datasourceItem;
if (context.PublishOptions.Mode != PublishMode.SingleItem)
return objList;
switch (context.Action)
{
case PublishAction.PublishSharedFields:
Item sourceItem =
context.PublishHelper.GetSourceItem(context.ItemId);
if (sourceItem == null)
return objList;
itemReferences = this.GetReferences(sourceItem, true);
datasourceItem =
itemReferences.Where(item => item.TemplateName == "Promo Folder").FirstOrDefault();
if (datasourceItem != null)
objList.AddRange(datasourceItem.GetChildren());
objList.AddRange(itemReferences);
break;
case PublishAction.PublishVersion:
Item versionToPublish =
context.VersionToPublish;
if (versionToPublish == null)
return objList;
itemReferences = this.GetReferences(versionToPublish, false);
datasourceItem =
itemReferences.Where(item => item.TemplateName == "Promo Folder").FirstOrDefault();
if(datasourceItem!=null)
objList.AddRange(datasourceItem.GetChildren());
objList.AddRange(itemReferences);
break;
default:
return objList;
}
return objList;
}
private IEnumerable<Item> GetReferences(Item item, bool sharedOnly)
{
Assert.ArgumentNotNull((object)item, nameof(item));
List<Item> source = new List<Item>();
ItemLink[] array =
((IEnumerable<ItemLink>)item.Links.GetValidLinks()).Where<ItemLink>((Func<ItemLink,
bool>)(link =>
item.Database.Name.Equals(link.TargetDatabaseName,
StringComparison.OrdinalIgnoreCase))).ToArray<ItemLink>();
if (sharedOnly)
{
array =
((IEnumerable<ItemLink>)array).Where<ItemLink>((Func<ItemLink, bool>)(link =>
{
Item sourceItem =
link.GetSourceItem();
if (sourceItem == null)
{
return false;
}
if
(!ID.IsNullOrEmpty(link.SourceFieldID))
{
return
sourceItem.Fields[link.SourceFieldID].Shared;
}
return true;
})).ToArray<ItemLink>();
}
foreach (Item obj in
((IEnumerable<ItemLink>)array).Select<ItemLink,
Item>((Func<ItemLink, Item>)(link =>
link.GetTargetItem())).Where<Item>((Func<Item, bool>)(relatedItem => relatedItem
!= null)).ToList<Item>())
{
source.AddRange(PublishQueue.GetParents(obj));
source.Add(obj);
}
return source.Distinct<Item>((IEqualityComparer<Item>)new ItemIdComparer());
}
}
}
E)
Replace processor with custom one
<processor type="AddSubItemsOfRelatedItem.AddSubItemsOfRelatedItem, AddSubItemsOfRelatedItem" patch:instead="processor[@type='Sitecore.Publishing.Pipelines.GetItemReferences.AddItemLinkReferences, Sitecore.Kernel']" resolve="true"/>
Thanks for
exploring J
Please visit
official site related to publishing information.


Comments
Post a Comment