Light Up with Windows 7 Libraries

OK, it is time we to dive into the gist of Windows 7 Libraries. This is the fourth post in a series of Windows 7 Libraries posts. So far, we discussed what Windows 7 Libraries are (Understanding Windows 7 Libraries) and we reviewed the internal structure of Libraries and how they integrate into the Windows Shell Libraries Under The Hood. In this post, we review the different programming models developers can use when integrating Libraries into their applications.

Libraries are the new entry points for user’s data In Windows 7. Libraries are an integral part of the Shell and are promoted across Windows in Windows Explorer, Start Menu, and Common File Dialog. Windows 7 Users will use Libraries in their day-to-day activities. As such, they will expect applications that are running on Windows 7 to work properly with their Libraries and provide the same user experience as Windows Explorer. It is therefore important that, as developers, we know how to work with libraries and make our applications Libraries-aware.

But before we start, I want to note that while most applications should just work fine with Windows 7 Libraries, there is an opportunity to tap into the Windows 7 Libraries environment to provide a richer user experiences.

 Making an application Libraries-aware

One may wonder what would happen if “my application does not support Libraries.” Let’s imagine an application that, as part of its functionality, needs to save a file to the disk. The application prompts the user with the option to select a location for the file. The user may pick the Documents Library as his save "folder" since this is where the user usual goes when he needs to work on a document. However, if the application doesn’t recognize that the user selected the Documents Library and not a regular folder, the application may try to save to the Documents Library. This is a problem, since by now, as we already know, that Libraries are non –file system location and therefore can’t be address as regular folders.

A Library-aware application should include mechanisms for handling situations where users inadvertently try to pick libraries as if they are folder for either saving files to a Library or loading the contents of a Library. Furthermore, most applications allow users to interact with the file system as part of the application experience. An application should provide users with the same familiar entry points and UI offered by the Windows 7 Libraries. By including folders in a Library, users designate where their important data is stored, and thus applications should promote these locations by enabling Library browsing.

Developers have three integration points with Windows 7 that can help applications become Library-aware. You should review and become familiar with all three points and choose according to your needs.

  1. The most basic integration method is simply to use the new Common File Dialog (CFD) for picking files and folders while performing either save or load operations
  2. The second integration point offers applications the opportunity to Light Up on Windows 7 by enabling applications to select Libraries and consume Libraries contents.
  3. The last and most advanced integration point is to stay in sync with Library contents and directly manipulate Libraries by using the new IShellLibrary API that allows full control over libraries, including creating new libraries.

 

Let’s start by looking at the basic integration point, which can also be viewed as the bare minimum requirement for application compatibility when working with Windows 7 Libraries.

Using the Common File Dialog (CFD)

The good news is that Windows 7 Libraries are first class citizens in the CFD, allowing users to browse through Libraries, search Libraries, and even pick a specific Library as a save location, not an actual folder in the Library, but rather the Library itself!

But (there is always a but), we highly recommended that you use the new CFD interfaces that was introduced with Windows Vista, and not older or customized versions of the CFD. It is very important to note that the APIs for using the legacy CFD have not changed since Windows Vista, and XP to support application compatibility. However, the legacy version of the CFD (as is displayed in the next image) doesn’t directly support Libraries or the full new user experience offered in Windows 7.

image 

Even if Libraries are presented in the right navigation pane, they require an extra click to save into one of the folders rather than just the Library. Also the legacy CFD doesn’t expose the search and arrangement views functionality that libraries offer. Finally, the legacy CFD doesn’t support the multiple selections of files across several folders, a basic functionality promoted in Libraries.

Therefore, it is important to use the proper APIs to show the correct CFD version. When using .NET, developers can use either the System.Windows.Forms.FileDialog or the Microsoft.Win32.FileDialog namespace. The latter uses the legacy version of the CFD; therefore, .NET developers should always use the WinForms namespace to show the new CFD. The following is a code snippet that prompts the user to choose a save location by showing the common save file dialog as shown in the next image.

System.Windows.Forms.SaveFileDialog _fd = 
new System.Windows.Forms.SaveFileDialog();
_fd.Title = "Please choose a location to save your file";
_fd.FileName = "[Get Folder…]";
_fd.Filter = "Library|no.files";
if (_fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string dir_path = System.IO.Path.GetDirectoryName(_fd.FileName);
if (dir_path != null && dir_path.Length > 0)
{
//this returns the path to the default save location
lblResult.Content = dir_path;
}
}

image

Native code developers should use the new family of IFileDialog native APIs (IFileDialog, IFileOpenDialog, IFileSaveDialog, IFileDialogCustomize, IFileDialogEvents, IFileDialogControlEvents) that are replacing the legacy GetOpenFileName and GetSaveFileName APIs from earlier Windows versions.

The Shell native APIs are COM-based; therefore, before using any COM object we must remember to initialize the COM object by calling CoCreateInstance. As an example, the following code snippet prompts the user to choose a Library or a folder to save the file to, by showing the common save file dialog.

