How to get Sitecore Agent details

If you have scenario to get the details of Sitecore Agents, its last run time and interval to run, below code will help

	Sitecore.Tasks.Scheduler scheduler = new Sitecore.Tasks.Scheduler();
            var agentData  = scheduler.GetType().GetField("m_agents", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).GetValue(scheduler);

            Type schedulerType = typeof(Sitecore.Tasks.Scheduler);
            Type agentType = schedulerType.GetNestedType("Agent", BindingFlags.NonPublic);
            FieldInfo lastrunpro = agentType.GetField("m_lastRun", BindingFlags.NonPublic | BindingFlags.Instance);
            FieldInfo Name = agentType.GetField("m_name", BindingFlags.NonPublic | BindingFlags.Instance);
            FieldInfo interval = agentType.GetField("m_interval", BindingFlags.NonPublic | BindingFlags.Instance);

            if (agentData != null)
                var agentCollection = agentData as IEnumerable;

                foreach (object agent in agentCollection)
                    AgentsList.Add(new AgentsInfo() {
                        Name = Convert.ToString(Name.GetValue((agent))).Replace("Sitecore.Tasks.", ""),
                        LastRunTime = Convert.ToString(lastrunpro.GetValue((agent))),
                        Interval = Convert.ToString(interval.GetValue((agent)))



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s