How To Quickly Publish A List Of All Items In Sitecore

Example Subheading

October 29, 2022

By Gorman Law

Introduction

The most common way to work with published items is to use the publish:itemProcessed event. This will fire once for every item processed during publish. However, there might be times when one might want to work with the entire list of published items, or might want a more efficient way to do it.

ProcessedPublishingCandidates

This is a new property which Sitecore introduced to collect all the processed items.

The Code

ProcessedPublishingCandidates works within the publish pipeline, so first, we need a new config file that will patch our custom publish processor. Add the config file to your wwwroot\[your site here]\App_Config\Include\Project folder.


<?xml version="1.0" encoding="utf-8" />
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <publish>
            <processor patch:after="*[@type='Sitecore.Publishing.Pipelines.Publish.ProcessQueue, Sitecore.Kernel']" type="YourProject.Helpers.Pipelines.CustomPublisher, YourProject.Helpers"></processor>
            </publish>
        </pipelines>
    </sitecore>
</configuration>

Now create a new class named CustomPublisher that extends PublishProcessor and in the class, add the following code.

    
using Sitecore.Data.Items;
using Sitecore.Publishing.Pipelines.Publish;

namespace YourProject.Helpers.Pipelines
{
    public class CustomPublisher : PublishProcessor
    {

        public override void Process(PublishContext context)
        {
            if (context == null)
                return;
            
            // grabs list of items created, updated, and deleted
            var processedItems = context.ProcessedPublishingCandidates.Keys.Select(x => context.PublishOptions.TargetDatabase.GetItem(x.ItemId)).Where(y => y != null);

            foreach(Item item in processedItems) {
                // do item processing here
            }
            
        }
    }
}
    

Make sure to change around the namespace to fit into your project.

Conclusion

Hope this code helps your publishing needs!

Gorman Headshot

Gorman Law

Full Stack Developer

Gorman is a Full Stack Developer and a University of Calgary alumni who has a background in Canada's financial industry. Outside of work, he likes to go rock climbing, try out new ice cream places, and watch sports.