Monday 11 November 2013

SPListItem.Delete() Vs SPListItem.Recycle()

Hi All,
Good Day. Hope u all are doing good. Today we will see about 2 ways of deleting an item programmatically, which ofcourse are significant on their own ways.

While doing coding , 
SPListItem.Delete() is the most used method to delete an item. There is one more method - SPListItem.Recycle().

SPListItem.Recycle() :
When we use this method, the item gets moved to the Recycle Bin. So that , it can be restored if needed.

SPListItem.Delete() :
When we use this method, the item gets permanently deleted.

Under the hood :

Internally there isn’t much difference between the SPListItem.Delete and SPListItem.Recycle methods. Both call an internal SPListItem.Delete method with a different parameter which determines whether an item should be moved to the Recycle Bin or permanently deleted.


public override void Delete()   
{   
      if (this.HasExternalDataSource)   
      {   
            SPUtility.ValidateFormDigest();   
            string bdcid = (string) ((string) this.GetValue("BdcIdentity"));   
            this.ParentList.DataSource.DeleteItem(bdcid);   
      }   
      else   
      {   
            this.DeleteCore(DeleteOp.Delete);   
      }   
}   
public System.Guid Recycle()   
{   
      if (this.HasExternalDataSource)   
      {   
            SPExternalList.ThrowNotSupportedExceptionForMethod("Recycle", base.GetType());   
      }   
      return this.DeleteCore(DeleteOp.Recycle);   

}   

I hope you will be getting a good use of Recycle method in your forthcoming challenges.

Thanks for reading. 

2 comments:

  1. Please take a look at these lines and correct them , what parameters should be defined to distinguish method overloading for permanent delete and move to recycle bin ?

    SPListItem.Recycle() :
    When we use this method, the item gets moved to the Recycle Bin. So that , it can be restored if needed.

    SPListItem.Recycle() "
    When we use this method, the item gets permanently deleted.

    ReplyDelete