How to publish multiple folders/root items using Sitecore scheduling Agent?
Solution: Use the code below…
{
// 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/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> <sitecore> <scheduling> <agent type="Website.SchedulingAgent.PublishContentFolderAgent, Website.AssembalyName" method="Run" interval="06:00:00"/> </scheduling> </sitecore></configuration>
No comments:
Post a Comment