Uploading Files to SharePoint Server 2007 from ASP.NET Web Applications by Using the HTTP PUT Method

SharePoint 2007

Summary:  Learn how to use the HTTP PUT method to programmatically upload files from an ASP.NET Web application to a Microsoft Office SharePoint Server 2007 site. (6 printed pages)

Office Visual How To

Applies to:  Microsoft Office SharePoint Server 2007, Windows SharePoint Services 3.0, Microsoft ASP.NET, Microsoft Visual Studio 2008

Joel Krist, iSoftStone

June 2009


A common scenario involves moving files from a Microsoft ASP.NET Web application to a Microsoft Office SharePoint Server 2007 site. Office SharePoint Server provides the Copy Web service for copying files between SharePoint sites or within a SharePoint site, and the Imaging Web service for uploading files to SharePoint image libraries. However, neither Web service supports generic file uploading to SharePoint Server. This Office Visual How-To shows you how to programmatically upload files to a predefined SharePoint list by using the HTTP PUT method.

See It

Uploading Files to SharePoint Server 2007

Watch the Video

Length: 03:56 | Size: 4.87 MB | Type: WMV file

Code It | Read It | Explore It

Code It

Download the code sample that accompanies this article: Upload Files to SharePoint Server Using HTTP PUT

To demonstrate how to programmatically upload files to Office SharePoint Server 2007 from Microsoft ASP.NET applications, this section walks through the following four steps:

  1. Creating an ASP.NET Web application in Microsoft Visual Studio 2008

  2. Importing the System.IO namespace and the System.Net namespace

  3. Creating the file upload form

  4. Adding the upload code to the solution

Creating an ASP.NET Web Application in Visual Studio 2008

You can create an ASP.NET Web application by using Visual Studio 2008.

To create an ASP.NET Web application in Visual Studio 2008

  1. Start Visual Studio 2008.

  2. On the File menu, point to New, and then click Project.

  3. In the New Project dialog box, in the Project types pane, select Visual Basic Windows or Visual C# Web type.

  4. In the Templates pane, select ASP.NET Web Application.

  5. Name the project SharePointUpload and select the Create directory for solution check box.

    Figure 1. Creating the solution

    Creating the solution
  6. Click OK.

Importing the System.IO and System.Net Namespaces

Importing the System.IO namespace and the System.Net namespace enables the sample code to work with the objects and types defined in these namespaces without needing to specify the fully qualified namespace path each time. Open the Source view of the solution's Default.aspx page and add the following @ Import directives to the top of the page, just below the @ Page directive that is created by Visual Studio.

<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>

Creating the File Upload Form

For this demonstration, we use an ASP.NET Web application that consists of a single Web form that makes use of the ASP.NET FileUpload control and the Button control.

Figure 2. The ASP.NET Web form

The ASP.NET Web form

To create the Web form shown in Figure 2, open the Source view of the Default.aspx Web form that was created by Visual Studio and replace the markup located between the <body> tag and the </body> tag with the following markup.

<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" Width = "500px" />
<br />
<br />
<asp:Button ID="UploadButton" runat="server" OnClick="Button1_Click"
    Text="Upload File" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>

Adding the Upload Code to the Solution

To add the code that performs the file upload, open the Source view of the Default.aspx Web form that was created by Visual Studio, and then add the following script block between the <head> tag and the </head> tag, after the </title> tag.

<script runat="server">

    Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim uploadedFilePath As String = "C:\Temp\Uploads\"
        Dim sharePointListPath As String = _
        If FileUpload1.HasFile Then
                FileUpload1.SaveAs( _
                uploadedFilePath + FileUpload1.FileName)
                Label1.Text = "File name: " & _
                     FileUpload1.PostedFile.FileName & "<br>" & _
                     FileUpload1.PostedFile.ContentLength & _
                     " bytes<br>" & _
                     "Content type: " & _

                UploadFileToSharePoint( _
                    uploadedFilePath + FileUpload1.FileName, _
                    sharePointListPath + FileUpload1.FileName)
            Catch ex As Exception
                Label1.Text = "ERROR: " & ex.Message.ToString()
            End Try
            Label1.Text = "You have not specified a file."
        End If
    End Sub
    Sub UploadFileToSharePoint(ByVal UploadedFilePath As String, _
                ByVal SharePointPath As String)

        Dim response As WebResponse = Nothing

            ' Create a PUT Web request to upload the file.
            Dim request As WebRequest = _

            request.Credentials = CredentialCache.DefaultCredentials
            request.Method = "PUT"

            ' Allocate a 1 KB buffer to transfer the file contents.
            ' You can adjust the buffer size as needed, depending on
            ' the number and size of files being uploaded.
            Dim buffer() As Byte = New Byte(1023) {}

            ' Write the contents of the local file to the
            ' request stream.
            Using stream As Stream = request.GetRequestStream()
                Using fsWorkbook As FileStream = _
                    File.Open(UploadedFilePath, _
                        FileMode.Open, FileAccess.Read)
                    Dim i As Integer = fsWorkbook.Read(buffer, 0, _

                    Do While i > 0
                        stream.Write(buffer, 0, i)
                        i = fsWorkbook.Read(buffer, 0, buffer.Length)

                End Using
            End Using

            ' Make the PUT request.
            response = request.GetResponse()
        Catch ex As Exception
            Throw ex
        End Try

    End Sub


This sample code assumes that the file to upload to SharePoint Server is located in the C:\Temp\Uploads folder. To change the location of the file, modify the sample code and change the value of the uploadedFilePath variable.

The code currently has a placeholder for the SharePoint site and list to upload the file to. Modify the sharePointListPath variable to specify a real SharePoint site and list.

Read It

This article provides sample code that shows how to upload a file from an ASP.NET Web application to an OfficeSharePoint Server 2007 site. The example makes use of the ASP.NET FileUpload server control to upload a file to the Web application, and then uses the HTTP PUT method to upload the file to the SharePoint site.

Explore It

  • FileUpload Class

  • WebRequest Class

  • WebRequest.Method Property