1

I am making a custom web part and need to be able to pull a picture thumbnail from a picture library. I am using SPMetal generated classes and don't see a way to do this.

It seems that thumbnails are put into a folder /_t and are changed to .jpg files with their extension appended with _ext.jpg but I was hoping that this value would exist somewhere.

So my question is how do I get the thumbnail source for a picture in a library? Prefer to use SPMetal classes (Even if I have to extend the class it is ok).

2 Answers 2

2

To fix this issue I had to extend the classes generated by SPMetal. http://zimmergren.net/technical/extending-the-linq-to-sharepoint-context-to-allow-additional-fields-and-properties-in-your-queries is a pretty good resource for this except for the fact that I had to remove the namespace to get it to work (I'm not entirely sure why though. Can only assume it is because the generated classes have no namespace).

The MapFrom method is what caused me problems. Essentially it seemed like the the listitem being passed to MapFrom doesn't have all the fields loaded and I got an exception stating the value is not withing the expected range. The key to solving this was to add the CustomMapping attribute to get it to load the fields that I need. To solve the problem I used the following code

    public string ThumbnailUrl { get; set; }

    public string PictureUrl { get; set; }
    [CustomMapping(Columns = new[] { "EncodedAbsThumbnailUrl", "EncodedAbsUrl" })]
    public void MapFrom(object listItem)
    {
        var item = listItem as SPListItem;
        if (item != null)
        {
            ThumbnailUrl = item["EncodedAbsThumbnailUrl"].ToString();
            PictureUrl = item["EncodedAbsUrl"].ToString();
        }
    }
1

The thumbnail is also available via a particular field AlternateThumbnailUrl its internal name. Try adding that to your SPMetal as being an out-of-the-box field it should work.

3
  • Ok I tried that and that field appears to have the value I need I cannot extend the generated class to get it to accept this value. I keep getting an exception "Value does not fall within the expected range.". This field is valid and I have tried it with a few different fields including ThumbnailExists. It seems to work when I try to lookup "Editor" but it just seems like it isn't passing the full object.
    – Josh
    Commented Sep 5, 2012 at 19:01
  • 1
    I forgot to add the following to the MapFrom. [CustomMapping(Columns = new[] { "EncodedAbsThumbnailUrl", "EncodedAbsUrl" })]
    – Josh
    Commented Sep 5, 2012 at 19:26
  • if that fixed your problem let us know so we could share that answer, thank you! Commented Sep 6, 2012 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.