Sandun Perera
Sandun Perera

Reputation: 571

item.Delete() gives Object reference not set to an instance of an object error in Sitecore

In my Sitecore application I have the below statements.

using (new Sitecore.SecurityModel.SecurityDisabler())
    {
       Item item = database.GetItem(itemId);
       if (item != null)
       {
          item.Delete();
       }
    }

The item object is not null and in item.Delete(); statement the error occurs. Can anyone tell whats wrong with this issue?

Updated: Stack trace

    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.EngineCommand`2.Executed()
    at Sitecore.Data.Engines.EngineCommand`2.Execute()
    at Sitecore.Data.Engines.DataEngine.DeleteItem(Item item)
    at Sitecore.Data.Managers.ItemProvider.DeleteItem(Item item, SecurityCheck securityCheck)
    at Sitecore.Data.Managers.ItemManager.DeleteItem(Item item)
    at Sitecore.Data.Items.Item.Delete(Boolean removeBlobs)
    at Sitecore.Data.Items.Item.Delete()

I have further found that the item is actually got deleted from the Sitecore tree, and then gave the error.

Upvotes: 2

Views: 1445

Answers (3)

Carlos G.
Carlos G.

Reputation: 376

item.Delete() gives Object reference not set to an instance of an object error in Sitecore

Normally this can happen because of security. Maybe the user impersonated does not have read access to the item or the item requires permission in order to be deleted.

Look into using the SecurityDisabler and the UserSwitcher.

in the cookbook you can see a reference to this type of issue: http://sdn.sitecore.net/upload/sitecore6/content_api_cookbook-a4.pdf

there are a couple of snippets of the disabler throughout the PDF. for example on page 12.

Also page 47 talks about the error.

Still I found some cases using the SecurityDisabler does not work. I have an example where the security explicitly prohibit a delete by a normal user. Only the Admin is capable to do such. In this case I'm still getting the error that you are getting even though I'm using the securityDisabler:

/////SNIPPET

 ...

  string userName = @"sitecore\[A USER]";
                    if (Sitecore.Security.Accounts.User.Exists(userName))
                    {
  Sitecore.Security.Accounts.User user = Sitecore.Security.Accounts.User.FromName(userName, false);
                        //using (new Sitecore.Security.Accounts.UserSwitcher(user))
                        using (new Sitecore.SecurityModel.SecurityDisabler())
                        {
                            using (new Sitecore.Security.Accounts.UserSwitcher(user))
                            {
                               //DO SOMETHING
                             }
                         }
                     }



 ////END OF SNIPPET

Good luck with your code.

Regards,

Upvotes: 1

Vicent Galiana
Vicent Galiana

Reputation: 328

The method which is triggering the exception Sitecore.Tasks.ItemEventHandler.OnItemDeleted tries to Remove "Itemstaks" for that item, from the "taskdatabase" So the only reason i can guess for this error is your configuration has something wrong. Do you have this section in your config file or include file?:

<TaskDatabase type="Sitecore.Data.$(database).$(database)TaskDatabase, Sitecore.Kernel">
  <param connectionStringName="core" />
</TaskDatabase>

Is the related connection string available?

Upvotes: 0

user459491
user459491

Reputation:

I remember that I met this problem few time ago and I resolved it with BeginEdit() and EndEdit(). It's a little bit strange but it works. Can you change your code to:

using (new Sitecore.SecurityModel.SecurityDisabler())
{
   Item item = database.GetItem(itemId);
   if (item != null)
   {
      item.Editing.BeginEdit();
      item.Delete();
      item.Editing.EndEdit();
   }
}

Upvotes: 0

Related Questions