*ppsi = NULL;
IFileSaveDialog *pfod;
hr = CoCreateInstance(
CLSID_FileSaveDialog,
NULL,
CLSCTX_INPROC,
IID_PPV_ARGS(&pfod));

if (SUCCEEDED(hr))
{
hr = pfod->SetOptions(FOS_PICKFOLDERS);
if (SUCCEEDED(hr))
{
hr = pfod->Show(hWndParent);
if (SUCCEEDED(hr))
{
hr = pfod->GetResult(ppsi);
}
}
pfod->Release();
}

After initializing the *pfod IFileSaveDialog variable, we set the dialog options to pick folders by passing the FOS_PICKFOLDERS flag to the IFileOpenDialog.SetOptions(). This code tells the Open dialog to enable the user to choose folders rather than files, and allows the user to choose a Library. If choosing a library, the CFD will then return the default save location folder that is associated with the chosen Library.

The two above code snippets are very simple and don’t show any new code; however, it is important to promote consistency among applications running on Windows 7 and supporting Windows 7 Libraries.

In the next post, we will (finally) show the new Windows 7 library API, as well as how to consume Library contents with the existing Shell APIs.



Comments

  1. Posted on: April 16, 2009 at 8:47PM  

    I absolutely hate the legacy CFD. It is a sign of a low quality app. The sad thing is, a lot of apps use it. I love the ability to search in the new CFD.

  2. Posted on: April 16, 2009 at 10:46PM  

    The new and old Common File Dialogs (CFD) still don't remember view settings most of the time although Explorer windows do. Can you pls fix this? And the Customize Icon feature for Libraries?

  3. Posted on: April 18, 2009 at 3:06PM  

    Oh Yeah , For W-7  Good. Shares...

    Thanks '' Yochay Kiriaty ''

  4. Posted on: October 21, 2009 at 7:38PM  

    The given example:

    IFileSaveDialog->SetOptions(FOS_PICKFOLDERS)

    no longer appears to work (Windows 7 RTM).  API change?

