Tuesday, April 10, 2012

How to delete Sharepoint list items permanently without being flushed to recycle bin


When we delete items using Processbatchdata, the deleted items are getting stored in the recycle bin. In order to avoid that we can set the recycle bin setting to “off” before deletion and to “on” after deletion as highlighted below.
Please note: Turning the recycle bin setting to OFF will delete the already stored item in recycle bin which is default behavior.



using (SPSite site = new SPSite("http://yoursiteURL"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string batchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                    "<ows:Batch OnError=\"Return\">{0}</ows:Batch>";

                    string processtext = @"<Method ID=""Del10"">
                                 <SetList Scope=""Request"">22BE7359-DD0D-4CD3-8920-584F3DED62E6</SetList>
                                 <SetVar Name=""Cmd"">Delete</SetVar>
                                 <SetVar Name=""ID"">10</SetVar>
                                 </Method>
                                ";

                    processtext = string.Format(batchFormat, processtext);
                    web.Site.WebApplication.RecycleBinEnabled = false;
                    string h = web.ProcessBatchData(processtext);
                    web.Site.WebApplication.RecycleBinEnabled = true;

                }

1 comment: