Pass Sitecore Items in .Net Class with Scheduler: Part-3

In my previous blog, we had discussed Create and Configure Scheduler in Sitecore. At the end of this series of Sitecore Scheduler blogs, I will discuss with you about Pass Sitecore Items in the .NET class with Scheduler.

Suppose in your .Net class your scheduler logic has some condition based on the Sitecore Items field values and to access those values you can opt for one of the below options:

  1. Get the Sitecore Item by its Item ID and write the logic of the get the respective field value by Item as below:
    var scItem = Sitecore.Context.Database.GetItem(new ID("{11111111-1111-1111-1111-111111111111}"));
    var fieldValue = scItem.Fields["Field Name"].Value;
    
    In the above approach, you have restricted the user for a constant ID and in the case of multiple items, your line of code will increase.

  2. Pass Sitecore Items into the Items field and get the values of those Sitecore Items fields by code. In this approach, you can pass multiple items and dynamically you can change the Sitecore items by the content editor.

In this blog, I will discuss the second option with you. In my previous blog, we have discussed the Items field in Scheduler Task, which will be used for Pass Sitecore items to .Net Class.

In the Scheduler task, you need to specify the Sitecore item in the Items field. If you want to pass a single item, then you can write either Item GUID or Path of that Item. In case if you want to pass multiple items then you need to write the path of Sitecore items with Pipe sign "|" separation as below:

“/sitecore/content/Benefit Categories/Automotive|/sitecore/content /Courses/Marinas”

"The pipe (“|”) separator is used to select several paths in one expression"

Now, update your .NET class code as below to read the multiple passed Sitecore items and respective fields value. You need to write a class that will have the logic of functionality you want to execute.

namespace Helixbase.Foundation.Scheduler
{
    public class DataImportScheduler
    {
        public void DataImport(Item[] items, Sitecore.Tasks.CommandItem command, Sitecore.Tasks.ScheduleItem schedule)
        {
            foreach (var item in items)
            {
                if (item != null && !string.IsNullOrWhiteSpace(item.Fields["Field Name"].Value))
                {
                    var fieldsValue = item.Fields["Field Name"].Value;               
                    //Code here
                }
            }
        }
    }
}

Happy Sitecoreing ðŸ˜Š

Comments

Popular posts from this blog

Sitecore Installation Error: Failed to Start Service 'Sitecore Marketing Automation Engine'

Import CSV Data in Sitecore Using PowerShell: Part-3

Sitecore Technology MVP Journey 2022