21 October 2009

Programmatically uploading an image to a SharePoint Picture Library

SharePoint’s ability to store and maintain libraries of resources (such as images, documents and other files) is a very useful weapon in a SharePoint developer’s arsenal. In this article I will show how you can leverage the power of a Picture Library to store uploaded content for use across your Site Collection.

It’s fairly simple to navigate to the user interface for a picture library and upload a new file. It is almost as simple as clicking ‘Upload Picture’ from the Upload menu. However you will additionally be encouraged to add meta-data to your picture in order to better catalogue it.

image_thumb.png

However when you are writing a custom SharePoint solution like a web part or an application page you may not want the user to have to navigate away from your solution in order to upload the image into the requisite picture library. Luckily the SharePoint object model combined with some fairly simple ASP.Net code can simplify this task for you.

The basic concept is simple you take a standard ASP.Net FileUpload control and add this in to your solution. This control provides the basic user interface needed to upload a file and it has some very useful methods for working with the file once it has been uploaded.

image_thumb_3.png

In addition to this control you will need some ASP.Net code in order to retrieve the contents of this file and some simple Windows SharePoint Services (WSS) 3.0 code to place this picture in the requisite picture library.

You will need to access a Stream of the data from the file uploaded in the FileUpload control. This can then be used to create a new document in the Picture Library. The FileUpload control has a handy property called PostedFile which will provide access to the file which is being uploaded and in turn this has a property called InputStream which contains the Stream of data from the file. Some simple code to achieve this would be:

Dim streamImage As Stream = Nothing
If fleInput.HasFile Then
    streamImage = fleInput.PostedFile.InputStream
End If

The HasFile property of the FileUpload control is fairly self explanatory as it merely indicates if a file has been selected in the control.

The SPPictureLibrary class in WSS 3.0 is dedicated to managing the resources held within a picture library; essentially this is identical to the SPDocumentLibrary class with the addition of 3 properties (ThumbnailSize, WebImageHeight and WebImageWidth) which control how the images display within this picture library. Although this class exists there is no need to use it unless you are interested in dealing with one of those 3 properties. In fact your code only requires the RootFolder property on the SPList class.

The RootFolder property is an instance of the SPFolder class and this provides access to the SPFileCollection which represents the files in this library. So uploading a new file is a case of adding a new SPFile instance into this collection. The code for this would be as shown below:

Using Site As New SPSite(“SITECOLLECTIONURL”)
    Using Web As SPWeb = Site.OpenWeb(“SITEURL”)
        Dim pics As SPList = Web.Lists(“PICTURELIBRARYNAME”)
        pics.RootFolder.Files.Add( _
            fleInput.FileName, streamImage)
    End Using
End Using

Essentially you are adding the Stream as a new file to your picture library. In actual fact because this code is operating at such a low level you can use it to upload any file into a document library within SharePoint.

image_thumb_4.png

This article has shown how you can use a simple FileUpload control and the power of WSS 3.0 to simplify the uploading of files into Picture Libraries and other SharePoint document libraries.

Keep up to date with Simple-Talk

For more articles like this delivered fortnightly, sign up to the Simple-Talk newsletter

This post has been viewed 13018 times – thanks for reading.

  • Rate
    [Total: 2    Average: 1/5]
  • Share