Explore the Experience in SharePoint 2013

Monday, October 13, 2014

Importance and Difference between SPListItem.Delete() method and SPListItem.Recycle() method.

No comments
If you delete a document in SharePoint using the UI, it’s being moved to the Recycle Bin, so that it can be restored if necessary. There are situations when you want to include deleting list items and documents in your custom solutions. The most commonly used approach I’ve seen is calling the SPListItem.Delete() method. While this does the job, and deletes the item, it deletes it permanently instead of moving to the Recycle Bin.

Looking carefully at the SPListItem class, you can find SPListItem.Recycle() method. It turns out that it’s exactly that method that you need to call in order to move a list item/document to the Recycle Bin instead of deleting it permanently.

In general moving items to the Recycle Bin instead deleting them permanently is what you should do in our custom solutions. It is standard SharePoint behavior and therefore something end users will expect of our solutions as well. You should perform the permanent deletion only if explicitly named in the requirements. Otherwise, let’s just stick to what SharePoint does to leverage the same User Experience.

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.

  1. public override void Delete()   
  2. {   
  3.       if (this.HasExternalDataSource)   
  4.       {   
  5.             SPUtility.ValidateFormDigest();   
  6.             string bdcid = (string) ((string) this.GetValue("BdcIdentity"));   
  7.             this.ParentList.DataSource.DeleteItem(bdcid);   
  8.       }   
  9.       else   
  10.       {   
  11.             this.DeleteCore(DeleteOp.Delete);   
  12.       }   
  13. }   
  1. public System.Guid Recycle()   
  2. {   
  3.       if (this.HasExternalDataSource)   
  4.       {   
  5.             SPExternalList.ThrowNotSupportedExceptionForMethod("Recycle", base.GetType());   
  6.       }   
  7.       return this.DeleteCore(DeleteOp.Recycle);   
  8. }   
  9.   

No comments :

Post a Comment