Link to home
Start Free TrialLog in
Avatar of Hardi
Hardi

asked on

WebDav & Exchange: Create appointment, avoid overwrite existing one

I can create calendar appointments on an Exchange server using WebDav.
Normally I would set the appointment filename to be subject+".eml"
But if there are 2 appointments with the same subjects they would overwrite each other.

Anyone know the solution?
I am thinking perhaps I should check if http://blah/exchange/blah/Calendar/blah.eml already exists, but I don't know how to do that.

I am using VS 2005, C#, class library.
Avatar of LeeDerbyshire
LeeDerbyshire
Flag of United Kingdom of Great Britain and Northern Ireland image

The destination URL doesn't need to be based on the subject, Exchange just does that by default, and adds incrementing numbers to the end if required.  The easiest thing to do is to create a unique URL based on the date and time, and then add a random number on the end.  You will end up with something like

http://server/exchange/user/calendar/20070910-140625-86527.eml
Avatar of Hardi
Hardi

ASKER

Thanks LeeDerbyshire
I thought of that too.. but I just want to keep having subjects as filenames, just as Exchange does.
It is also easier to find the item in Exchange Explorer.
Do you know how to check if that URL already exists?
Do a GET to the URL.  If you get a 404 response, then it doesn't exist.  A 200 means OK, and it does exist.  If it exists, then add something unique to the end.
Avatar of Hardi

ASKER

Sounds good :-D
How exactly do I "do a GET"?
Well, to create the appointments, I guess you are using a WinHTTPRequest object, or something similar, to send a PROPPATCH request? To see if something exists, try sending a GET instead, and look at the status of the response.  Maybe you could show the code you are using to create the appointments?
Avatar of Hardi

ASKER

Actually I don't understand my code at all... I couldn't find any tutorial about webdav

                PROPPATCHRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(
                    strCalendarUri + strApptItem); // this is the URL http://....eml
                PROPPATCHRequest.Credentials = MyCredentialCache;
                PROPPATCHRequest.Method = "PROPPATCH";
                bytes = Encoding.UTF8.GetBytes((string)strApptRequest); // strApptRequest is the xml
                PROPPATCHRequest.ContentLength = bytes.Length;
                PROPPATCHRequestStream = PROPPATCHRequest.GetRequestStream();
                PROPPATCHRequestStream.Write(bytes, 0, bytes.Length);
                PROPPATCHRequestStream.Close();
                PROPPATCHRequest.ContentType = "text/xml";
                PROPPATCHResponse = (System.Net.HttpWebResponse)PROPPATCHRequest.GetResponse(); // Create the appointment in the Calendar folder of the user's mailbox.
                PROPPATCHResponse.Close();

Thank you LeeDerbyshire
ASKER CERTIFIED SOLUTION
Avatar of LeeDerbyshire
LeeDerbyshire
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Hardi

ASKER

Thanks LeeDerbyshire it's close. I got it working

                try
                {
                    string originalName = strApptItem;
                    for(int n=1; n<=1000; n++)
                    {
                        GETRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strCalendarUri + strApptItem);
                        GETRequest.Credentials = MyCredentialCache;
                        GETRequest.Method = "GET";
                        GETResponse = (System.Net.HttpWebResponse)GETRequest.GetResponse();
                        GETResponse.Close();
                        strApptItem = originalName + n.ToString();
                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message.IndexOf("(404)") == -1)
                        throw ex;
                }

I don't assume it exists because all subjects are going to contain unique IDs... But just in case... =P