Friday, June 25, 2004

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