0

I am trying to get to my site settings item in my tree to see what office id I need between my US site and my CA version. I have tried Sitecore.Context.Site but since this is a scheduled task it comes through null, so just wondering if there is a work around to try and get access in this class to my tree again.

enter image description here

3
  • Could you please provide more details? Do you have a single scheduled task that does the same thing for 2 different sites? Can you break it up into 2 scheduled tasks so that it's clear for which site it's running? You can always do Sitecore.Context.Database.GetItem(relevantSiteSettingsItemId). If you don't want to use the Item Id, you could also use the item path. Apr 2 at 17:39
  • This task is for processing our forms that we submitted. We then are doing a post to an external api that we need to send the office ID based on what is in the sitesettings for that site. The item ID wouldnt work since I need it dynamic, to tell me if sitesetting is for CA it gives me office id of 2 and if I am on US then it would give me 1. I think getting it by path might work since the id is different per site. Apr 2 at 17:49
  • If you are using path then it should be configurable via item so that if path change in near future then you just update the item otherwise item id also as a configurable item
    – Amit Kumar
    Apr 2 at 20:09
0

Depending on your scheduler implementation you can use SiteContextSwitcher to switch Sitecore.Context.Site to your actual site context.

First you need to get the website either by name:

var website = Factory.GetSite("yoursitename");

or if you have an item object you can get its site by the path

var siteInfo = Sitecore.Configuration.Factory.GetSiteInfoList().FirstOrDefault(x => item.Paths.FullPath.StartsWith(x.RootPath));    
var website = Sitecore.Configuration.Factory.GetSite(siteInfo.Name)

using (new SiteContextSwitcher(website))
{
   //implement your logic here
}
0

You can send Sitecore Items in .Net Class with Scheduler items and within the scheduler, you can access those items and perform your functionality.

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

enter image description here

Now, update your .NET class code as below to read the multiple passed Sitecore items and respective fields' values. 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
                }
            }
        }
    }
}

See blog post for more details: Pass Sitecore Items in .Net Class with Scheduler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.