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):
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.