Thursday, June 24, 2004

Using Bcopy with WebDav in a script

There are a bunch of Batch commands in WebDav that allow you to perform multiple actions with one request this is explained here. I was looking at this today for another script I was writing and although the response and requests are documented in the Exchange SDK unfortunately no code examples exist. A wider search of the web didn't really show anything up either. Although its not extremely hard to use it did take me a little while to work out the correct way to use it so I thought I'd post it as I found it helpful. The script copies multiple items in one mailbox folder to another folder in the same mailbox with one XML request.


sDestinationURL = "http://yourserver/exchange/yourmailbox/targetfolder/"
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:"">"
xmlstr = xmlstr & "<D:target>"
xmlstr = xmlstr & "<D:href>http://yourserver/exchange/yourmailbox/inbox/test-3.EML</D:href>"
xmlstr = xmlstr & "<D:dest>http://yourserver/exchange/yourmailbox/targetfolder/test-3.EML</D:dest>"
xmlstr = xmlstr & "</D:target>"
xmlstr = xmlstr & "<D:target>"
xmlstr = xmlstr & "<D:href>http://yourserver/exchange/yourmailbox/inbox/test-4.EML</D:href>"
xmlstr = xmlstr & "<D:dest>http://yourserver/exchange/yourmailbox/targetfolder/test-4.EML</D:dest>"
xmlstr = xmlstr & "</D:target>"
xmlstr = xmlstr & "<D:target>"
xmlstr = xmlstr & "<D:href>http://yourserver/exchange/yourmailbox/inbox/test-5.EML</D:href>"
xmlstr = xmlstr & "<D:dest>http://yourserver/exchange/yourmailbox/targetfolder/test-5.EML</D:dest>"
xmlstr = xmlstr & "</D:target>"
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!."
Else
Wscript.echo "Request Failed. Results = " & XMLreq.Status & ": " & XMLreq.statusText
End If