Freigeben über


Windows 7: Create Flickr Federated Search Connector with .Net

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:

https://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="https://search.yahoo.com/mrss/">

  <channel>

    <title>Flickr Search Results</title>

    <item>

      <title>IMG_0134c</title>

      <link>https://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="https://farm4.static.flickr.com/3100/2682983091_7cea404dc4_b.jpg"
                     type="image/jpeg" height="333" width="500" />

      <media:thumbnail url="https://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="https://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="https://a9.com/-/spec/opensearch/1.1/">

  <ShortName>Flickr Federated Search</ShortName>

  <Url type="application/rss+xml"
       template="https://.../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!

Comments

  • Anonymous
    January 02, 2010
    Your blog posts are extremely well written, love it completely.