Link to home
Create AccountLog in
Exchange

Exchange

--

Questions

--

Followers

Top Experts

Avatar of Angel02
Angel02

The remote server returned an error: (409) Conflict. WebDav
I am trying to add an appointment to the public calendar on exchange using WebDav. I using the attached code that I obtained from one of the forums.

I get the error
The remote server returned an error: (409) Conflict.

Can anyone please tell me what might cause this?
Public Function AddToCalendar(ByVal MtgDate As Date, ByVal Loc As String, ByVal MtgName As String, ByVal InstrName As String)


        ' Variables.
        Dim oRequest As HttpWebRequest
        Dim oResponse As WebResponse
        Dim MyCredentialCache As CredentialCache
        Dim oRequestStream As System.IO.Stream
        Dim sb As StringBuilder = New StringBuilder
        Dim strCalendarUri As String = "http://ServerName/public/PublicCalendar/"

        Dim apptStartTime As String = MtgDate.ToString("yyyy-MM-ddTHH:mm:ss")

        Dim apptEndTime As String = MtgDate.ToString("yyyy-MM-ddTHH:mm:ss")

        Dim strMailbox As String = String.Empty
        strMailbox = "skallem@gosystemsinc.com"

            ' Appointment item.
        Dim strApptItem As String = Guid.NewGuid().ToString() + ".eml"

            ' User name and password of appointment creator.
        Dim strUserName As String = "Username"
        Dim strPassword As String = "Password"

        ' XML namespace info for the WebDAV request.
        Dim strXMLNSInfo As String = "xmlns:g=""DAV:"" " _
                                  & "xmlns:e=""http://schemas.microsoft.com/exchange/"" " _
                                  & "xmlns:mapi=""http://schemas.microsoft.com/mapi/"" " _
                                  & "xmlns:mapit=""http://schemas.microsoft.com/mapi/proptag/"" " _
                                  & "xmlns:x=""xml:"" xmlns:cal=""urn:schemas:calendar:"" " _
                                  & "xmlns:dt=""urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/"" " _
                                  & "xmlns:header=""urn:schemas:mailheader:"" " _
                                  & "xmlns:mail=""urn:schemas:httpmail:"""

        ' Set the appointment item properties.  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.
        Dim strCalInfo As String = "<cal:location>" & Loc & "</cal:location>" _
                                & "<cal:dtstart dt:dt=""dateTime.tz"">" _
                                & apptStartTime _
                                & "</cal:dtstart>" _
                                & "<cal:dtend dt:dt=""dateTime.tz"">" _
                                & apptEndTime _
                                & "</cal:dtend>" _
                                & "<cal:instancetype dt:dt=""int"">0</cal:instancetype>" _
                                & "<cal:busystatus>BUSY</cal:busystatus>" _
                                & "<cal:meetingstatus>CONFIRMED</cal:meetingstatus>" _
                                & "<cal:alldayevent dt:dt=""boolean"">0</cal:alldayevent>" _
                                & "<cal:responserequested dt:dt=""boolean"">1</cal:responserequested>" _
                                  & "<cal:reminderoffset dt:dt=""int"">900</cal:reminderoffset>"

        ' Set the required attendee of the appointment.
        Dim strHeaderInfo As String = "<header:to>" + strMailbox + "</header:to>"

        ' Set the subject of the appointment.
        Dim strMailInfo As String = "<mail:subject>" _
                                 & MtgName _
                                 & "</mail:subject>" _
                                 & "<mail:htmldescription>" _
                                 & "Location: " _
                                 & Loc _
                                 & "</mail:htmldescription>"

        ' Build the XML body of the HTTP request.
        Dim strApptRequest As String = "<?xml version=""1.0""?>" _
                                & "<g:propertyupdate " + strXMLNSInfo + ">" _
                                & "<g:set><g:prop>" _
                                & "<g:contentclass>urn:content-classes:appointment</g:contentclass>" _
                                & "<e:outlookmessageclass>IPM.Appointment</e:outlookmessageclass>" _
                                & strMailInfo _
                                & strCalInfo _
                                & "<mapi:finvited dt:dt=""boolean"">1</mapi:finvited>" _
                                & "</g:prop></g:set>" _
                                & "</g:propertyupdate>"

        ' 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(strCalendarUri), _
            "NTLM", New System.Net.NetworkCredential(strUserName, strPassword))

        ' Create the HttpWebRequest object.
        oRequest = HttpWebRequest.Create(strCalendarUri + strApptItem)

        
        ' Add the network credentials to the request.
        oRequest.Credentials = MyCredentialCache

        ' Specify the method.
        oRequest.Method = "POST"

        ' Set the content header length.  This must be
        ' done before writing data to the request stream.
        oRequest.ContentLength = strApptRequest.Length

        ' Get a reference to the request stream.
        oRequestStream = oRequest.GetRequestStream()

        ' Write the message body to the request stream.
        oRequestStream.Write(Encoding.UTF8.GetBytes(strApptRequest), 0, strApptRequest.Length)

        ' Close the Stream object to release the connection
        ' for further use.
        oRequestStream.Close()

        ' Set the content type header.
        oRequest.ContentType = "text/xml"

        '' Create the appointment in the Calendar folder of the
        ' user's mailbox.
        oResponse = oRequest.GetResponse()

        ' Clean up.
        oResponse.Close()

        
    End Function

