Currently I'm developing a webpart using SP2010 named ProfilePicture. I've successfully imported the webpart to SP2007. My problem is I receive an error whenever I attempt to upload a picture. I can display but cannot upload(replace). When I debug in my local 2010 environment no error occurs; everything running ok but in 2007 error:

The remote server returned an error: (401)Unauthorized.

The file is copied to the ~temp file but fails to copy to sharepoint document library.

What should I do? totally lost.

in local (80/Temp) ---> http://servername/ProfilePicture/name.jpg ---> no error

in other server(2007) -- > (80/Temp) --> http://servername/ProfilePicture/name.jpg --> unauthorized

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim MemberID As String = Request.QueryString("MemberID")
    Image1.ImageUrl = "http://servername/ProfilePicture/" & RTrim(MemberID) & ".jpg"
End Sub

Protected Sub UploadButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles UploadButton.Click
    Dim uploadedFilePath As String = Server.MapPath("~/Temp/")
    Dim sharePointListPath As String = "http://servername/ProfilePicture/"

    If FileUpload1.HasFile Then
            Dim fileName As String = FileUpload1.FileName.Replace(FileUpload1… Request.QueryString("MemberID")) & ".jpg"

            FileUpload1.SaveAs(uploadedFilePath + fileName)

            UploadFileToSharePoint(uploadedFilePath + fileName, sharePointListPath + fileName)

            Label1.Text = "File name: " & _
            FileUpload1.PostedFile.FileName & "<br>" & FileUpload1.PostedFile.ContentLength & " bytes<br>" & _
                "Content type: " & FileUpload1.PostedFile.ContentType & "<br>" & _
                "Save To: " & sharePointListPath + fileName & " From: " & uploadedFilePath + fileName

        Catch ex As Exception
            Label1.Text = "ERROR: " & ex.Message.ToString() & "sharePointListPath: " & (sharePointListPath)

        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 = WebRequest.Create(SharePointPath)
        request.Credentials = New System.Net.NetworkCredential("user", "password", "domain")
        request.PreAuthenticate = True

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

        Dim buffer() As Byte = New Byte(1023) {}

        Using stream As IO.Stream = request.GetRequestStream()
            Using fsWorkbook As FileStream = File.Open(UploadedFilePath, FileMode.Open, FileAccess.Read)
                Dim i As Integer = fsWorkbook.Read(buffer, 0, buffer.Length)

                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
        Label2.Text = "ERROR2: " & ex.Message.ToString()
    End Try
End Sub
  • Did you ever find an answer for this that DOES use an HTTP request and not the SP api? I have to do this from an application that resides outside the SP runtime... Commented May 2, 2012 at 15:43

3 Answers 3


Use the SharePoint API to drop the binary data of the uploaded file into the Document Library instead of messing around with WebRequests when the file is already on the server.


As Jaap mentioned above you should use Sharepoint APIs to upload directly instead of moving to a temp location and copying it over.

To add to the above you should use privileges of existing logged-in user instead of coding username and password in the page. If current user is not important you can run it with elevated privileges. For a reference on how to use existing privileges or running with elevated privileges refer to the following




Not sure if it matters at this point but I drove myself nuts on this problem today and came to find out it was because the code I had executing to do this upload was running on the same server. IIS's security measures kicked in and created a 401.2 (server config error) that I only found once I dug into the IIS logs. Either way, Method 1 here:



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.