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.
LVL 6
HardiAsked:
Who is Participating?
 
LeeDerbyshireConnect With a Mentor Commented:
Well, I have to admit that I don't know much about C# - I saw this question in the Exchange admin area, and thought I'd answer it because I do some coding, too.  But I only really use VB.  At a guess, I'd say you'd need something like this:

GETRequest = (System.Net.HttpWebRequest)
  HttpWebRequest.Create(strCalendarUri + strApptItem);
GETRequest.Credentials = MyCredentialCache;
GETRequest.Method = "GET";
GETResponse = (System.Net.HttpWebResponse)
  GETRequest.GetResponse();
If GETRequest.StatusCode = "404"
  (// Use your code to add the appointment )
GETResponse.Close();

To be honest, I think it would be easier to always assume that it exists, and to use the subject and add some random characters based on the date/time etc. as described above.

http://server/exchange/users/calendar/TestAppointment-20070911-132855-85634.eml
0
 
LeeDerbyshireCommented:
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
0
 
HardiAuthor Commented:
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?
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
LeeDerbyshireCommented:
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.
0
 
HardiAuthor Commented:
Sounds good :-D
How exactly do I "do a GET"?
0
 
LeeDerbyshireCommented:
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?
0
 
HardiAuthor Commented:
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
0
 
HardiAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.