Thursday, September 15, 2005

Creating a new folder in a mailbox if one doesn't exist in WebDav

I posted a script that did this using ADO/Exoledb some time ago here someone asked this week about doing the same thing using WebDAV. In WebDAV to create a folder you use the MKCOL verb eg but if you try and issue this against a URL that already exist then it will throw a 405 error. While dealing with such an error is not that difficult an alternative to doing this is to first do a search against the parent folder to see if that folder exists and then take the appropriate action. A script to do this looks like the following I've posted a downloadable copy here. The script itself looks for a folder called Spamfolder within the inbox if it doesn't find it then it creates it using MKCOL.


server = "Servername"
mailbox = "Mailbox"
NewFLd = "Spamfolder"
strURL = "http://" & server & "/exchange/" & mailbox & "/inbox/"
strQuery = ""
strQuery = strQuery & "SELECT ""http://schemas.microsoft.com/mapi/proptag/x3001001E"""
strQuery = strQuery & " FROM scope('shallow traversal of """
strQuery = strQuery & strURL & """') Where ""DAV:ishidden"" = False AND ""DAV:isfolder"" = True AND "
strQuery = strQuery & """http://schemas.microsoft.com/mapi/proptag/x3001001E"" = '" & NewFLd & "'
"
set req = createobject("microsoft.xmlhttp")
req.open "SEARCH", strURL, false
req.setrequestheader "Content-Type", "text/xml"
req.setRequestHeader "Translate","f"
req.send strQuery
If req.status >= 500 Then
ElseIf req.status = 207 Then
set oResponseDoc = req.responseXML
set oNodeList = oResponseDoc.getElementsByTagName("d:x3001001E")
wscript.echo oNodeList.length
if oNodeList.length <> 0 then
wscript.echo "Folder Already Exists"
else
call Createfolder(NewFld,strURL)
end if
Else
End If

Sub Createfolder(fldname,parentfolder)

nfolderURL = parentfolder & "/" & fldname & "/"
req.open "MKCOL", nfolderURL, false
req.setrequestheader "Content-Type", "text/xml"
req.setRequestHeader "Translate","f"
req.send
if req.status = 201 then
Wscript.echo "Folder created sucessfully"
else
wscript.echo req.status
wscript.echo req.statustext
end if

end sub

3 comments:

sk73 said...

I retrieve a error status 440 when i try to make an appointment / create a folder using webdav (microsoft.xmlhttp) I've tried to search Google, MSDN and a lot more bur there is wery little information about this subject.

Glen said...

Sounds like you are getting a logon timeout. Are you running Forms based authentication on your server. If you are you need to put some extra code in to deal with the FBA logon. I've got some FBA code in the sample from my post http://gsexdev.blogspot.com/2005/08/sending-read-and-delivery-recipients.html . Also http://www.infinitec.de/exchange/howtos/webdavwithfba.aspx has some good samples. If thats not it post of email me your code and i'll have a look to see if there is anything obvious wrong

Eric said...

This works great, although I saw in another blog that you said it was possible to put in a Homepage URL. Any chance you could post the code to do this?

Much Appreciated,