0

I have a function like below which give me the list of items which are closer to publishing date but not yet approved in workflow. I have to mail this list to few people.

  private void GetItemsTobePublishedbutNotApproved()
  {
        IWorkflow[] workflows = Sitecore.Context.Database.WorkflowProvider.GetWorkflows();

        IWorkflow chosenWorkflow = workflows.Where(x => x.WorkflowID == "{A5BC37E7-ED96-4C1E-8590-A26E64DB55EA}").FirstOrDefault(); // choose your worfklow

        WorkflowState[] workflowStates = chosenWorkflow.GetStates();
        string draftStateId = Convert.ToString(workflowStates.Where(c => c.DisplayName == "Draft").FirstOrDefault());

        foreach (WorkflowState state in workflowStates)
        {
            if (!state.FinalState && state.DisplayName != "Draft")
            {
                DataUri[] itemDataUris = chosenWorkflow.GetItems(state.StateID);
                foreach (DataUri uri in itemDataUris)
                {
                    Item item = Sitecore.Context.Database.GetItem(uri);
                    DateField dateField = (DateField)item.Fields["Publish Date"];
                    var dateTime = dateField.DateTime;
                    DateTime currentdateTime = DateTime.Now;
                    if ((currentdateTime - dateTime).TotalDays < 2)
                    {
                        string name = item.Name;
                        string currentState = state.DisplayName;
                        Sitecore.Workflows.WorkflowEvent[] workflowHistory = Sitecore.Context.Database.WorkflowProvider.GetWorkflow(item).GetHistory(item);
                        //Get the Author Information 
                        string user = workflowHistory.Where(x => x.OldState == "DraftStateId").FirstOrDefault().User;
                        var submittingUser = Sitecore.Security.Accounts.User.FromName(user, false);
                        //Email all th details to a person 
                    }

                }
            }
        }
    }

I have to create a job with the above function which runs once per day and trigger email. What should I do to convert above function as scheduled job?

Thanks

1

You have couple of options to make this happen. Two options are below:

1) SiteCron

If you need to run job exactly at specified time, you should introduce Sitecron module into your solution. You can find it in Akshay's friendly github or directly in Sitecore Marketplace. This option is good when there is no problem introducing new module into solution.

You need to create class inheriting from IJob:

using Quartz;
using Sitecore.Data;
using Sitecore.Data.Managers;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleJob.Publishing
{
    public class SmartSitePublishJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Log.Info("GetItemsTobePublishedButNotApprovedJob Execute - Start", this);

            Database master = Sitecore.Configuration.Factory.GetDatabase("master");

            IWorkflow[] workflows = master.WorkflowProvider.GetWorkflows();

            IWorkflow chosenWorkflow = workflows.Where(x => x.WorkflowID == "{A5BC37E7-ED96-4C1E-8590-A26E64DB55EA}").FirstOrDefault(); // choose your worfklow

            WorkflowState[] workflowStates = chosenWorkflow.GetStates();
            string draftStateId = Convert.ToString(workflowStates.Where(c => c.DisplayName == "Draft").FirstOrDefault());

            foreach (WorkflowState state in workflowStates)
            {
                if (!state.FinalState && state.DisplayName != "Draft")
                {
                    DataUri[] itemDataUris = chosenWorkflow.GetItems(state.StateID);
                    foreach (DataUri uri in itemDataUris)
                    {
                        Item item = Sitecore.Context.Database.GetItem(uri);
                        DateField dateField = (DateField)item.Fields["Publish Date"];
                        var dateTime = dateField.DateTime;
                        DateTime currentdateTime = DateTime.Now;

                        if ((currentdateTime - dateTime).TotalDays < 2)
                        {
                            string name = item.Name;
                            string currentState = state.DisplayName;
                            Sitecore.Workflows.WorkflowEvent[] workflowHistory = Sitecore.Context.Database.WorkflowProvider.GetWorkflow(item).GetHistory(item);
                            //Get the Author Information 
                            string user = workflowHistory.Where(x => x.OldState == "DraftStateId").FirstOrDefault().User;
                            var submittingUser = Sitecore.Security.Accounts.User.FromName(user, false);
                            //Email all the details to a person 
                         }    
                     }
                 }
             }

             Log.Info("GetItemsTobePublishedButNotApprovedJob Execute - End", this);
        }
    }
} 

For further reference and sample solution take a look on github. Take a look also on video how to wire things up -> https://www.akshaysura.com/video/sitecron-demo/ .

2) Sitecore OOTB Schedule Jobs

You can also leverage Sitecore OOTB Scheduler which is fine but it cannot guarantee when job will run precisely. It is based on agent and settings. It will run circa at same time within couple of minutes.

In this case, you need to create custom class in your solution with this signature:

public void Execute(Item[] items, Sitecore.Tasks.CommandItem command, Sitecore.Tasks.ScheduleItem schedule)
{
    //Here goes same code as in previous example for SiteCron
}

You then need to create new command in Sitecore under /sitecore/system/Tasks/Commands/ based on /sitecore/templates/System/Tasks/Command Template where you specify your class as Type and Method name in Method field:

enter image description here

You need to create new schedule job under /sitecore/system/Tasks/Schedules/ based on /sitecore/templates/System/Tasks/Schedule Template where you specify your Command that you created in previous step and Schedule:

enter image description here

More details on how to set schedule based on your preference + other stuff can be found in this nice article -> https://www.degdigital.com/insights/how-to-create-sitecore-scheduled-task/

  • Thanks for the reply peter .I have upvoted this .will try it out mark it as answer soon. – user3621 May 17 '18 at 13:27
  • @user3621 Please upvote and mark as answer if it helped :) – Peter Procházka Jun 18 '18 at 6:04

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.