0
votes

I'm faced with a task where i'm supposed to unpublish items in sitecore when they are archived automatically.

I'm not able to find any events described in the sitecore documentation that is fired when an item is auto archived, that is when the archive date is reached.

I'm able to use both pipelines and events, and the sitecore version is 7.2.

Sorry for not giving an example on how i've tried to solve it, but i'm really lost in the dark here.

Update 19-02-2019. It turns out that Sitecore deletes the item after it is archived, so I ended up using the OnItemDeleted event to do my custom processing instead.

1
I did consider a workflow, but im not sure if it is executed when the item is archived automatically, do ypu know?MikNiller
Is is Sitecore OOTB Sitecore automatic archiving? Or is it something you created?Marek Musielak
Yes it is OOTB - It is the automatic archiving, when "Archive date" is reached, sitecore will automatically "remove" it from the master db tree and into the archive folder/application. Right now i'm instead considering a task that will run every e.g. 5 minutes and find freshly archived items and unpublish them ...MikNiller

1 Answers

1
votes

you can do this with a custom processor:

namespace Website.Pipelines
{
  public class UnpublishArchivedItem : DeleteItems
  {
    public void Process(ClientPipelineArgs args)
    {
        Assert.ArgumentNotNull(args, "args");
        Database database = Factory.GetDatabase(args.Parameters["database"]);
        Assert.IsNotNull(database, typeof(Database), "Name: {0}", args.Parameters["database"]);
        ListString listStrings = new ListString(args.Parameters["items"], '|');

        Database target = Factory.GetDatabase("web"); 

        foreach (string listString in listStrings)
        {
            Item item = database.GetItem(listString, Language.Parse(args.Parameters["language"]));
            if (item == null)
            {
                continue;
            }
            Log.Audit(this, "Unpublish item: {0}", new string[] { AuditFormatter.FormatItem(item) });

            item.Editing.BeginEdit();
            item.Publishing.NeverPublish = true;
            item.Editing.EndEdit();

            PublishManager.PublishItem(item.Parent, new []{ target }, item.Languages, true, false);
        }
    }
  }
}

This will set your archived item as unpublishable and will be removed from the "web" database.

Alternatively, you can remove directly the item from the web database using the item.Delete() method but personally, it's not optimal because you need to update your indexes.

Then, create a config file to define your processor and insert it before the item is archived, which is the Execute method.

<?xml version="1.0" encoding="utf-8"?>
  <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
      <processors>
        <uiArchiveItems>
          <processor type="Website.Pipelines.UnpublishArchivedItem,Website" patch:before="*[@method='Execute']"  />
        </uiArchiveItems>
      </processors>
    </sitecore>
</configuration>

Try this and let me know if it worked.