Mehfuz's WebLog

Live crazy, think different!



Passionate about cutting edge technologies and facinated by the modern web and phone revolution.Currently working at Telerik Corporation, the leading .net component vendor.
Follow me



New LINQ provider for Flickr

LINQ provider for Flickr gives  an easy way to query , add and delete Flickr photos. Here, I will give some sneak preview about how to use the provider to do different operations on Flickr, as if doing queries with SQL objects.

Now, waiting no more, lets see, what can we do with it.

Now, Let's say, i want to get a particular user's ("chschulz") photos with "New york" as search key  and I also want only the first page, where each page should have 5 items.

The code snippet for that will be :

// create the context
FlickrContext context = new FlickrContext();
// set the user.
string user = "chschulz";
// do query.
var query = (from ph in context.Photos
             where ph.User == user && ph.SearchText == "New York" && ph.PhotoSize == PhotoSize.Thumbnail
             select ph).Take(5).Skip(0);


    foreach (Photo p in query)
        Console.WriteLine(p.Title + "\r\n" + p.Url);

catch (Exception ex)


50th Steuben Parade
50th Steuben Parade
50th Steuben Parade
50th Steuben Parade
50th Steuben Parade

One thing to add,  by default when we will query without any private/semi-private view mode, result will contain only public photos.

To have Semi-private /private photos, We can set option by ViewMode enum

public enum ViewMode
    Public = 1,

Note, There is a value called ViewMode.Owner, when specified , it means "get all my photos". This will get all private/semi-private photos of an authenticated account.Also, ViewMode.Private and ViewMode.Owner, will get the private photos only and all photos of a specified user respectively.

When we  will try to view or modify private/semi-private photos, The authentication process will done by API on behalf. The API will automatically , open a new browser(for windows app) / take to the URL, where the user will be prompted for valid credentials and access permission for the app, for which the photos to be viewed/deleted/added. Finally, the user will be taken back to the app, after user closes browser (windows app) / automatically (web app, we have to specify the URL of our app, in the return URL of API configuration page of Flickr).

There is also an enum for specifying Photosize

public enum PhotoSize
    Default = Medium (this is set by default)

In addition, the most important options for searching are:

  • Photo.SearchText : Used for specifying keyword for which Title, Description or tags will be search on.
  • Photo.User : The name of the user , of whom the photos will be queried on.

Moving forward, LINQ query for viewing single phtoto for an ID.

FlickrContext context = new FlickrContext();

var query = from ph in context.Photos
            where ph.Id == PhotoId && ph.PhotoSize == PhotoSize.Medium
            select ph;

Photo photo = query.Single<Photo>();

Next , query syntax for deleting a photo.

FlickrContext context = new FlickrContext();
var query = from ph in context.Photos
             where ph.Id == PhotoId
             select ph;

Photo photo = query.Single<Photo>();


Any  remove or add operation must follow a FlickrContext.SubmitChanges(). This is where, the action will be posted to Flickr.

Finally, API is never done without a photo add operation. Snippet for that :-

FlickrContext context = new FlickrContext();
new Photo{ FileName = Path.GetFileName(uploader.Value),
File = uploader.PostedFile.InputStream, ViewMode = ViewMode.Private});

Here, we have to either set the Photo.File(Stream) for uploaded file (in web app), or we can only specify  Phtoto.FilePath (windows app).

*The above , three examples are taken from Flickr.Web (test) app , which is provided with the LINQ.Flickr API at codeplex.

This is the short briefing of LINQ To Flickr API , which I have recently uploaded in codeplex, please give a look at it and let me know , how can I improve it more.

The API can be downloaded at : LINQ to Flickr API


kick it on
Posted: Oct 28 2007, 11:53 PM by mehfuzh | with 22 comment(s) |
Filed under: ,


Kazi Manzur Rashid said:

Mehfuz it is really kool thing.

# October 28, 2007 8:51 PM

mehfuzh said:

Thanks, Rashid and Omer for the feedback!


# October 29, 2007 10:41 AM

mehfuzh said:

When you set the value = true, it shows your pictures both private and public from your account and for that you must need to grant the app. In other cases (value=false, public photos), it is not required by flickr.

I am upgrading the Demo_project, so stay tuned.

# February 10, 2008 1:55 AM

Aggpup said:

Does it get around the appallingly slow commenting rate that flickr's API usually allows?

# July 22, 2009 9:04 AM

mehfuzh said:

What you mean by slow, actually, it's an implementation over REST provided by flickr. So, anything that goes with flickr goes with this API as well. Actually, i never found commenting slow in flickr, may be a bandwidth issue.

# July 29, 2009 7:16 AM

rahul mohan said:

hi Mehfuz

i want to get the friend list(after that need to get the friends pictures) using this api ,how can i get that ...could u please make a comment on this

# November 24, 2009 6:53 AM

mehfuzh said:

Not supported yet, but you can open up a task at the project page, so that i can add it for you . Or you can contribute as well , all i need is your codeplex id.

# November 25, 2009 12:41 AM