Attempting to get UserProperties throws an operation failed error

Add-in Express™ Support Service
That's what is more important than anything else

Attempting to get UserProperties throws an operation failed error
 
nwein




Posts: 577
Joined: 2011-03-28
I've noticed this error happening every once in a while for some users. I could never replicate it, and it definitely not happening all the time but sometimes just attempting to get the UserProperties object from a MailItem (or a MeetingItem or an AppointmentItem) throws an
The operation failed.
error.
You can see a really simple sample of a code and where it breaks here:

private static void Test(MailItem mail)
{
	UserProperties userProperties = null;
	try
	{
		userProperties = mail.UserProperties; // <-- will throw an error here		
		// rest of the code...		
	}
	catch(Exception ex)
	{	
	}
	finally
	{
		if (userProperties != null) Marshal.ReleaseComObject(userProperties);
	}
}


Do you have any idea why this would happen?
Is there any way around this? I haven't dabbled with PropertyAccessor yet (i'm getting mixed messages from Microsoft regarding PropertyAccesor, on https://msdn.microsoft.com/en-us/library/ff870127(v=office.14).aspx page it says that it has performance implications but https://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.userproperties(v=office.14).aspx it says the opposite... :S) but would rather just know the reason and/or how I can avoid it.
Posted 29 Jul, 2015 15:30:11 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Nir,

I would check if the COM object passed to this method is released correctly. Can it be that the email gets deleted/moved when you call this method?


Andrei Smolin
Add-in Express Team Leader
Posted 30 Jul, 2015 05:03:20 Top
nwein




Posts: 577
Joined: 2011-03-28
The COM object is being released afterwards and besides, that happens later, at the time the method evaluates the COM object is valid and not separated from its RCW.
Can it be that the email gets deleted/moved when you call this method?

Yes, this is definitely a possibility (actually, very likely), this would also explain it not happening all the time but only on occasion (probably some rules to delete/move an item as).
How would this be an issue? Is it possible to tell if the object has moved/was deleted before checking for UserProperties or can I just leave it in those scenarios and accept that that's the how Outlook works?
Posted 30 Jul, 2015 10:10:55 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Nir,

Instead of caching the COM object, I would store the value retrieved via MailItem.EntryId and use NameSpace.GetItemFromId to re-create the COM object whenever required. In this case, GetItemFromId produces an exception if the item is moved/deleted. If you need to prevent moving/deleting the item, you can cancel the BeforeItemMove event; override the BeforeItemMove method of an Outlook Items (not Item!) Events class, see the Add New Item dialog of your add-in project.


Andrei Smolin
Add-in Express Team Leader
Posted 31 Jul, 2015 01:48:48 Top
nwein




Posts: 577
Joined: 2011-03-28
Ok, i'll see what makes more sense and try to implement it.
Thanks for the suggestions.
Posted 31 Jul, 2015 10:04:10 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
You are always welcome!


Andrei Smolin
Add-in Express Team Leader
Posted 31 Jul, 2015 10:10:55 Top