Open in new window

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


ASKER CERTIFIED SOLUTION
Avatar of LeeDerbyshireLeeDerbyshire🇬🇧

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of Angel02Angel02

ASKER

Thanks. Your are right. So I changed the method to "PROPATCH" and I get the error

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

Attached is the request as it is sent. Can someone figure out whats wrong ?
<?xml version="1.0"?><g:propertyupdate xmlns:g="DAV:" xmlns:e="http://schemas.microsoft.com/exchange/" xmlns:mapi="http://schemas.microsoft.com/mapi/" xmlns:mapit="http://schemas.microsoft.com/mapi/proptag/" xmlns:x="xml:" xmlns:cal="urn:schemas:calendar:" xmlns:dt="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" xmlns:header="urn:schemas:mailheader:" xmlns:mail="urn:schemas:httpmail:"><g:set><g:prop><g:contentclass>urn:content-classes:appointment</g:contentclass><e:outlookmessageclass>IPM.Appointment</e:outlookmessageclass><mail:subject>Meeting </mail:subject><mail:htmldescription>Location: Office</mail:htmldescription><cal:location>Office</cal:location><cal:dtstart dt:dt="dateTime.tz">2011-08-18T10:30:00</cal:dtstart><cal:dtend dt:dt="dateTime.tz">2011-08-18T10:30:00</cal:dtend><cal:instancetype dt:dt="int">0</cal:instancetype><cal:busystatus>BUSY</cal:busystatus><cal:meetingstatus>CONFIRMED</cal:meetingstatus><cal:alldayevent dt:dt="boolean">0</cal:alldayevent><cal:responserequested dt:dt="boolean">1</cal:responserequested><cal:reminderoffset dt:dt="int">900</cal:reminderoffset><mapi:finvited dt:dt="boolean">1</mapi:finvited></g:prop></g:set></g:propertyupdate>

Open in new window


SOLUTION
Avatar of LeeDerbyshireLeeDerbyshire🇬🇧

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of Angel02Angel02

ASKER

It looks like that's it ! Thanks. That time format got rid of the error.

I set the datetime format as ""yyyy-MM-ddTHH:mm:ss.000Z"
But the time in the appointment is saved as 2011-08-18 06:30:00
Also, when I try to set the time in PM like 17:00:00, it is throwing the Bad Request error.

How do I set the datetime format to work in EST?

Avatar of LeeDerbyshireLeeDerbyshire🇬🇧

Unfortunately, you will always need to do it in the 2011-08-18T10:30:00.000Z form - it won't accept anything else.  You also need to supply the date/time in UTC, which means you have to calculate the difference yourself.  Quite tricky, because you have to take account of daylight saving time, too.  This is one of the most annoying aspects of Exchange/WebDAV stuff.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Angel02Angel02

ASKER

Ok. I would calculate the timezone difference myself.

Something strange is happening though. The code is working right sometimes, and saving the appointment on Exchange. But sometimes its throwing the Bab Request (400) randomly. I have to refresh the application or just wait for sometime and it starts working again. What can possibly be the reason for such random error ?

Avatar of LeeDerbyshireLeeDerbyshire🇬🇧

