Extending LINQ to DASL with Custom DASL Properties
In an earlier post I discussed LINQ to DASL, part of the Office Interop API Extensions, which is one of the forthcoming VSTO Power Tools. LINQ to DASL allows you to write LINQ expressions against Outlook item collections. I also mentioned that many known DASL properties were not mapped to their Outlook item equivalents in this initial release. So how do you write LINQ expressions using DASL properties that weren't included? The answer is: by extending the base LINQ to DASL classes. Let's start with a non-LINQ example:
Outlook.Folder folder = (Outlook.Folder) this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
var topics = new List<string>();
foreach (object item in folder.Items)
{
Outlook.MailItem mailItem = item as Outlook.MailItem;
if (mailItem != null)
{
if (mailItem.ConversationTopic.Contains("VSTO"))
{
topics.Add(mailItem.ConversationTopic);
}
}
}
foreach (var topic in topics)
{
System.Diagnostics.Debug.WriteLine(String.Format("Conversation Topic: {0}", topic));
}
The example shows our first attempt to find all mail items with a conversation index containing the acronym "VSTO". It simply iterates over each item in the folder, checks whether it's a mail item, then checks whether it has a matching conversation topic. Iterating over each and every item in the folder is not very efficient. We can have Outlook perform the filtration in its own native (and presumably more efficient) way. We do this by creating a DASL query string and passing it to the Items.Restrict() method. Here is the updated example:
Outlook.Folder folder = (Outlook.Folder) this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
string filter = @"@SQL=(""urn:schemas:httpmail:thread-topic"" LIKE '%VSTO%')";
Outlook.Items items = folder.Items.Restrict(filter);
var topics = new List<string>();
foreach (object item in items)
{
Outlook.MailItem mailItem = item as Outlook.MailItem;
if (mailItem != null)
{
topics.Add(mailItem.ConversationTopic);
}
}
foreach (var topic in topics)
{
System.Diagnostics.Debug.WriteLine(String.Format("Conversation Topic: {0}", topic));
}
The collection returned by Items.Restrict() is the subset of items matching the filter. It may be more efficient, but it certainly doesn't simplify the code. It would be nice if we could use a strongly-typed LINQ expression. Unfortunately, the Mail class in the initial LINQ to DASL implementation does not have a property which maps to the "urn:schemas:httpmail:thread-topic" DASL property. We can extend the Mail class to do so, however, as shown below:
internal class MyMail : Mail
{
[OutlookItemProperty("urn:schemas:httpmail:thread-topic")]
public string ConversationTopic
{ get { return Item.ConversationTopic; } }
}
We create a new class, MyMail, which inherits from the existing LINQ to DASL Mail class. We then add a ConversationTopic property which simply delegates to the ConversationTopic property on the Outlook.MailItem reference held by the Mail class. The key piece is the OutlookItemPropertyAttribute attached to the property. This attribute provides the mapping between the property as used in a LINQ expression and the DASL property in the query string. With that, we can finally write our LINQ expression:
Outlook.Folder folder = (Outlook.Folder) this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
var topics =
from item in folder.Items.AsQueryable<MyMail>()
where item.ConversationTopic.Contains("VSTO")
select item.ConversationTopic;
foreach (var topic in topics)
{
System.Diagnostics.Debug.WriteLine(String.Format("Conversation Topic: {0}", topic));
}
Extensions to LINQ to DASL are not limited to the addition of new DASL properties. You can also extend the LINQ to DASL classes to allow strongly-typed query expressions using custom user properties. I'll discuss that topic in a future post.
Comments
Anonymous
February 20, 2008
PingBack from http://msdnrss.thecoderblogs.com/2008/02/20/extending-linq-to-dasl-with-custom-dasl-properties/Anonymous
December 27, 2008
Hi when i try to run this code under vs 2005 -2008 I get an error Error 1 'test_console_app.Program' does not contain a definition for 'Application' and no extension method 'Application' accepting a first argument of type 'test_console_app.Program' could be found (are you missing a using directive or an assembly reference?) C:testtest console apptest console appProgram.cs 18 58 test console app it does not matter if I am using a console app or a winform I get the same error using Outlook = Microsoft.Office.Interop.Outlook; with refrance to com object 12Anonymous
December 29, 2008
Hi Mike, I'd need to see more of your code to identify the issue, but here are a couple of things to check:
- You've added references to BOTH the Outlook Interop assembly (Microsoft.Office.Interop.Outlook.dll) AND the Office Interop Extensions API assembly for Outlook (Microsoft.Office.Interop.Outlook.Extensions.dll).
- You've included references to the Microsoft.Office.Interop.Outlook, the Microsoft.Office.Interop.Outlook.Extensions, AND the Microsoft.Office.Interop.Outlook.Extensions.Linq namespaces.
- If you use a using statement that aliases a namespace (e.g. "using Outlook = ..."), then you've prefixed all uses of that namespace's types with that alias (e.g. Outlook.Application). Let me know if any of that helps. -Phil
- Anonymous
June 09, 2009
Can you please post the link to your post relating to this? You can also extend the LINQ to DASL classes to allow strongly-typed query expressions using custom user properties. I'll discuss that topic in a future post.