1
votes

I am using a Windows Service which does cleanup jobs on Sitecore Items (inactive or old products) and when I am trying to delete them I cannot.

if (qItem != null)
{
 qItem.Recycle();
}

Relevat StackTrace is:

Message: Object reference not set to an instance of an object.
StackTrace:   at Sitecore.Tasks.ItemEventHandler.OnItemDeleted(Object sender, EventArgs args)
   at Sitecore.Events.Event.EventSubscribers.RaiseEvent(String eventName, Object[] parameters, EventResult result)
   at Sitecore.Events.Event.EventSubscribers.RaiseEvent(String eventName, Object[] parameters)
   at Sitecore.Events.Event.RaiseEvent(String eventName, Object[] parameters)
   at Sitecore.Events.Event.RaiseItemDeleted(Object sender, ItemDeletedEventArgs args)
   at Sitecore.Events.Event.DataEngine_ItemDeleted(Object sender, ExecutedEventArgs`1 e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at Sitecore.Data.Engines.EngineCommand`2.RaiseExecuted()
   at Sitecore.Data.Engines.DataEngine.RaiseDeletedItem(Item item, ID parentId, Boolean result)
   at Sitecore.Data.Archiving.Archive.NotifyItemDeleted(Item item, ID parentId)
   at Sitecore.Data.Archiving.SqlArchive.DoArchiveItems(IEnumerable`1 items, Guid archivalId)
   at Sitecore.Data.Archiving.SqlArchive.DoArchiveItems(IEnumerable`1 items, Guid archivalId)
   at Sitecore.Data.Archiving.SqlArchive.ArchiveItem(Item item)
   at Sitecore.Data.Items.Item.Recycle()

I mention again that this error I get only if I recycle an item from a Windows Service. Same recycle works ok from Web app.

In Web app I am logged in into sitecore and user is admin, so he has some rights.

In Windows Service the user (Sitecore.Context.User) is "default\Anonymous".

To bypass security checks I surrounded the Recycle call inside a SecurityDisabler using but without results.

Do I need to login into Sitecore from my Windows Service? If yes, how can I do that?

Or is there another way to recycle/delete Sitecore Item from a non-web application?

Here is the code that runs and throws a Access Denied Exception (it does not run inside a SecurityDisabler context):

Recycle item directly, not inside a security disabler. Here is the code that recycles item inside a SecurityDisabler context. No Access Denied exception now, but a "Object reference not set to an instance" exception.

Recycle item inside a SecurityDisabler context

3
You could try the web service. You can find it under /sitecore/shell/WebServiceLloyd

3 Answers

4
votes

Looking in Reflector at the method (Sitecore.Tasks.ItemEventHandler.OnItemDeleted) which is throwing the exception, it would appear that your null reference is likely the TaskDatabase.

protected void OnItemDeleted(object sender, EventArgs args)
{
    Item item = Event.ExtractParameter(args, 0) as Item;
    Error.AssertNotNull(item, "No item in parameters");
    using (new SecurityDisabler())
    {
        Globals.TaskDatabase.RemoveItemTasks(item);
    }
}

I assume for your windows service, you have an App.config with a stripped down Sitecore configuration. You should ensure that the /sitecore/configuration/TaskDatabase element is in your Sitecore config.

1
votes

When we install a Sitecore instance, we are provided with a web service which is exposed under the same domain as the Sitecore application

http://sitedomain.com/sitecore/shell/webservice/service.asmx

This service has all the methods you need to work with Sitecore API.

It worked for me

0
votes

I had a very similar issue with a standalone service using Sitecore APIs.

I was using the BucketManager to Sync an item bucket, which ended up invoking the same item.Delete() method that Recycle calls, so my stack trace led me to the same spot.

The TaskDatabase was defined in the web.config I was using, but I decided the Load() method on Globals was never getting called, so I was never initializing the Task Database field with anything from Config.

In the constructor of the object I invoked the delete from, I included the line:

Sitecore.Globals.Load();

This solved my issue.