none
Creating an Outlook Calendar event into a specifc profile's folder through C#/WPF RRS feed

  • Question

  • Dear All

    I have been trying for days to get this solution to work but I find it's not running well.

    Essentially I need to create an Outlook Calendar event, Update (to change due dates) and finally close the event by deleting it.

    I managed to get the code to work in creating into the default (currently open Outlook profile).

    It is fine for my personal mailbox because I don't have many events.

    However I need the app to create it into my team mailbox which is a different outlook profile. (i get to choose outlook profiles when i open outlook).

    First question:

    How do I create/update calendar events into a specific outlook profile?

     public void SendMail(string EmailUrl)
            {
    
                try
                {
    
                    Outlook.Application otApp = new Outlook.Application();// create outlook object
                    Outlook.MailItem otMsg = (Outlook.MailItem)otApp.CreateItem(Outlook.OlItemType.olMailItem); // Create mail object
                    Outlook.Recipient otRecip = (Outlook.Recipient)otMsg.Recipients.Add(EmailUrl);
                    otRecip.Resolve();// validate recipient address
                    otMsg.Subject = "[XXXX] LOAN LAPTOP NOTIFICATION - TICKET: " + text_tTicketId.Text + " | Due: " + date_dueDate.Text + "";
                    otMsg.Body = "You ( " + text_cUserId.Text + " ) have loaned a laptop from us with serial number " + text_aSerialNo.Text + ". It is due to be returned by " + date_dueDate.Text + ". Do return an email to the CI Enquiry Mailbox should you have further enquiries.";
                    otMsg.Save();
                    otMsg.Send();
                    MessageBox.Show("Email sent via outlook");
                    //Begin Outlook Calender Insertion
    
                    
                    Outlook.AppointmentItem oAppointment = (Outlook.AppointmentItem)otApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
                    oAppointment.Subject = "[FSRAMS] Laptop(" + text_aAssetNo.Text + ") Loan(" + text_tTicketId.Text + ") Due for return.";
                    oAppointment.Body = "Please contact customer " + text_cUserId.Text + " to return their loan device.";
                    oAppointment.Start = Convert.ToDateTime(date_dueDate.Text);
                    oAppointment.End = Convert.ToDateTime(date_dueDate.Text);
                    oAppointment.ReminderSet = true; // Set the reminder
                    oAppointment.ReminderMinutesBeforeStart = 15; // reminder time
                    oAppointment.Importance = Outlook.OlImportance.olImportanceHigh; // appointment importance
                    oAppointment.BusyStatus = Outlook.OlBusyStatus.olBusy;
                    oAppointment.Save();
    
                    OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["FSRAMSDBConn"].ConnectionString); //Create database command object
                    OleDbCommand cmd = new OleDbCommand();
                    conn.Open();
                    cmd.Connection = conn;
                    cmd.CommandText = "UPDATE Ticket SET Ticket.tEntryID = '" + oAppointment.EntryID.ToString() + "' WHERE (((Ticket.tTicketId)='" +text_tTicketId.Text + "'))";
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Your EntryID is inserted.", "Update Complete");

    This code allows me to create fine into the default calendar, but i want it to create specifically into the team exchange account calendar which is a separate account i call "CI.Enquiry".

    Second Question:

    When I delete the calendar, the app gets very slow and effectively hangs for a long time as it seems to be querying each of the calendar events, my personal calendar is small so it's okay. On the team calendar it's very packed and therefore the app gets very slow. I find this inefficient.

    Is there a way for me to get it to only load the specific event i want based on entry ID?

    I am planning to create the app into a separate calender folder created just for this app. is that a good idea? And if so, how do i get Outlook to create into this specific mailbox folder?

    here is my delete code:

    private void deleteCalendarEvent()
            {
                Microsoft.Office.Interop.Outlook.Application OlApp = new Microsoft.Office.Interop.Outlook.Application();
                NameSpace OlNamspace = OlApp.GetNamespace("MAPI");
                MAPIFolder AppointmentFolder = OlNamspace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
                AppointmentFolder.Items.IncludeRecurrences = true;
                foreach (AppointmentItem app in AppointmentFolder.Items)
                {
                    if (app.EntryID == SelectedOutlookEntryID)
                    {
                        app.Delete();
    
    
                        Outlook.Application otApp = new Outlook.Application();// create outlook object
                        Outlook.MailItem otMsg = (Outlook.MailItem)otApp.CreateItem(Outlook.OlItemType.olMailItem); // Create mail object
                        Outlook.Recipient otRecip = (Outlook.Recipient)otMsg.Recipients.Add("XXXXXX@XXXXX.COM");
                        otRecip.Resolve();// validate recipient address
                        otMsg.Subject = "[XXXXXX] LOAN LAPTOP RETURNED - TICKET: " + text_tTicketId.Text + " | Due: " + date_dueDate.Text + "";
                        otMsg.Body = "Customer ( " + txt_cUserId.Text + " ) has returned a laptop with serial number " + txt_aSerialNo.Text + ". The ticket on FSRAMS is now closed. Entry recorded by " + LastModifiedBy + ".";
                        otMsg.Save();
                        otMsg.Send();
                        MessageBox.Show("Outlook Calendar Item Deleted, email dispatched to CI Enquiry Mailbox.", "Success!");
                    }
                } 
            }

    Hope I can get some input on this, I'm really lost now :(


    Friday, July 25, 2014 2:52 AM

Answers

  • Hello Brandon,

    First of all, I have noticed that you don't release underlying COM objects properly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more in the Systematically Releasing Objects article.

    > How do I create/update calendar events into a specific outlook profile?

    The Logon method of the Namespace class logs the user on to MAPI, obtaining a MAPI session. Here is what the MSDN library states:

    Use the Logon method only to log on to a specific profile when Outlook is not already running. This is because only one Outlook process can run at a time, and that Outlook process uses only one profile and supports only one MAPI session. When users start Outlook a second time, that instance of Outlook runs within the same Outlook process, does not create a new process, and uses the same profile.

    If Outlook is already running, using this method does not create a new Outlook session or change the current profile to a different one.

    If Outlook is not running and you only want to start Outlook with the default profile, do not use the Logon method.

    > Is there a way for me to get it to only load the specific event i want based on entry ID?

    The Namespace class provides the GetItemFromId method which returns a Microsoft Outlook item identified by the specified entry ID (if valid). For more information about Entry IDs, see the EntryID property.

    > I am planning to create the app into a separate calender folder created just for this app. is that a good idea? And if so, how do i get Outlook to create into this specific mailbox folder?

    I don't quite understand you. Do you want to create a separate calendar folder in Outlook?

    The Folders collection (see the corresponding property of the Folder class) provides the Add method which creates a new folder in the Folders collection. The second parameter allows you to specify the Outlook folder type for the new folder. If the folder type is not specified, the new folder will default to the same type as the folder in which it is created.

    • Marked as answer by Brandon Choo Tuesday, July 29, 2014 2:17 AM
    Friday, July 25, 2014 8:08 AM