6

Scenario:

I have over 10,000 items in my sitecore/content tree. Nearly 2,000 of them are thrived from the Article template. Inside of the Article items, there are two important fields: Is Featured - Checkbox, and Featured Expiration Date - Date.

I like to create a scheduled task that finds only the items that thrive from **@@[templateId="{My template id}"], and Fields["Is Featured"].value == "1". After that, I would check for the value of Fields["Featured Expiration Date"]. If the value is <= DateTime.Now; then, I'm going to remove the Is Featured flag, and update the item.

Is this even possible as a scheduled task?

7

The scheduled task is going to be a tool used to execute code you've written. There are ways to create custom commands in C# or use the existing on that is included with Sitecore PowerShell Extensions (SPE).

For simplicity, I'll show you how to do that in SPE. Here's a really good section in the book covering the creating of task scripts.

Examples

In this question I showed a few ways to perform queries in SPE. In the following I'll use the Sitecore Query syntax.

Example: The following queries all items that have the IsFeatured field checked true.

Get-Item -Path "master:" -Query "/sitecore/content//*[@IsFeatured = '1']"

Example: The following queries all the items with a NewsDate greater than 2017-10-01.

Get-Item -Path "master:" -Query "/sitecore/content//*[@NewsDate > '20171001T000000']"

To combine the queries together you can do

[@IsFeatured = '1' and @NewsDate > '20161001T000000'].

Here's a nice screenshot of me running the command inside of the Console bundled with SPE.

Console Example

After the PowerShell script is written and saved. You can configured the scheduled task.

Wizard

Task Wizard

Schedule

PowerShellScriptCommand

| improve this answer | |
  • This is exactly what I need. Thank you so much. – Ali Khakpouri Dec 16 '17 at 14:17
4

If you don't want to install SPE module like @Michael West suggested, there is another way to do this. You can implement a custom class for your agent and add it to schedule with a config patch.

A config patch to add your class to the schedule can look like this:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <scheduling>
      <agent type="YourNamespace.YourAgentClass, YourAssemblyName" method="Run" name="AgentName" interval="01:00:00"/>
    </scheduling>
  </sitecore>
</configuration> 

In that config patch, you specify your agent's class name, a method to execute (Run) and interval.

Your custom class can look like this:

public class YourAgentClass
{
    public void Run()
    {

    }
}

Inside Run method you can use search index to find your items:

using (var context = index.CreateSearchContext())
{
    var results = context.GetQueryable<ArticleSearchResultItem>()
              .Where(item => item.TemplateId == ArticleTemplateId
                             && item.IsFeatured == true)
              .ToList();
}

ArticleSearchResultItem class can look like this:

public class ArticleSearchResultItem
{
    [IndexField("_group")]
    [TypeConverter(typeof(IndexFieldIDValueConverter))]
    public virtual ID ItemID { get; set; }

    [IndexField("_template")]
    [TypeConverter(typeof(IndexFieldIDValueConverter))]
    public virtual ID TemplateId { get; set; }

    [IndexField("isfeatured")]
    public virtual bool IsFeatured{ get; set; }
}

You can also extend existing SearchResultItem class.

| improve this answer | |

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.