Lets imagine for a second that you have a lot of content Authors, and they are creating new items every day. At some point in the future, these items need to be moved to an Archive. Out of the box Sitecore has a functionality of Archiving Items, but it all goes into the Archive Application that you can access from Desktop > All Applications > Archive

I would like to achieve something different, I would like to move items from News folder to News Archive folder, illustrated below:

Auto Archive in Sitecore

In order to achieve this, you will need to create a News Template with certain fields, see the screenshot below:

Template

So then you will create a News item based on the News Template you just created.

You should see something similar to the screenshot below:

news-item

After you have all the items created in Sitecore Content Editor, now lets go to the Visual Studio and write some code:


using Sitecore.Configuration;
using Sitecore.Data.Items;
using Sitecore.Data;
using Sitecore.Data.Fields;
namespace sitecore.local.CustomCode
{
public class ItemMoving
{
private readonly Database _sourceDatabase = Factory.GetDatabase("master");
public void Execute()
{
var itemContainer = _sourceDatabase.GetItem("/sitecore/content/AutoArchive/News");
var destinationContainer = _sourceDatabase.GetItem("/sitecore/content/AutoArchive/News Archive");
if (itemContainer != null)
{
var items = itemContainer.Children;
foreach (Item item in items)
{
var expiryDate = (DateField)item.Fields["Expiration Date"];
if (expiryDate.DateTime < System.DateTime.Now.ToUniversalTime()) {
item.MoveTo(destinationContainer);
}
}
}
}
}
}

view raw

itemMoving.cs

hosted with ❤ by GitHub

After you build and deploy the code, you will need to add some custom Configurations. So go ahead into the folder Website>App_Config>Include>Project>ItemMoving.config and insert the code below, this will patch the actual Sitecore.config


<?xml version="1.0" encoding="utf-8"?>
<configuration>
<sitecore>
<scheduling>
<!–Custom Code that will do item moving–>
<agent type="sitecore.local.CustomCode.ItemMoving" method="Execute" interval="00:00:59"/>
</scheduling>
</sitecore>
</configuration>

So this agent will execute every minute, and search for the Expiration Date within the items, that are in the News (Source Folder) and move the expired files to the News Archive (Destination folder). If you have any questions do not hesitate to ask.

Acknowledgement:
Richard Seal – https://sitecore.stackexchange.com/users/70/richard-seal
Dannon Gruver – https://www.linkedin.com/in/dannongruver
This blog post is based on the post from Stack Exchange:
https://sitecore.stackexchange.com/questions/5974/move-items-between-folders-programmatically
Note that this blog post has no intention to claim anybodies work as my own. This blog post is for informative purposes only.

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s