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!

Problem with Creating Task Item in Exchange mailbox using WebDAV .NET

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:

Imports System.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 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 mailbox using WebDAV - the code for that is at:

-

It's just failing when I try to add a Task.

my code above is based on the example on this webpage:


I've looked at a couple of examples for Creating Tasks using WEbDAV in .NET - and the people who posted them say they worked for them.

I'm not sure why they're not working for me. The Calendar example worked - so I don't think it has something to do with my Exchange server.

Would appreciate any help in this matter.
 
I got it to work -

There were formatting problems in the XML - for e.g there are no spaces after each line in the xmlns tag


strXMLNSInfo = "xmlns:g=\""DAV:\""" & _
"xmlns:e=\"" & _

should be


strXMLNSInfo = "xmlns:g=\""DAV:\"" " & _
"xmlns:e=\"" & _

Pretty basic error...


Does anyone know how to set the Priority Property of the Task Item. Like what the hexadecimal code for that is.


Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top