Friday, April 29, 2022

Publishing Folder Programmatically-Using Sitecore Scheduling agent

 How to publish multiple folders/root items using Sitecore scheduling Agent?

Solution:  Use the code below…

public void Run()
{
    // set publish options
    Database sourceDatabase = Sitecore.Configuration.Factory.GetDatabase("master");

    Item settingItem = sourceDatabase.GetItem("/sitecore/content/
PublishContentFolderSetting");
    var folderList = settingItem.GetMultiListValueItems("FolderList");

    var TargetDbList = settingItem.GetMultiListValueItems("TargetDatabaseList");
    foreach(var targetDb in TargetDbList)
    {
        foreach(var folderPath in folderList)
        {
            Database targetDatabase = Factory.GetDatabase(targetDb.Fields["Target database"].Value);

            PublishOptions myOptions = new PublishOptions(sourceDatabase, targetDatabase,
PublishMode.Smart,
Sitecore.Data.Managers.LanguageManager.DefaultLanguage, DateTime.Now);

            myOptions.RootItem = sourceDatabase.Items[folderPath.Paths.Path];
            myOptions.Deep = true;

            Publisher myPublisher = new Publisher(myOptions);
            // publish in AsyncMode

            var myPublishJob = myPublisher.PublishAsync();
            // publish in SyncMode

            myPublishJob.Start();
        }
 }  
}


Folder Setting Item:





Patch Config:


<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"
    <sitecore>
        <scheduling>
            <agent type="Website.SchedulingAgent.PublishContentFolderAgent,
Website.AssembalyName" method="Run" interval="06:00:00"/>
        </scheduling>
    </sitecore>
</configuration>

No comments:

Post a Comment

How to Create a Public Link Using the Sitecore Content Hub REST API

Creating a public link using the Sitecore Content Hub REST API is a straightforward process that enables you to share content externally whi...