How to add a set to a collection?

Topics: Developer Forum
Mar 12, 2011 at 7:02 PM

Hi,

I was wondering if there is a way to add a photo set to a collection.

The information I have is the photo set ID as well as the collection ID (i.e., both already exist on Flickr and belong to the same user).

Many thanks,
Dominik 

Coordinator
Mar 13, 2011 at 9:00 AM
There is no Flickr API method for creating and modifying collections.

Please see http://www.flickr.com/services/api for the full list of
Flickr methods.

Sam
Mar 13, 2011 at 2:34 PM

Thanks for the quick, reply, Sam.

I had hoped to just have missed the feature in the API specifications.

I had a look at the XHR data being sent when adding a set to collection using Flickr's own "Organize & Create" and created a patch (ID: 8729) which allows to add and remove photosets from collections. I don't know if it is complete or worth integrating as method signatures might change without notice.

/// <summary>
/// Edits sets in a collection. To add a set make sure that all current set IDs
/// in the collections are listed along with the set ID to add.
/// </summary>
/// <param name="collectionId">The ID of the collection to edit</param>
/// <param name="photosetIds">The IDs of photosets to be part of the collection</param>
public void CollectionsEditSets(string collectionId, IList<string> photosetIds)
{
    // construct comma separated list of photoset IDs
    string photosetIdsParameter = "";
    for (int i = 0; i < photosetIds.Count; i++)
    {
        photosetIdsParameter += photosetIds[i];
        if (i < photosetIds.Count - 1)
            photosetIdsParameter += ",";
    }

    Dictionary<string, string> parameters = new Dictionary<string, string>();
    parameters.Add("method", "flickr.collections.editSets");
    parameters.Add("collection_id", collectionId);
    parameters.Add("photoset_ids", photosetIdsParameter);
    // not sure what this parameter is for
    parameters.Add("do_remove", "0");
            
    GetResponseNoCache<NoResponse>(parameters);
}

/// <summary>
/// Removes a set from a collection.
/// </summary>
/// <param name="collectionId">The ID of the collection to edit</param>
/// <param name="photosetId">The ID of the photoset to be removed</param>
public void CollectionsRemoveSet(string collectionId, string photosetId)
{
    Dictionary<string, string> parameters = new Dictionary<string, string>();

    parameters.Add("method", "flickr.collections.removeSet");
    parameters.Add("collection_id", collectionId);
    parameters.Add("photoset_id", photosetId);

    GetResponseNoCache<NoResponse>(parameters);
}
Using this, a photoset could be added as follows:

private void AddPhotosetToCollection(string collectionId, string photosetId)
{
    CollectionCollection collection = _flickr.CollectionsGetTree(collectionId, _userId);

    List<string> photosetIds = new List<string>();
    foreach (CollectionSet _set in collection[0].Sets)
    {
        photosetIds.Add(_set.SetId);
    }
    photosetIds.Add(photosetId);

    _flickr.CollectionsEditSets(collectionId, photosetIds);
}

Thanks,
Dominik 

Coordinator
Mar 13, 2011 at 3:32 PM

Have you tested this? Does it work?

There are some methods that the Flickr web site uses that only work for Flickr, and not for other developers.

Sam

Mar 13, 2011 at 5:29 PM

It worked in the test I did: I successfully added photosets to a collection from my application.

Dominik