16

I am trying to find items from deleted items folder given the items unique id

ItemId id = new ItemId("zTK6edxaI9sb6AAAQKqWHAAA");
SearchFilter.IsEqualTo filter = new SearchFilter.IsEqualTo(ItemSchema.Id, id);
ItemView view = new ItemView(10);
view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Subject);
FindItemsResults<Item> results = _ExchangeService.FindItems(WellKnownFolderName.DeletedItems, filter, view);
Console.WriteLine(results.TotalCount);

This code returns an exception saying:

Validation failed.
Parameter name: searchFilter

The same code works if I search for a message with Subject.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
nilobarp
  • 3,806
  • 2
  • 29
  • 37

2 Answers2

37

You don't need to use FindItems if you already know the ItemId

EmailMessage email = EmailMessage.Bind(service, new ItemId(StringItemId));
Abhi
  • 824
  • 9
  • 8
  • Do you know of any way to ensure the ID exists before, so that the Bind method will not except on an item that no longer exists? – namezero Nov 29 '18 at 13:32
  • This is great! Helped me figure out folder names as well in PoweShell. [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service, [Microsoft.Exchange.WebServices.Data.FolderId]("*************************")) – Douglas Plumley Nov 30 '18 at 20:48
6

You cannot search on a ComplexProperty such as the ItemId. I'm assuming that Item.Bind won't work due to the item being moved, which changed the ItemId?

If that's the case, then you'll need to use a SearchFilter on another property. If these are Items that you created via EWS, you could attach a unique Extended Property to each and use that if you need to search for one.

user1017413
  • 2,023
  • 4
  • 26
  • 41
  • Yes the change of ItemIDs proved fatal and I realized that it is not a reliable mechanism for correlating items that were moved to some other folder like Inbox to Deleted items. – nilobarp Dec 18 '13 at 19:02
  • The extended property will be blatenly copied on item copy in Outlook, making the unique ID not-so-unique anymore. – namezero Nov 29 '18 at 13:32