Saturday, March 19, 2011

How to add images to picture library with thumbnails using CAML

Adding images to a SharePoint picture library is done basically like adding any other type of file to a library in SharePoint: You create a module, point it towards the library, and includes the files. Well, there is a catch...

Imagine you have a feature that creates a picture library. The same feature also contains a folder ('Files') with image files to add. Then you would write something like this in your elements.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module
    Name="Images"
    Url="MyPictures"
    Path="Files">
    <File 
      Url="MyImage.png" 
      Path="MyImage.png" 
      Type="GhostableInLibrary"/>
  </Module>
</Elements>

Well and done: Your images are added to the picture library. But, wait, where did my thumbnails go?

Adding thumbnails through CAML

SharePoint displays two different thumbnails for each image:

  • On the picture library 'All items' > 'Thumbnails' view, which is the default view for the picture library. This is the 'w' thumbnail.
  • On the picture display form. This is the 't' thumbnail.

To deploy an image with thumbnails you must also supply the two thumbnails mentioned above. What you do is this:

  1. Start by deploying your image to some test site.
  2. Go to the picture library, locate your image, then check it out, and check it in.
  3. SharePoint will now have created the thumbnails for you. Download a copy of the two thumbnails (right-click the thumbnail images and save as...). Keep the names SharePoint created.
  4. Now include these two images in your feature, by adding them to subfolders called '_t' and '_w'. These folders will be hidden by SharePoint, so the user won't see them.
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module
    Name="Images"
    Url="MyImages"
    Path="Images">
    <File
      Url="MyImage.jpg"
      Path="MyImage.jpg"
      Type="GhostableInLibrary"/>
    <File
      Url="_t/MyImage_jpg.jpg"
      Path="_t\MyImage_jpg.jpg"
      Type="GhostableInLibrary"/>
    <File
      Url="_w/MyImage_jpg.jpg"
      Path="_w\MyImage_jpg.jpg"
      Type="GhostableInLibrary"/>
  </Module>
</Elements>

This approach will deploy the image and display thumbnails right away. However, it does not work on SharePoint 2010 for the w thumbnail.

Adding thumbnails through code

If the above seems too cumbersome, or you just have a large amount of pictures, then you could force SharePoint to create the thumbnails, by effectively doing what you do manually above: Checking the image out and in. This could be done in a feature receiver.

First the CAML:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module
    Name="Images"
    Url="MyImages"
    Path="Images">
    <File
      Url="MyImage.jpg"
      Path="MyImage.jpg"
      Type="GhostableInLibrary"/>
  </Module>
</Elements>

Then the feature receiver:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
  var web = (SPWeb)properties.Feature.Parent;
  var image = web.GetFile("MyImages/MyImage.jpg");
  image.CheckOut();
  image.CheckIn("Thumbnails created", SPCheckinType.OverwriteCheckIn);
}

The feature receiver approach is in my opinion the 'correct' way to do it, since we are asking SharePoint to do whatever image processing it wants. By deploying the thumbnails using CAML (the first approach) you could say that we are 'tricking' SharePoint into displaying the thumbnails - there may be a registration of some kind we are missing, which could explain why it is not working in SharePoint 2010...

No comments:

Post a Comment