Insights

How To Quickly Publish A List Of All Items In Sitecore

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!

👋 Hey Sitecore Enthusiasts!

Sign up to our bi-weekly newsletter for a bite-sized curation of valuable insight from the Sitecore community.

What’s in it for you?

  • Stay up-to-date with the latest Sitecore news
  • New to Sitecore? Learn tips and tricks to help you navigate this powerful tool
  • Sitecore pro? Expand your skill set and discover troubleshooting tips
  • Browse open careers and opportunities
  • Get a chance to be featured in upcoming editions
  • Learn our secret handshake
  • And more!
Sitecore Snack a newsletter by Fishtank Consulting
 

Meet 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.

Connect with Gorman