DCSIMG
July 2009 - Posts - Guy Burstein's Blog

Guy Burstein's Blog

Developer Evangelist @ Microsoft

News

Guy Burstein The Bu

Disclaimer
Postings are provided 'As Is' with no warranties and confer no rights.

Guy Burstein LinkedIn Profile

July 2009 - Posts

Windows 7: Create .Net Flickr Federated Search Connector

Windows 7: Create .Net Flickr Federated Search Connector

Windows 7 makes searching for content much easier with Federated Search. In this post I’ll talk about how to how to create a Flickr Federated Search Providers in .Net.

Windows 7 .Net Flickr Federated Search Connector Provider C# .osdx 

Before We Start

Windows 7 .Net Flickr Federated Search Connector Provider C# .osdx This sample uses Flickr API through Flickr.Net. In order to build your own version of this Flickr Federated Search Connector:

Create a new ASP.Net Web Application

Create a new ASP.Net Web Application with Visual Studio.

Windows 7 .Net Flickr Federated Search Connector Provider C# .osdx

Windows 7 .Net Flickr Federated Search Connector Provider C# .osdxAdd the Flickr.Net project (called FlickrNet.csproj) to your solution, and add a reference to it from your web application.

Add the Flickr API and Secret Key’s to your web.config in the AppSettings section:

<appSettings>

  <add key="flickr_api_key" value="923422ab4225345dfgdb777daf46"/>

  <add key="flickr_secret" value="aa8b23fsdfhyc3beda"/>

</appSettings>

Create the Flickr Search Provider

The following provider will be executed by the Windows Explorer client using HTTP GET, and will receive several parameters from it:

http://localhost:52903/Search.aspx?q=TechEdIsrael2008&start=1&count=20

It will perform the search against Flickr, and return the response as RSS.

<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">

  <channel>

    <title>Flickr Search Results</title>

    <item>

      <title>IMG_0134c</title>

      <link>http://farm4.static.flickr.com/3100/2682983091_7cea404dc4_m.jpg</link>

      <description>show shadow dance techedisrael2008</description>

      <pubDate>Sun, 20 Jul 2008 01:47:25 GMT</pubDate>

      <media:content url="http://farm4.static.flickr.com/3100/2682983091_7cea404dc4_b.jpg"
                     type="image/jpeg" height="333" width="500" />

      <media:thumbnail url="http://farm4.static.flickr.com/3100/2_7cea404dc4_t.jpg" />

    </item>

    <item>

      ...

    </item>

  </channel>

</rss>

Create a new Web Form called Search.aspx.

To make it easier to test the response, right click the web application, and open the Properties window. Go to the Web pane, select Search.aspx as the start page and provide the query parameters:

Windows 7 .Net Flickr Federated Search Connector Provider C# .osdx

In the page markup, add a new asp:Repeater control that will define the template for each item in the response:

<asp:Repeater runat="server" ID="itemsRepeater">

</asp:Repeater>

In the page code behind, implement the logic that uses Flickr.Net to search for photos and bind the result to the repeater control:

protected void Page_Load(object sender, EventArgs e)

{

  string flickrApiKey = WebConfigurationManager.AppSettings["flickr_api_key"];

  string flickrSecret = WebConfigurationManager.AppSettings["flickr_secret"];

 

  string searchTerm = Request.QueryString["q"];

  int startPage = int.Parse(Request.QueryString["start"]);

  int count = int.Parse(Request.QueryString["count"]);

 

  Flickr flickr = new Flickr();

  flickr.ApiKey = flickrApiKey;

  flickr.ApiSecret = flickrSecret;

  Photos photos = flickr.PhotosSearch(null, "", 0, searchTerm,
                      DateTime.MinValue, DateTime.MinValue, 0,
                      count, startPage, PhotoSearchExtras.All);

 

  this.itemsRepeater.DataSource = photos.PhotoCollection;

  this.itemsRepeater.DataBind();

}

In the code above I first grab the API and secret key’s from the configuration file and then, I extract the parameters from the query string. I create a new instance of the Flickr object, and perform the actual search for photos, providing the page number and the number of items in each page. Finally, I bind the returned photo collection the the repeater control.

Replace the html header and footer with RSS header and footer in Search.aspx.

Before:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Search.aspx.cs" Inherits="FlickrFederatedSearch.Search" %>

 

<head runat="server">

  <title></title>

</head>

<body>

  <form id="form1" runat="server">

  <div>

    <asp:Repeater runat="server" ID="itemsRepeater">

    </asp:Repeater>

  </div>

  </form>

</body>

</html>

After:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Search.aspx.cs" Inherits="FlickrFederatedSearch.Search" %>

 

<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">

  <channel>

    <title>Flickr Search Results</title>

    <asp:Repeater runat="server" ID="itemsRepeater">

    </asp:Repeater>

  </channel>

</rss>

Set the ItemTemplate of the Repeater control to display the photos data according to the output format:

