Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error when creating Task Item in Exchange Mailbox

Status
Not open for further replies.

saadabc

Programmer
Aug 5, 2004
107
US
Hi - I'm using WebDAV to try and create a Task Item in an Exchange Mailbox -

when I run this code:

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 System.Net.HttpWebRequest
Dim PROPPATCHResponse As System.Net.WebResponse
Dim MyCredentialCache As System.Net.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.
' strApptItem = TaskName & "Task.eml"

' URI of the user's calendar folder.
strTaskUri = " & strExchSvrName & "/exchange/" & _
strMailbox & "/Task/"

' User name and password of appointment creator.
strUserName = strMailbox
strDomain = "SACRAMENTO"

strPassword = "P@ssw0rd"

' XML namespace info for the WebDAV request.
strXMLNSInfo = "xmlns:g=\""DAV:\""" & _
"xmlns:e=\"" & _
"xmlns:mapi=\"" " & _
"xmlns:mapit=\"" & _
"xmlns:dt=\""urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/\"" " & _
"xmlns:h=\"" & _
"xmlns:i=\"" & _
"xmlns:header=\""urn:schemas:mailheader:\"" " & _
"xmlns:mail=\""urn:schemas:httpmail:\"""

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>"


strHeaderInfo = "<header:to>" & "" & "</header:to>"

strMailInfo = "<mail:subject>Test Task Subject</mail:subject>" & _
"<mail:htmldescription>Test Description</mail:htmldescription>"

'// Build the XML body of the PROPPATCH request.
strApptRequest = "<?xml version=\""1.0\""?>" & _
"<g:propertyupdate " + strXMLNSInfo + ">" & _
"<g:set><g:prop>" & _
"<g:contentclass>urn:content-classes:task</g:contentclass>" & _
"<e:eek:utlookmessageclass>IPM.Task</e:eek:utlookmessageclass>" & _
strMailInfo & _
strTaskInfo & _
strHeaderInfo & _
"</g:prop></g:set>" & _
"</g:propertyupdate>"
MyCredentialCache = New System.Net.CredentialCache
MyCredentialCache.Add(New System.Uri(strTaskUri), "NTLM", New System.Net.NetworkCredential(strUserName, strPassword, strDomain))

' Create the HttpWebRequest object.
PROPPATCHRequest = CType(System.Net.HttpWebRequest.Create(strTaskUri & strTaskItem), _
System.Net.HttpWebRequest)

PROPPATCHRequest.Credentials = MyCredentialCache
PROPPATCHRequest.Method = "PROPPATCH"

PROPPATCHRequest.ContentType = "text/xml"

' Encode the body using UTF-8.
bytes = System.Text.Encoding.UTF8.GetBytes(strApptRequest)


PROPPATCHRequest.ContentLength = bytes.Length
PROPPATCHRequestStream = PROPPATCHRequest.GetRequestStream()
PROPPATCHRequestStream.Write(bytes, 0, bytes.Length)
PROPPATCHRequestStream.Close()
PROPPATCHResponse = CType(PROPPATCHRequest.GetResponse(), System.Net.HttpWebResponse)
PROPPATCHResponse.Close()

MsgBox("Task successfully created.")


Catch ex As Exception
MsgBox(ex.Message)

End Try


I get the following error message on this line:

PROPPATCHResponse = CType(PROPPATCHRequest.GetResponse(), System.Net.HttpWebResponse)

Error Message:
"The remote server returned an error: (400) Bad Request"

I am able to successfully add Calendar Appointment to the same Outlook book using similar code (i copied the code from ) - and it worked successfully for Calendar Appointment .

Then I saw the code above on this site:


converted it to VB .NET - ran it and it gives thsi Bad Request error. I'm trying to figure out why.

One thing I noticed is that the Task Property Names in the XMl in this code are coded in hexadecinal (i.e. 0x00008105 etc). why?

Can anyone help me with this one?
 
I've looked at another example on the web and modified my code to the below - but I'm still getting the same error at the line

PROPPATCHResponse = CType(PROPPATCHRequest.GetResponse(), System.Net.HttpWebResponse)

"The remote server returned an error: (400) Bad Request"



strExchSvrName = "SACINF02"

strMailbox = Environment.UserName

strMailbox = Right(strMailbox, Len(strMailbox) - InStr(strMailbox, "\"))

strTaskItem = "testtask.eml"

strTaskUri = " & strExchSvrName & "/exchange/" & _
strMailbox & "/tasks/"


strUserName = strMailbox
strDomain = "SACRAMENTO"

strPassword = thepassword


' XML namespace info for the WebDAV request.
strXMLNSInfo = "xmlns:d=""DAV:""" & _
"xmlns:e="" " & _
"xmlns:b=""urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/"" " & _
"xmlns:f=""urn:schemas:mailheader:"" " & _
"xmlns:g=""urn:schemas:httpmail:""" & _
"xmlns:h=""

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:0x8517 b:dt=""dateTime.tz"">2005-10-10T13:30:00.00Z</h:0x8517>"
' 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:eek:utlookmessageclass>IPM.Task</e:eek:utlookmessageclass>" & _
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>" & _


MyCredentialCache = New System.Net.CredentialCache
MyCredentialCache.Add(New System.Uri(strTaskUri & strTaskItem), "NTLM", New System.Net.NetworkCredential(strUserName, strPassword, strDomain))


PROPPATCHRequest = CType(System.Net.HttpWebRequest.Create(strTaskUri & strTaskItem), _
System.Net.HttpWebRequest)


PROPPATCHRequest.Credentials = MyCredentialCache

' Specify the PROPPATCH method.
PROPPATCHRequest.Method = "PROPPATCH"
PROPPATCHRequest.Headers.Add("Translate", "t")
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
PROPPATCHResponse = CType(PROPPATCHRequest.GetResponse(), System.Net.HttpWebResponse)
Catch ex As Exception
MsgBox(ex.ToString())
End Try


MsgBox("Task successfully created.")
PROPPATCHResponse.Close()


This is completely baffling me - people who have posted this code on their website are saying that the code is working for them ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top