Delete SharePoint List Items with Recycle Bin Support

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.

Lets have a close look at the methods which SharePoint internally calls to get this done.   

  1. [ClientCallableMethod(Name="DeleteObject", RemoveThisObjectFromParentCollection=true), ClientCallableExceptionConstraint(FixedId="4", Condition="Operation is not supported.Cannot find the Entity Instance to delete.", ErrorType=typeof(System.InvalidOperationException))]   
  2.   
  3. public override void Delete()   
  4. {   
  5.       if (this.HasExternalDataSource)   
  6.       {   
  7.             SPUtility.ValidateFormDigest();   
  8.             string bdcid = (string) ((string) this.GetValue("BdcIdentity"));   
  9.             this.ParentList.DataSource.DeleteItem(bdcid);   
  10.       }   
  11.       else   
  12.       {   
  13.             this.DeleteCore(DeleteOp.Delete);   
  14.       }   
  15. }   
  16.   
  17.   
  18. [ClientCallableExceptionConstraint(FixedId="2", ErrorCode=-2130575338, Condition="Item does not exist.The page you selected contains an item that does not exist. It may have been deleted by another user.", ErrorType=typeof(SPException)), ClientCallableMethod(RemoveThisObjectFromParentCollection=true), ClientCallableExceptionConstraint(FixedId="3", Condition="Method 'Recycle' is not supported on Microsoft.SharePoint.SPListItem for external lists.", ErrorType=typeof(System.NotSupportedException))]   
  19. public System.Guid Recycle()   
  20. {   
  21.       if (this.HasExternalDataSource)   
  22.       {   
  23.             SPExternalList.ThrowNotSupportedExceptionForMethod("Recycle", base.GetType());   
  24.       }   
  25.       return this.DeleteCore(DeleteOp.Recycle);   
  26. }   
  27.   
  28. private System.Guid DeleteCore(DeleteOp deleteOp)   
  29. {   
  30.       System.Guid guid;   
  31.       if (this.m_Items.QueryInternal.ExpandRecurrence)   
  32.       {   
  33.             throw new SPException(SPResource.GetString("CannotUpdateRecurrentItems"new object[0]));   
  34.       }   

  35.       if (this.ParentList.BaseType == SPBaseType.DocumentLibrary)   
  36.       {   
  37.             this.m_Items.SetFileRefForDeleteDocLibItem(this);   
  38.       } 
  39.   
  40.       else if ((this.Web.UIVersion < 15) && (this.ParentList.BaseTemplate == SPListTemplateType.DiscussionBoard))       {   
  41.             DiscussionListHelpers.HandleDiscussionItemDeleted(this.Web, this.ParentList, this);   
  42.       }   

  43.       SPSecurity.SetListInHttpContext(HttpContext.Current, this.ParentList.InternalName);   
  44.       bool isDirty = this.m_Items.IsDirty;   
  45.       this.m_Items.SetIsDirty(false);   
  46.       if ((this.ContentType != null) && (this.ContentType.Name != null))   
  47.       {   
  48.             this.Web.Request.SetVar(this.Web.Url, "EventContentTypeName"this.ContentType.Name);   
  49.       }   
  50.       if (isDirty)   
  51.       {   
  52.             this.m_Items.SetIsDirty(isDirty);   
  53.       }   
  54.   
  55.       this.Web.Request.DeleteItem(this.Web.Url, this.ParentList.InternalName, this.ID, (uint) deleteOp, this.UnrestrictedUpdate, out guid);   
  56.       this.m_Items.SetListItemsStateAsDirty();   
  57.       this.m_bDirtyItem = true;   
  58.       this.m_ItemsFallback = null;   
  59.       return guid;   
  60. }  
The Recycle Bin is temporary storage for all items deleted from that site. You can access the Recycle Bin from the left menu bar in your site.

Happy SharePoint !!!!
Ebook Download
View all
Learn
View all