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 """""
strQuery = strQuery & " FROM scope('shallow traversal of """
strQuery = strQuery & strURL & """') Where ""DAV:ishidden"" = False AND ""DAV:isfolder"" = True AND "
strQuery = strQuery & """"" = '" & NewFLd & "'
set req = createobject("microsoft.xmlhttp") "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"
call Createfolder(NewFld,strURL)
end if
End If

Sub Createfolder(fldname,parentfolder)

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

end sub


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 . Also 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,