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:
<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:
- Start by deploying your image to some test site.
- Go to the picture library, locate your image, then check it out, and check it in.
- 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.
- 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.
<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:
<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:
{
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