WSS Practice: Upload image to a Pictures Library

I was trying to upload images to a Pictures Library, and getting thumbnailing working, and so on. I found that thumbnailing wasn’t working, so I kept trying to create and upload my own thumbnail, which was pretty dumb. SharePoint will do that for you – but you’ve got to upload your file with a correct extension for an image format…

The code that worked was:

SPPictureLibrary pics = (SPPictureLibrary)site.Lists["Pictures"];
FileStream fs = new FileStream(@"c:lolrus.jpg",FileMode.Open,FileAccess.Read);
pics.RootFolder.Files.Add("lolrus.jpg", fs);

Previously I’d been trying this for the final line:

pics.RootFolder.Files.Add("lolrus", fs);

The thumbnails weren’t being generated because I failed to have the .jpg extension. I guess I figured I was defining the Display Name of the item, rather than the file name. I added in the ‘.jpg’ and when I uploaded my image, thumbnails were automatically generated. Nice.

Advertisement
WSS Practice: Upload image to a Pictures Library

2 thoughts on “WSS Practice: Upload image to a Pictures Library

  1. VenkatMoss says:

    Good concept in article….

    Nice to go through…if possible elaborate so that new to MOSS people can understand easily…
    Thanks

  2. Not really a lot more to say. Create yourselve an SPWeb object, then get your Picture library as above (“site” is an SPWeb object).

    This article describes getting an SPWeb object:
    http://www.novolocus.com/2008/11/14/how-to-get-an-spweb-object-from-a-url/

    You’ll need to reference and use the Microsoft.SharePoint assembly – so this code will only run on the server 😦 If you need it to work remotely, well, you’re looking at having to use web services then.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.