You'll have to somehow get it to show you the request when it fails.  Maybe the text used for the date value isn't always what you think it's going to be.  See if it helps (or if it's even possible) to set the duration, rather than the end time, and let it work out the end time by itself.

Avatar of Angel02Angel02

ASKER

I captured the request as it was being sent. i also set the duration instead of end-time.
but the request sent when the response failed and the request sent when the response succeeded were exactly the same. I have attached both the requests.

The problem doesn't seem to be with the request. Can it be something to do with the Exchange server? Also, can it be due to the order of the lines in the code ? I have attached the code again. Can you please let me know if you find anything else wrong in the code? The error is completely random.
Worked.xml

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Angel02Angel02

ASKER

Here are other files.
DidNotWork.xml
AddCalendar.txt

Avatar of LeeDerbyshireLeeDerbyshire🇬🇧

Hm.  The worst kind of problem - something that works some times, and not others.  The XML looks fine; and since it works sometimes, I think we can assume it's valid.  Same for the code.  There is another cause for the 400 error, but again, I would not expect it to be intermittent.  You will get a 400 error if an Exchange WebDAV request goes to the wrong site on the server.  This can happen when there are several sites, and the server doesn't know which site to send the request to.  You will only know if this has happened if you look at the iis log files for sites other than the default web site (if there are any, that is), and you find the PROPATCH requests (and the 400 responses) in them.  But like I say, it shouldn't do this intermittently.

Avatar of Angel02Angel02

ASKER

Thank you for the suggestion. So I looked at the Log Files. The requests were all different from each other without a certain pattern. I realized some were not obtaining the domain name automatically. So I added the domain name to the Username. (domain\Username)

Then I saw a proper pattern. But that made things worse. The requests would look exactly the same but one of them would randomly return 400. I have attached 3 requests as example, one of them returning 400.

I checked all other websites apart from Default and none of them have a PROPATCH request. The Default Website has a bunch of sub-directories, one of which is "public" which is what I think I am accessing.

Do you think this is Normal or should I contact my Network admin ? It really doesn't look like the problem with the code but with the Exchange Server.
2011-08-08 19:49:53 192.123.1.1 PROPPATCH /public/PublicCalendar/fff2daed-7096-4b97-9fb2-cd3cdeab7d46.eml - 80 - 192.123.1.2 - 401 2 2148074254
2011-08-08 19:49:53 192.123.1.1 PROPPATCH /public/PublicCalendar/fff2daed-7096-4b97-9fb2-cd3cdeab7d46.eml - 80 Domain\Admin 192.123.1.2 - 207 0 0
2011-08-08 19:56:34 192.123.1.1 PROPPATCH /public/PublicCalendar/98b16a95-7390-435f-8aa0-d717481fd405.eml - 80 - 192.123.1.2 - 401 2 2148074254
2011-08-08 19:56:34 192.123.1.1 PROPPATCH /public/PublicCalendar/98b16a95-7390-435f-8aa0-d717481fd405.eml - 80 Domain\Admin 192.123.1.2 - 400 0 0
2011-08-08 19:56:47 192.123.1.1 PROPPATCH /public/PublicCalendar/49cf4c0a-faa1-44e1-925c-1e77630b656b.eml - 80 - 192.123.1.2 - 401 2 2148074254
2011-08-08 19:56:47 192.123.1.1 PROPPATCH /public/PublicCalendar/49cf4c0a-faa1-44e1-925c-1e77630b656b.eml - 80 Domain\Admin 192.123.1.2 - 207 0 0

Open in new window


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of LeeDerbyshireLeeDerbyshire🇬🇧

And for all of the requests logged above the XML was the same?  Or is it not possible to be sure any more?  There is a document dedicated to 400 errors here (which just goes to show you there's an article for just about everything out there):

http://www.getnetgoing.com/HTTP-400.html

Some of it is obviously not relevant, but something might be.

It's interesting that the subcode is 0 (so your error is really 400.0).  The 0 subcode isn't valid for IIS, so it must be from the WebDAV ISAPI itself (it is known not be too helpful with its subcodes).
Exchange

Exchange

--

Questions

--

Followers

Top Experts

Exchange is the server side of a collaborative application product that is part of the Microsoft Server infrastructure. Exchange's major features include email, calendaring, contacts and tasks, support for mobile and web-based access to information, and support for data storage.