Problems with deleted items in Outlook

M

Mathieu Maes

Hi everyone,

I'm developing an Outlook 2007 add-in to synchronize the Outlook
agenda with a webbased tool using a web service.
So far I'm able to import and export appointments from/to the web
service.

Now I'm stuck with deleted items. If a user deletes an appointment in
Outlook, and the add-in performs an import, it should be recreated.

I use following code to find a certain Outlook item:
private static AppointmentItem findOutlookItem(string olID)
{
try
{
Microsoft.Office.Interop.Outlook.Application Ol = new
Microsoft.Office.Interop.Outlook.Application();
MAPIFolder OlMapi = Ol.Session.GetDefaultFolder
(OlDefaultFolders.olFolderCalendar);
return (AppointmentItem)Ol.Application.GetNamespace
("MAPI").GetItemFromID(olID, OlMapi.StoreID);
}
catch (System.Exception ex)
{
ionPM.Logging.Write("ionSync.Core.findOutlookItem",
ex.ToString());
return null;
}
}

Whenever you delete an appointment in Outlook, it actually moves the
item to the "deleted items" folder.

In theory this code should return null, but the weird thing is that
above code still returns the (deleted) items, even though it's not
supposed to look in the "deleted items" folder!

I tried a workaround where I do a OlItem.Move
(Ol.Session.GetDefaultFolder(OlDefaultFolders.olFolderCalendar)), but
this method returns an exception with the message "Unable to perform
this action because the item is deleted".

The _AppointmentItem interface doesn't seem to have a variable that
can help me determine if the item is deleted or not.

Does anyone know how I can solve this ?

Kind regards,
Mathew
 
K

Ken Slovak - [MVP - Outlook]

When an item is deleted and moved to Deleted Items the EntryID may or may
not change. That's up to the store provider. In the case of a PST file the
EntryID doesn't change, in an Exchange mailbox it does. GetItemFromID() will
return an item from anywhere in that store, so the behavior you see is
expected.

Get the item's Parent property. Parent will be the folder the item is in and
you can then check the Name to see if it's Deleted Items.

For an Outlook addin never create a new Outlook.Application object. Use the
one passed to you in OnConnection() or for VSTO in the Startup() handler.

Also, don't use compound dot operators such as
Ol.Application.GetNameSpace().GetItemFromID(). For one thing,
Ol.Application is redundant. Secondly, each dot operator you use will create
invisible objects, one for each dot operator, that you have no control over.
Instead, get a specific NameSpace object and then use that to get the item.
Same for any other constructs like that.
 
M

Mathieu Maes

When an item is deleted and moved to Deleted Items the EntryID may or may
not change. That's up to the store provider. In the case of a PST file the
EntryID doesn't change, in an Exchange mailbox it does. GetItemFromID() will
return an item from anywhere in that store, so the behavior you see is
expected.

Hi Ken,

Thanks for your reply.

I found my error right before reading your post, so I'm replying for
future reference. I assumed that the storeID was an identifier for a
specific folder, which was my mistake. I managed to add a verification
using _AppointmentItem.parent, like you suggested.

I didn't really like the idea to compare the name with a fixed string
"Deleted items". I believe people can rename it, or it can be
different in localized version. Or, people can create a new folder,
calling it "Deleted items"... This is why I'm now comparing the
parent's fullpath with the one in Ol.Session.GetDefaultFolder().


Kind regards,
Mathew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top