Skip to main content

Publishing issue with subitems of "related items" from Experience Editor

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.

https://doc.sitecore.com/users/90/sitecore-experience-platform/en/publish-an-item-to-your-website.html

Comments

Popular posts from this blog

Sitecore Publishing target and Solr Configuration

Creating a new publishing target is very helpful if your Sitecore environment is setup in multiple content delivery servers. We can publish data from Master database to one or more publishing target databases. Main thing to notice here is Master database contains all versions of any content or assets while publishing target database contains single latest version for each language.      This post aims to provide systematic instructions to set up publishing target. It comprises of a target database (where the content will be published) and publishing target definition item in master database that points to that target database.             Post creation of publishing target need to take care of Solr configuration. This post cove master slave Solr configuration and onPublishEndAsync strategy. We are using 1 Solr Master instance and 6 Slave instance (2-EU, 2-Ap, 2- NA).     1   So...

Setup Sitecore Habitat v9.0 project in simple way

In this article , I would describe how to setup the Sitecore habitat V9.0 project with Sitecore Experience Platform 9.0 rev in very simple way. I used below system configuration to setup this project: 1. Windows 10 (32/64-bit) 2. IIS 10.0 3. .NET Framework 4.6.2 4. Microsoft Visual Studio 2017 5. Microsoft SQL Server 2017 6. Solr 6.6.2 7. Microsoft PowerShell® version 5.0 8. Web Platform Installer 5.0. 9. Web Deploy 3.6 for Hosting Servers 10. Node-v8.9.4-x64 Follow below steps to setup the project. A).   Habitat require Sitecore instance. Setup the clean Sitecore 9.0 instance. Ex : http://demo.local.com , Directory location : C:\inetpub\wwwroot\demo.local.com   For more info visit -   https://dev.sitecore.net/ B). Download the Habitat V9.0 project from Github site :  https://github.com/Sitecore/Habitat/     Note  :  When you download the packages, it is possible that the .ZIP packages are marked as blocke...

Web Deploy setup and configuration with Sitecore

Web Deploy empowers Visual Studio and WebMatrix to help developers streamline the deployment of Web applications to Microsoft IIS Web servers or to Microsoft Azure Websites. In addition, integration with Web Platform Installer allows developers to simply and easily install community webapplications.   This post describe the installation of Web Deploy and Configuration with Sitecore instance.  This post will be very useful if you want to sync your Sitecore media items with all of the CD's environment (Distributed environment) . In one of my previous project, I was keeping all the media items in file system and synchronizing all the files from CM to Cd's server. This solution is tested with 1 CMS and 6 CD's server (2-EU, 2- NA, 2- AP) with more than 5 Lakh files.  1.       Software Pre-requisites           a.       OS – Window 7/ Windows Server 2008 R2 Enterprise       ...