Trackbacks

  1. Posted by: linkfeedr » Blog Archive » Light Up with Windows 7 Libraries - RSS Indexer (beta) on April 16, 2009 at 6:07PM

    Pingback from  linkfeedr  » Blog Archive   » Light Up with Windows 7 Libraries - RSS Indexer (beta)

  2. Posted by: Light Up with Windows 7 Libraries - Windows 7 for Developers - The … | www.windows7vista.com on April 16, 2009 at 8:03PM

    Pingback from  Light Up with Windows 7 Libraries - Windows 7 for Developers - The … | www.windows7vista.com

  3. Posted by: Tech News, Resources from Blogosphere - 17 April 09(10) | Best Webhosting on April 16, 2009 at 11:20PM

    Pingback from  Tech News, Resources from Blogosphere - 17 April 09(10) | Best Webhosting

  4. Posted by: WindowsObserver.com » Windows 7 Google Alerts for 16 April 2009 on April 17, 2009 at 3:44AM

    Pingback from  WindowsObserver.com   » Windows 7 Google Alerts for 16 April 2009

  5. Posted by: Reviewing programming models of Windows 7 Libraries on April 17, 2009 at 3:58AM

    Pingback from  Reviewing programming models of Windows 7 Libraries

  6. Posted by: Peters Blog on April 17, 2009 at 4:51AM

    For those of you who attended the last WMUG meeting, you'll remember that Gordon gave a very good

  7. Posted by: Dew Drop - April 17, 2009 | Alvin Ashcraft's Morning Dew on April 17, 2009 at 7:27AM

    Pingback from  Dew Drop - April 17, 2009 | Alvin Ashcraft's Morning Dew

  8. Posted by: Light Up with Windows 7 Libraries - Windows 7 for Developers - The … | Current Technology Updates daily on April 17, 2009 at 10:30AM

    Pingback from  Light Up with Windows 7 Libraries - Windows 7 for Developers - The … | Current Technology Updates daily

  9. Posted by: links for 2009-04-21 | Alpesh Nakar Blogs on SharePoint, Microsoft and that's IT on April 21, 2009 at 11:56PM

    Pingback from  links for 2009-04-21 | Alpesh Nakar Blogs on SharePoint, Microsoft and that's IT

  10. Posted by: RSS Weekly Digest - #3 on April 22, 2009 at 5:34AM

    Pingback from  RSS Weekly Digest - #3

  11. Posted by: Windows 7 for Developers on April 23, 2009 at 6:16PM

    This is the fifth post about Windows 7 Libraries. By now, you should be familiar with Windows 7 Libraries

  12. Posted by: Consuming the Contents of Windows 7 Libraries | Windows Seven 7 on April 23, 2009 at 7:08PM

    Pingback from  Consuming the Contents of Windows 7 Libraries | Windows Seven 7

  13. Posted by: US ISV Developer Evangelism Team on April 25, 2009 at 5:37PM

    The release candidate (RC) for Windows 7 is just around the corner. Dates for the Windows 7 RC were announced

  14. Posted by: You Get The . Info » Consuming the Contents of Windows 7 Libraries - 109th Edition on April 27, 2009 at 7:24PM

    Pingback from  You Get The . Info » Consuming the Contents of Windows 7 Libraries - 109th Edition

  15. Posted by: Walkthrough to Get Your Applications Ready for Windows 7 | Coded Style on April 30, 2009 at 4:01PM

    Pingback from  Walkthrough to Get Your Applications Ready for Windows 7 | Coded Style

  16. Posted by: Walkthrough to Get Your Applications Ready for Windows 7 | Coded Style on April 30, 2009 at 8:10PM

    Pingback from  Walkthrough to Get Your Applications Ready for Windows 7 | Coded Style

  17. Posted by: Walkthrough to Get Your Applications Ready for Windows 7 | Coded Style on May 01, 2009 at 2:26AM

    Pingback from  Walkthrough to Get Your Applications Ready for Windows 7 | Coded Style

  18. Posted by: Walkthrough to Get Your Applications Ready for Windows 7 | Coded Style on May 01, 2009 at 6:32AM

    Pingback from  Walkthrough to Get Your Applications Ready for Windows 7 | Coded Style

  19. Posted by: Walkthrough to Get Your Applications Ready for Windows 7 | Coded Style on May 02, 2009 at 7:33AM

    Pingback from  Walkthrough to Get Your Applications Ready for Windows 7 | Coded Style

  20. Posted by: Walkthrough to Get Your Applications Ready for Windows 7 | Coded Style on May 03, 2009 at 8:29AM

    Pingback from  Walkthrough to Get Your Applications Ready for Windows 7 | Coded Style

  21. Posted by: Windows 7 for Developers on May 09, 2009 at 2:38PM

    After two weeks break, we are back to our normal technical posts, covering Windows 7 Libraries. This

  22. Posted by: Windows 7 for Developers on May 15, 2009 at 12:30AM

    Here is another post in the Windows 7 Libraries post series. So far, we have covered what Windows 7 Libraries

  23. Posted by: Light Up with bWindows 7/b Libraries - bWindows 7/b for Developers - The b…/b « Windows 7 Live Info on May 18, 2009 at 11:44AM

    Pingback from  Light Up with bWindows 7/b Libraries - bWindows 7/b for Developers - The b…/b «  Windows 7 Live Info

  24. Posted by: You Get The . Info » Stay in Sync with Windows 7 Libraries - 193th Edition on May 19, 2009 at 8:37PM

    Pingback from  You Get The . Info » Stay in Sync with Windows 7 Libraries - 193th Edition

  25. Posted by: Windows 7 for Developers on June 09, 2009 at 4:53PM

    We are wrapping up our comprehensive drilldown into the Windows 7 Libraries API and architecture with

  26. Posted by: Windows 7 Libraries ??? Managed Code | Windows Seven 7 on June 09, 2009 at 5:56PM

    Pingback from  Windows 7 Libraries ??? Managed Code | Windows Seven 7

  27. Posted by: Windows 7 Libraries ??? Managed Code | Windows 7 Information, News, Downloads, Support Forums on June 10, 2009 at 2:24AM

    Pingback from  Windows 7 Libraries ??? Managed Code | Windows 7 Information, News, Downloads, Support Forums

  28. Posted by: Windows 7 for Developers on June 11, 2009 at 11:51AM

    Last updated: June 11, 2009 This post is an index to the recent Windows 7 Libraries series. It includes

  29. Posted by: Windows 7 Programming Guide ??? Libraries | Windows Seven 7 on June 11, 2009 at 12:23PM

    Pingback from  Windows 7 Programming Guide ??? Libraries | Windows Seven 7

  30. Posted by: Windows 7 Programming Guide ??? Libraries « Agave Anejo's Blog on June 21, 2009 at 10:59AM

    Pingback from  Windows 7 Programming Guide ??? Libraries « Agave Anejo's Blog

  31. Posted by: Matt Palmer's Blog on June 25, 2009 at 8:48AM

    Windows 7 will be available in stores October 22 - is your app ready? Here's a quick checklist to

  32. Posted by: Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 2 | Windows Seven 7 on June 25, 2009 at 9:06PM

    Pingback from  Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 2 | Windows Seven 7

  33. Posted by: Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 2 | Windows 7 Information, News, Downloads, Support Forums on June 26, 2009 at 10:29AM

    Pingback from  Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 2 | Windows 7 Information, News, Downloads, Support Forums

  34. Posted by: Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 2 | Everything Microsoft on July 02, 2009 at 1:19AM

    Pingback from  Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 2 | Everything Microsoft

  35. Posted by: Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 2 | Tom Richardson on August 09, 2009 at 4:50PM

    Pingback from  Developing for the Windows 7 Taskbar ??? Jump into Jump Lists ??? Part 2 | Tom Richardson