<asp:Repeater runat="server" ID="itemsRepeater">

  <ItemTemplate>

    <item>

      <title><%# Eval("Title") %></title>

      <link><%# Eval("SmallUrl")%></link>

      <description><%# Eval("CleanTags")%></description>

      <pubDate>

        <asp:Literal runat="server"

         text='<%# ((FlickrNet.Photo)Container.DataItem).DateTaken.ToString("R") %>' />

      </pubDate>

      <media:content url='<%# Eval("LargeUrl")%>' type="image/jpeg" />

      <media:thumbnail url='<%# Eval("ThumbnailUrl")%>' />

    </item>

  </ItemTemplate>

</asp:Repeater>

Run the application. The browser should navigate to Search.aspx passing all the relevant query parameters. The response should be an RSS feed that contains items per photos found.

Creating the Flickr Federated Search Connector

Add a new Xml file to the project and name it FlickrFederatedSearch.osdx (stands for OpenSearch Description Xml). This file contains the data that Windows Explorer needs to order to perform the search against the search service.

<?xml version="1.0" encoding="utf-8" ?>

<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">

  <ShortName>Flickr Federated Search</ShortName>

  <Url type="application/rss+xml"
      
template="http://.../Search.aspx?q={searchTerms}&amp;start={startPage}
&amp;count={count}" />

</OpenSearchDescription>

Notice in the above xml that the parameters are taken from some predefined names ({searchTerms}, {count} etc.), and the ampersands (&) are Html encoded.

Right click the .osdx file, and select Open With.

Windows 7 .Net Flickr Federated Search Connector Provider C# .osdx

 
In the Open With dialog, select Windows Explorer.

Windows 7 .Net Flickr Federated Search Connector Provider C# .osdx

In the Add Search Connector dialog, click Add.

image

Now, Flickr Federated Search appears in the Favorites section in Windows 7 Explorer and you can use it for searching items.

 Windows 7 .Net Flickr Federated Search Connector Provider C# .osdx

Summary

In this post I’ve talked about how to create a Flickr Federated Search Connector for Windows 7 in .Net. The above provider provides basic features and can be enhanced with more capabilities described in the Windows 7 Federated Search Provider Implementer’s Guide.

Enjoy!

Windows 7: Register a New File Associations

Windows 7: Register a New File Associations

Windows 7: Register a New File Associations

Windows 7: Register a New File Associations Lets say I have a standard Windows Forms application that works with .guy file types (which is nothing but a text file):

This application can be launched from the command line with a file name as an argument:

> TextFilesViewer.exe SampleFile.guy

In this case, the application displays the file contents in the multiline textbox above:

private void ViewerForm_Load(object sender, EventArgs e)

{

  string[] args = Environment.GetCommandLineArgs();

  if (args.Length > 1)

  {

    string[] lines = File.ReadAllLines(args[1]);

    this.txtFileContents.Lines = lines;

  }

}

Since this application is the only application that knows how to work with .guy files, we would like to associate this file type with my application.

Windows 7: Register a New File Associations

In order to do that, add a reference to the RegistrationHelper sample. This is an exe that performs the actual registration of the file association and needs to be run with admin privileges. It can be found as a sample project in the WindowsAPICodePack\Samples\Shell\TaskbarDemo\CS\RegistrationHelper folderer

After you have added the reference you should add another file from the samples folder - RegistrationHelper.cs which is found in the WindowsAPICodePack\Samples\Shell\TaskbarDemo\CS\TaskbarDemo\ folder.
This file exposes several static methods that invoke the helper as another process with admin privileges.

private void registerFileTypeToolStripMenuItem_Click(object sender, EventArgs e)

{

  string appId = "TextFilesViewer";

 

  RegistrationHelper.RegisterFileAssociations(

    appId,

    false,

    appId,

    string.Format("{0} %1", Assembly.GetExecutingAssembly().Location),

    ".guy");

}

Notice that one of the parameters of RegistrationHelper.RegisterFileAssociations() method is the Application ID which is set to a meaningful name. I’ll talk more on the Application ID in later posts to understand its impact on additional features.

Windows 7: Register a New File AssociationsWhen the user tries to associate the file type, RegistratioinHelper will require admin privileges. If User Account Control (UAC) is enabled on your machine, you will be prompted to allow the registration utility to modify the registry.

After the registration completes, you can double click and .guy files in your machine, and the TextFilesViwer application will be launched to display its content.

Enjoy!

Getting Started with Windows 7 Development Through Windows API Code Pack

Getting Started with Windows 7 Development Through Windows API Code Pack

Windows API Code PackWindows API Code Pack 

Windows 7 is said to be a great operating system, much more secure, faster and productive than its predecessors. As with every version of Windows, it also contains a huge amount of new features exposed for developers as API’s, unfortunately, as unmanaged code. The Windows SDK team is working on a project called Windows API Code Pack which provides managed wrappers for Windows API’s.

Downloading the Windows API Code Pack, you’ll get a Zip file containing both the Windows API Code Pack Source and Samples. In the next few posts I’ll explore the new features of the new OS and provide the managed ways to leverage them from your applications using the above source and samples.

Enjoy!