Skip to main content

Recover Deleted Items from the Dumpster Programmatically

I started looking at this because I thought it would be a good way of finding any spam that was getting past my IMF filter that users weren't reporting. So I went of and hit the ESDK and CDO doco for any clues also did a Google crawl and didn't really turn up much except that CDO was not going to do the trick. The next step was to look at OWA and see if I could reuse any of the stuff it uses. After examining some of the conversations my browser was having with OWA I found that OWA was issuing a query with a scope of softdeleted eg "from scope ('SOFTDELETED traversal of ""') . I Googled this which turned up this Blog post from KC which started to fill in a few of the blanks.

The next thing I tried to do was use a Softdeleted traversal in a Exoledb script which although it seemed to be a valid query it didn't work or more to the point the query worked but instead of returning a record set of soft deleted items I just got all normal folder items. I tried the same thing again instead this time using MSDAIPP.dso (which uses WebDAV) and this worked so it seemed that the Softdeleted traversal only works in WebDAV and not Exoledb.

By this time I was quite interested and started thinking that I could go a bit further then just reporting on missed SPAM so I started looking at ways I could maybe copy this data out into a central repository. I started hitting a few problems because although I could access these soft deleted emails as a recordset, I couldn't open a record against any one item because as far as the mail API's where concerned the resource at that URL had been deleted. This lead me back to the OWA conversations I was looking at and I noticed when OWA restored an item from the dumpster it used the /-softdeleted-/-FlatUrlSpace-/ namespace to copy the item back into the store. I tried to access the /-softdeleted-/-FlatUrlSpace-/ directly with a ADO record but this still didn't work so I resigned to just duplicating OWA functionality which was to do a WebDav Bcopy of the items in the dumpster and then BDelete the items from the dumpster using the /-softdeleted-/-FlatUrlSpace-/ to reference the source items. The last problem was to work out what the /-softdeleted-/-FlatUrlSpace-/ property of the email was as this didn't seem to be located in the recordset either, what I found was that the /-softdeleted-/-FlatUrlSpace-/ property was the same as the /-FlatUrlSpace-/ which is available as the http://schemas.microsoft.com/exchange/permanenturl property all I needed was to append (foldername in my case deleteded items)/-softdeleted-/ before /-FlatUrlSpace-/

So what I ended up with is a script that will copy all the items from the Dumpster using a Bcopy into a folder in the users inbox and then deletes all the items from the dumpster. Its important to do the deletion because if you don't you'll end up with duplicates when the user deletes the emails again.


