Jumping into Windows 7 Jump Lists

Filed under "Little changes that you probably didn't notice".

When Microsoft introduced Windows 7 to the desktop, one of the many usability features that they added were "Jump Lists". Jump Lists serve as a sort of shortcut to not just open an application, but "jump right in" to a particular feature or document that the application knows how to manage. It's all about convenience and reducing the number of clicks necessary to accomplish a task.

When developing SAS Enterprise Guide 4.3, we hooked into the "recent documents" aspect of the Jump List for two types of documents: project files (.EGP) and SAS programs (.SAS). Both of these are also easily accessible in SAS Enterprise Guide under File->Recent Projects and File->Recent Programs (new in 4.3).

If you use SAS Enterprise Guide often, you can drag the program icon to your Windows 7 task bar and "pin" it in place for convenience.  Then you can right-click on that icon to see a list of recently accessed projects and/or programs.

How to hook into the Jump List

The remainder of this post is aimed toward other application developers, as it describes how to hook into the "recent documents" feature of Windows, which automatically puts your documents in your application's Jump List. It's technical and it features .NET C# code, so if that's not your interest, then you may be excused.

To hook into the "recent documents" list, you need to call some Windows APIs -- specifically, the SHAddToRecentDocs routine within Shell32 (yes, even on a 64-bit system). That means you have use "platform invoke", or pinvoke, within .NET.

To use pinvoke, you first declare a .NET routine to wrap the call to the operating system. Then, you use that routine when appropriate from within your application. At the end of this post I included an example of a C# class to wrap the SHAddToRecentDocs routine, and a helper routine (AddToRecentDocs) to make it easy to use.

In your application, when the end user successfully opens or saves a document (whatever that might be for your application), you can then call the new routine to "register" that document. For example:

public void SaveDocument(string docFullpath)
{
// statements to save the document to disk go here
// ...
ShRecentDocs.AddToRecentDocs(docFullpath);
}

When used, all "recent document" items are added to the Windows registry at:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RecentDocs
The names are organized by file extension and are stored as binary blobs, so you might be able to verify that new files show up by examining the registry, but you probably cannot just eyeball it for specific entries.

Here's the wrapper class:

///
/// pinvoke wrapper for adding a local file to the "Recent Documents" list
/// in the Windows shell.
/// Source: pinvoke.net
/// http://www.pinvoke.net/default.aspx/shell32/SHAddToRecentDocs.html
///
sealed public class ShRecentDocs
{
private ShRecentDocs() { }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue")]
public enum ShellAddToRecentDocsFlags
{
Pidl = 0x001,
Path = 0x002,
}

[DllImport("shell32.dll", CharSet = CharSet.Ansi)]
public static extern void
SHAddToRecentDocs(ShellAddToRecentDocsFlags flag, string path);

[DllImport("shell32.dll")]
public static extern void
SHAddToRecentDocs(ShellAddToRecentDocsFlags flag, IntPtr pidl);

///
/// This is the simple function to call to add a document to the list
///
public static void AddToRecentDocs(string path)
{
SHAddToRecentDocs(ShellAddToRecentDocsFlags.Path, path);
}
}