Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
        Try
            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
    Else
        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
    Try
        ' 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)
                Loop

            End Using
        End Using

        ' Make the PUT request.
        response = request.GetResponse()

    Catch ex As Exception
        'Throw ex
        Label2.Text = "ERROR2: " & ex.Message.ToString()
    Finally
        response.Close()
    End Try
End Sub
share|improve this question
    
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... –  Matthew Bonig May 2 '12 at 15:43

3 Answers 3

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:

http://support.microsoft.com/kb/896861

worked

share|improve this answer

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

http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/4854962a-0d4e-40e4-909a-581bb7a5ab03

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx

share|improve this answer

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.

SPList.RootFolder.Files.Add();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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