sDestinationURL = "http://yourserver/exchange/yourmailbox/test/"
Set XMLreq = CreateObject("Microsoft.xmlhttp")
XMLreq.open "BCOPY", "http://yourserver/exchange/yourmailbox/inbox/", False
XMLreq.setRequestHeader "Destination", sDestinationURL
xmlstr = "<?xml version=""1.0"" ?>"
xmlstr = xmlstr & "<D:copy xmlns:D=""DAV:"">"
Set Rec = CreateObject("ADODB.Record")
Set Rs = CreateObject("ADODB.Recordset")
rec.open "http://yourserver/exchange/yourmailbox", ,3
inbstr = "http://yourserver/exchange/yourmailbox/deleted items/"
strView = "select * from scope ('SOFTDELETED traversal of """ & inbstr & """') "
Rs.Open strView, rec.activeconnection, 3
If Rs.RecordCount <> 0 Then
while not rs.eof
xmlstr = xmlstr & "<D:target>"
xmlstr = xmlstr & "<D:href>" & replace(rs.fields("http://schemas.microsoft.com/exchange/permanenturl")" _
& ","/-FlatUrlSpace-/","/Deleted Items/-softdeleted-/-FlatUrlSpace-/") & "</D:href>"
xmlstr1 = xmlstr1 & "<D:href>" & replace(rs.fields("http://schemas.microsoft.com/exchange/permanenturl")" _
& ","/-FlatUrlSpace-/","/Deleted Items/-softdeleted-/-FlatUrlSpace-/") & "</D:href>"
xmlstr = xmlstr & "<D:dest>" & replace(rs.fields("Dav:href"),"Deleted%20Items","targetfolder") & "</D:dest>"
xmlstr = xmlstr & "</D:target>"
rs.movenext
wend
End If
xmlstr = xmlstr & "</D:copy>"
XMLreq.setRequestHeader "Content-Type", "text/xml;"
XMLreq.setRequestHeader "Translate", "f"
XMLreq.setRequestHeader "Content-Length:", Len(xmlstr)
XMLreq.send(xmlstr)
If (XMLreq.Status >= 200 And XMLreq.Status < 300) Then
Wscript.echo "Success! " & "Results = " & XMLreq.Status & ": " & XMLreq.statusText
ElseIf XMLreq.Status = 401 then
Wscript.echo "You don't have permission to do the job! Please check your permissions on this item."
Else
Wscript.echo "Request Failed. Results = " & XMLreq.Status & ": " & XMLreq.statusText
End If
XMLreq.open "BDELETE", "http://yourserver/exchange/yourmailbox/Deleted Items/", False
xmlstr = "<?xml version=""1.0"" ?>"
xmlstr = xmlstr & "<D:delete xmlns:D=""DAV:"">"
xmlstr = xmlstr & "<D:target>"
xmlstr = xmlstr & xmlstr1
xmlstr = xmlstr & "</D:target>"
xmlstr = xmlstr & "</D:delete>"
XMLreq.setRequestHeader "Content-Type", "text/xml;"
XMLreq.setRequestHeader "Translate", "f"
XMLreq.setRequestHeader "Content-Length:", Len(xmlstr)
XMLreq.send(xmlstr)
If (XMLreq.Status >= 200 And XMLreq.Status < 300) Then
Wscript.echo "Success! " & "Results = " & XMLreq.Status & ": " & XMLreq.statusText
ElseIf XMLreq.Status = 401 then
Wscript.echo "You don't have permission to do the job! Please check your permissions on this item."
Else
Wscript.echo "Request Failed. Results = " & XMLreq.Status & ": " & XMLreq.statusText
End If

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Gr...

Sending a MimeMessage via the Microsoft Graph using the Graph SDK, MimeKit and MSAL

One of the new features added to the Microsoft Graph recently was the ability to create and send Mime Messages (you have been able to get Message as Mime for a while). This is useful in a number of different scenarios especially when trying to create a Message with inline Images which has historically been hard to do with both the Graph and EWS (if you don't use MIME). It also opens up using SMIME for encryption and a more easy migration path for sending using SMTP in some apps. MimeKit is a great open source library for parsing and creating MIME messages so it offers a really easy solution for tackling this issue. The current documentation on Send message via MIME lacks any real sample so I've put together a quick console app that use MSAL, MIME kit and the Graph SDK to send a Message via MIME. As the current Graph SDK also doesn't support sending via MIME either there is a workaround for this in the future my guess is this will be supported.

Export calendar Items to a CSV file using Microsoft Graph and Powershell

For the last couple of years the most constantly popular post by number of views on this blog has been  Export calendar Items to a CSV file using EWS and Powershell closely followed by the contact exports scripts. It goes to show this is just a perennial issue that exists around Mail servers, I think the first VBS script I wrote to do this type of thing was late 90's against Exchange 5.5 using cdo 1.2. Now it's 2020 and if your running Office365 you should really be using the Microsoft Graph API to do this. So what I've done is create a PowerShell Module (and I made it a one file script for those that are more comfortable with that format) that's a port of the EWS script above that is so popular. This script uses the ADAL library for Modern Authentication (which if you grab the library from the PowerShell gallery will come down with the module). Most EWS properties map one to one with the Graph and the Graph actually provides better information on recurrences then...
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.