Re: Error: 400 Bad Request when creating Exchange Task using WebDAV
Hi -
Here is the code that I'm using:
It's VB .NET
Dim strExchSvrName As String
Dim strMailbox As String
Dim strTaskUri As String
Dim strTaskItem As String
Dim strDomain As String
Dim strUserName As String
Dim strPassword As String
Dim strApptRequest As String
Dim strMailInfo As String
Dim strTaskInfo As String
Dim strXMLNSInfo As String
Dim strHeaderInfo As String
Dim PROPPATCHRequest As HttpWebRequest
Dim PROPPATCHResponse As WebResponse
Dim MyCredentialCache As CredentialCache
Dim bytes() As Byte
Dim PROPPATCHRequestStream As System.IO.Stream
Try
' Exchange server name
strExchSvrName = "SACINF02"
' Mailbox folder name.
strMailbox = Environment.UserName
'Take out the Domain name from the Environment User Name
strMailbox = Right(strMailbox, Len(strMailbox) -
InStr(strMailbox, "\"))
' Appointment item.
'strTaskItem = TaskName & "Task.eml"
strTaskItem = "testtask.eml"
' URI of the user's calendar folder.
strTaskUri = "http://" & strExchSvrName & "/exchange/" & _
strMailbox & "/tasks/"
' User name and password of appointment creator.
strUserName = strMailbox
strDomain = "SACRAMENTO"
strPassword = thepassword
' XML namespace info for the WebDAV request.
strXMLNSInfo = "xmlns:d=""DAV:""" & _
"xmlns:e=""http://schemas.microsoft.com/exchange/"" " &
_
"xmlns:b=""urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/"" " & _
"xmlns:f=""urn:schemas:mailheader:"" " & _
"xmlns:g=""urn:schemas:httpmail:""" & _
"xmlns:h=""http://schemas.microsoft.com/mapi/id/{00062003-0000-0000-C000-000000000046}/"""
' Set the Task item properties. The reminder time is set
in seconds.
' To create an all-day meeting, set the dtstart/dtend range
for 24 hours
' or more and set the alldayevent property to 1. See the
documentation
' on the properties in the urn:schemas:calendar: namespace
for more information.
'"<cal:dtstart
dt:dt=""dateTime.tz"">2004-05-18T23:00:00.000Z</cal:dtstart>" & _
Start = Start.Replace("\", "-")
Start = Start.Replace("/", "-")
'strTaskInfo = "<h:0x00008104
dt:dt=\""dateTime.tz\"">2005-09-29T00:00:00Z</h:0x00008104>" & _
' "<h:0x00008105
dt:dt=\""dateTime.tz\"">2005-09-29T00:00:00Z</h:0x00008105>" & _
' "<h:0x0000811C dt:dt=\""boolean\"">0</h:0x0000811C>" & _
' "<h:0x00008101 dt:dt=\""int\"">0</h:0x00008101>" & _
'"<h:0x00008102 dt:dt=\""float\"">0</h:0x00008102>" & _
'"<i:0x00008503 dt:dt=\""boolean\"">1</i:0x00008503>"
strTaskInfo = "<h:0x00008102
b:dt=""float"">.25</h:0x00008102>" & _
"<h:0x00008101
b:dt=""int"">1</h:0x00008101>" & _
"<h:0x00008517
b:dt=""dateTime.tz"">2005-10-10T13:30:00.00Z</h:0x00008517>"
' Set the required attendee of the appointment.
'strHeaderInfo = "<header:to> & strMailBox & </header:to>"
strHeaderInfo = ""
' Set the subject of the appointment.
strMailInfo = "<f:subject>Test Task Subject </f:subject>" &
_
"<g:textdescription>Test Task
Description</g:textdescription>"
' Build the XML body of the PROPPATCH request.
strApptRequest = "<?xml version=""1.0""?>" & _
"<d:propertyupdate " & strXMLNSInfo & ">" & _
"<d:set><d:prop>" & _
"<d:contentclass>urn:content-classes:task</d:contentclass>" & _
"<e:outlookmessageclass>IPM.Task</e:outlookmessageclass>" & _
strMailInfo & _
strTaskInfo & _
strHeaderInfo & _
"</d:prop></d:set>" & _
"</d:propertyupdate>"
'"<mapi:finvited dt:dt=""boolean"">1</mapi:finvited>" &
_
'"<mapi:apptstateflags
dt:dt=""int"">0</mapi:apptstateflags>" & _
' Create a new CredentialCache object and fill it with the
network
' credentials required to access the server.
MyCredentialCache = New System.Net.CredentialCache
MyCredentialCache.Add(New System.Uri(strTaskUri &
strTaskItem), "NTLM", New System.Net.NetworkCredential(strUserName,
strPassword, strDomain))
' Create the HttpWebRequest object.
PROPPATCHRequest = HttpWebRequest.Create(strTaskUri &
strTaskItem)
' Add the network credentials to the request.
PROPPATCHRequest.Credentials = MyCredentialCache
' Specify the PROPPATCH method.
PROPPATCHRequest.Method = "PROPPATCH"
PROPPATCHRequest.Headers.Add("Translate", "f")
PROPPATCHRequest.ContentType = "text/xml"
bytes = System.Text.Encoding.UTF8.GetBytes(strApptRequest)
PROPPATCHRequest.ContentLength = bytes.Length
PROPPATCHRequestStream =
PROPPATCHRequest.GetRequestStream()
PROPPATCHRequestStream.Write(bytes, 0, bytes.Length)
'PROPPATCHRequestStream.Close()
Try
' Create the Task in the user's mailbox
PROPPATCHResponse =
CType(PROPPATCHRequest.GetResponse(), System.Net.HttpWebResponse)
Catch ex As Exception
MsgBox(ex.ToString())
End Try
MsgBox("Task successfully created.")
PROPPATCHResponse.Close()
Catch ex As Exception
' Catch any exceptions. Any error codes from the
PROPPATCH
' or MOVE method requests on the server will be caught
' here, also.
MsgBox(ex.Message)
End Try
date: 11 Oct 2005 16:20:15 -0700
author: unknown
Re: Error: 400 Bad Request when creating Exchange Task using WebDAV
The two problems I see when I run this code is in your namespace declaration
you haven't put any spaces between 2 of your declarations which will cause
them to run together.
strXMLNSInfo = "xmlns:d=""DAV:""" & _
"xmlns:e=""http://schemas.microsoft.com/exchange/"" " &
You need to add a space eg strXMLNSInfo = "xmlns:d=""DAV:"" " & _
and
"xmlns:g=""urn:schemas:httpmail:""" & _
"xmlns:h=""http://schemas.microsoft.com/mapi/id/{00062003-0000-0000-C000-000000000046}/"""
you need a space as well after "xmlns:g=""urn:schemas:httpmail:"" " & _
With those two fixes the proppatch works okay for me. A good way to pick up
these type of problem is use something like Ethereal to do a packet capture
of the actual request your sending to the Exchange box. This will let you
see what the actually XML is that gets sent is.
Cheers
Glen
wrote in message
news:1129072815.409000.271090@o13g2000cwo.googlegroups.com...
> Hi -
>
> Here is the code that I'm using:
>
> It's VB .NET
>
>
>
>
>
> Dim strExchSvrName As String
> Dim strMailbox As String
> Dim strTaskUri As String
> Dim strTaskItem As String
> Dim strDomain As String
> Dim strUserName As String
> Dim strPassword As String
> Dim strApptRequest As String
> Dim strMailInfo As String
> Dim strTaskInfo As String
> Dim strXMLNSInfo As String
> Dim strHeaderInfo As String
> Dim PROPPATCHRequest As HttpWebRequest
> Dim PROPPATCHResponse As WebResponse
> Dim MyCredentialCache As CredentialCache
> Dim bytes() As Byte
> Dim PROPPATCHRequestStream As System.IO.Stream
>
> Try
> ' Exchange server name
> strExchSvrName = "SACINF02"
>
> ' Mailbox folder name.
> strMailbox = Environment.UserName
>
> 'Take out the Domain name from the Environment User Name
>
> strMailbox = Right(strMailbox, Len(strMailbox) -
> InStr(strMailbox, "\"))
> ' Appointment item.
> 'strTaskItem = TaskName & "Task.eml"
> strTaskItem = "testtask.eml"
> ' URI of the user's calendar folder.
> strTaskUri = "http://" & strExchSvrName & "/exchange/" & _
> strMailbox & "/tasks/"
>
> ' User name and password of appointment creator.
> strUserName = strMailbox
> strDomain = "SACRAMENTO"
>
> strPassword = thepassword
>
>
> ' XML namespace info for the WebDAV request.
> strXMLNSInfo = "xmlns:d=""DAV:""" & _
> "xmlns:e=""http://schemas.microsoft.com/exchange/"" " &
> _
>
> "xmlns:b=""urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/"" " & _
> "xmlns:f=""urn:schemas:mailheader:"" " & _
> "xmlns:g=""urn:schemas:httpmail:""" & _
>
> "xmlns:h=""http://schemas.microsoft.com/mapi/id/{00062003-0000-0000-C000-000000000046}/"""
>
> ' Set the Task item properties. The reminder time is set
> in seconds.
> ' To create an all-day meeting, set the dtstart/dtend range
> for 24 hours
> ' or more and set the alldayevent property to 1. See the
> documentation
> ' on the properties in the urn:schemas:calendar: namespace
> for more information.
>
> '"<cal:dtstart
> dt:dt=""dateTime.tz"">2004-05-18T23:00:00.000Z</cal:dtstart>" & _
> Start = Start.Replace("\", "-")
> Start = Start.Replace("/", "-")
>
> 'strTaskInfo = "<h:0x00008104
> dt:dt=\""dateTime.tz\"">2005-09-29T00:00:00Z</h:0x00008104>" & _
> ' "<h:0x00008105
> dt:dt=\""dateTime.tz\"">2005-09-29T00:00:00Z</h:0x00008105>" & _
> ' "<h:0x0000811C dt:dt=\""boolean\"">0</h:0x0000811C>" & _
> ' "<h:0x00008101 dt:dt=\""int\"">0</h:0x00008101>" & _
> '"<h:0x00008102 dt:dt=\""float\"">0</h:0x00008102>" & _
> '"<i:0x00008503 dt:dt=\""boolean\"">1</i:0x00008503>"
>
>
> strTaskInfo = "<h:0x00008102
> b:dt=""float"">.25</h:0x00008102>" & _
> "<h:0x00008101
> b:dt=""int"">1</h:0x00008101>" & _
> "<h:0x00008517
> b:dt=""dateTime.tz"">2005-10-10T13:30:00.00Z</h:0x00008517>"
> ' Set the required attendee of the appointment.
> 'strHeaderInfo = "<header:to> & strMailBox & </header:to>"
> strHeaderInfo = ""
> ' Set the subject of the appointment.
> strMailInfo = "<f:subject>Test Task Subject </f:subject>" &
> _
> "<g:textdescription>Test Task
> Description</g:textdescription>"
>
> ' Build the XML body of the PROPPATCH request.
> strApptRequest = "<?xml version=""1.0""?>" & _
> "<d:propertyupdate " & strXMLNSInfo & ">" & _
> "<d:set><d:prop>" & _
>
> "<d:contentclass>urn:content-classes:task</d:contentclass>" & _
>
> "<e:outlookmessageclass>IPM.Task</e:outlookmessageclass>" & _
> strMailInfo & _
> strTaskInfo & _
> strHeaderInfo & _
> "</d:prop></d:set>" & _
> "</d:propertyupdate>"
> '"<mapi:finvited dt:dt=""boolean"">1</mapi:finvited>" &
> _
> '"<mapi:apptstateflags
> dt:dt=""int"">0</mapi:apptstateflags>" & _
>
>
> ' Create a new CredentialCache object and fill it with the
> network
> ' credentials required to access the server.
> MyCredentialCache = New System.Net.CredentialCache
> MyCredentialCache.Add(New System.Uri(strTaskUri &
> strTaskItem), "NTLM", New System.Net.NetworkCredential(strUserName,
> strPassword, strDomain))
>
> ' Create the HttpWebRequest object.
> PROPPATCHRequest = HttpWebRequest.Create(strTaskUri &
> strTaskItem)
>
> ' Add the network credentials to the request.
> PROPPATCHRequest.Credentials = MyCredentialCache
>
> ' Specify the PROPPATCH method.
> PROPPATCHRequest.Method = "PROPPATCH"
> PROPPATCHRequest.Headers.Add("Translate", "f")
> PROPPATCHRequest.ContentType = "text/xml"
> bytes = System.Text.Encoding.UTF8.GetBytes(strApptRequest)
> PROPPATCHRequest.ContentLength = bytes.Length
> PROPPATCHRequestStream =
> PROPPATCHRequest.GetRequestStream()
> PROPPATCHRequestStream.Write(bytes, 0, bytes.Length)
> 'PROPPATCHRequestStream.Close()
>
>
> Try
> ' Create the Task in the user's mailbox
> PROPPATCHResponse =
> CType(PROPPATCHRequest.GetResponse(), System.Net.HttpWebResponse)
> Catch ex As Exception
> MsgBox(ex.ToString())
>
> End Try
>
>
>
> MsgBox("Task successfully created.")
> PROPPATCHResponse.Close()
>
> Catch ex As Exception
> ' Catch any exceptions. Any error codes from the
> PROPPATCH
> ' or MOVE method requests on the server will be caught
> ' here, also.
> MsgBox(ex.Message)
>
> End Try
>
date: Wed, 12 Oct 2005 10:37:26 +1000
author: Glen Scales [MVP]
Re: Error: 400 Bad Request when creating Exchange Task using WebDAV
code looks okay but your source URL looks very wrong
strSourceURI = "http://" & exchServername & "/Calendar/" &
strmailbox & "/" & TaskName & "Appt.eml"
Should be something like http://server/exchange/mailbox/folder/item.eml
Cheers
Glen
wrote in message
news:1129586598.230769.278170@z14g2000cwz.googlegroups.com...
> Thanks.
>
> Now - about the WebDAV DELETE method - I'm using the following code to
> try and delete an appointment and it is crashing out with the error
> message: The remote server returned an error: (501): Not Implemented.
> What does this mean - and how can i fix it?
>
>
>
> Dim Request As System.Net.HttpWebRequest
> Dim Response As System.Net.HttpWebResponse
> Dim MyCredentialCache As System.Net.CredentialCache
> Dim strPassword As String
> Dim strDomain As String
> Dim strUserName As String
> Dim strSourceURI As String
> Dim strmailbox As String
> Dim exchServername As String
>
> strmailbox = Environment.UserName
> strmailbox = Right(strmailbox, Len(strmailbox) -
> InStr(strmailbox, "\"))
>
> exchServername = "SACINF02"
>
> Try
> ' Initialize variables.
> strUserName = strmailbox
> strPassword = thepassword
> strDomain = "SACRAMENTO"
> strSourceURI = "http://" & exchServername & "/Calendar/" &
> strmailbox & "/" & TaskName & "Appt.eml"
>
> ' Create a new CredentialCache object and fill it with the
> network
> ' credentials required to access the server.
> MyCredentialCache = New System.Net.CredentialCache
> MyCredentialCache.Add(New System.Uri(strSourceURI), _
> "NTLM", _
> New System.Net.NetworkCredential(strUserName,
> strPassword, strDomain) _
> )
>
> ' Create the HttpWebRequest object.
> Request = CType(System.Net.WebRequest.Create(strSourceURI),
> _
> System.Net.HttpWebRequest)
>
> ' Add the network credentials to the request.
> Request.Credentials = MyCredentialCache
>
> ' Specify the method.
> Request.Method = "DELETE"
>
> ' Send the DELETE method request and get the
> ' response from the server.
> Response = CType(Request.GetResponse(),
> System.Net.HttpWebResponse)
>
> MsgBox("Appointment successfully deleted.")
>
> ' Clean up.
> Response.Close()
>
> Catch ex As Exception
>
> ' Catch any exceptions. Any error codes from the
> ' DELETE method requests on the server will be caught
> ' here, also.
> MsgBox(ex.Message)
>
> End Try
>
>
>
> Thanks.
>
date: Tue, 18 Oct 2005 12:17:46 +1000
author: Glen Scales [MVP]