使用VB.NET将文档上载到SharePoint

时间:2015-09-30 15:13:30

标签: sql asp.net sql-server vb.net sharepoint

在我的公司,我们有一项计划,允许用户提交“获奖创意”。有一个表单可以填写,用户可以在表单上上传文档。这些想法由其他员工投票,由委员会审核,用户有能力赢得现金奖励,并在公司实施他们的想法。

我使用ASP.NET创建了一个自定义webpart,允许用户在SQL表中插入一行,并将文档添加到SharePoint文档列表中。他们上传的文档列表将添加到SQL表中的字段中,并在.aspx页面上显示为超链接。

以下是我在文档上传时遇到的一些问题:   - 文件根本没有上传   - 文档已上传,但未签入。
  - 文档已上载并签入,但未链接到记录(该字段未在SQL中更新)   - 如果添加多个文档,则会上载两个文档,但会对所有文档重复第一个文档的内容   - 使用Insert语句创建多个条目。

我无法弄清楚这是我的SQL还是我的VB的问题。我有更好的方法吗?任何帮助将不胜感激。

我目前的环境是这样的:
  - SharePoint 2013
  - SQL Server 2012
  - Microsoft Visual Studio Professional 2013

Partial Public Class WinningIdeasFormUserControl
Inherits UserControl
Public conn As SqlConnection
Public strDatabase As String
Public MyUserInfo As SPUser
Public strFiles As String
Public IdeaId As String
Public uploads As HttpFileCollection

Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    'reference to file collection that was sent by the browser request'
    uploads = HttpContext.Current.Request.Files 

    Try
        conn = New SqlConnection(strDatabase)
        Dim cmd As SqlCommand = New SqlCommand()
        strFiles = ""

        'SQL command to insert results into the database'
        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "INSERT INTO [WinningIdeas] (Title, Idea, Submitter, SPUserID) OUTPUT INSERTED.ID VALUES (@Title, @Idea, @Submitter, '" & MyUserInfo.ID & "')"

        cmd.Parameters.AddWithValue("@Title", txtTitle.Text)
        cmd.Parameters.AddWithValue("@Idea", txtIdea.InnerText)
        cmd.Parameters.AddWithValue("@Submitter", MyUserInfo.Name)

        'confirms that the database has been updated'
        cmd.Connection.Open()
        Dim reader As SqlDataReader = cmd.ExecuteReader()
        reader.Read()
        'set IdeaId to the output of the record that was just added (ID it was assigned in database)'
        IdeaId = reader(0).ToString
        cmd.Connection.Close()
        cmd.Connection = Nothing

        If uploads.Count > 1 Then
            Try
                'if the user uploaded documents, make sure we add them'
                uploadDocuments()
            Catch ex As Exception
                Span1.InnerHtml = "An error has occurred while trying to upload your document(s). Please contact WinnDESK@winnco.com for more information. " & ex.Message & "."
            End Try
        ElseIf (reader.RecordsAffected = 1) Then 'if a record has been successfully created'
            SendEmail()
            Response.Redirect("/Pages/Submitted.aspx?IdeaId=" & IdeaId)
        End If

    Catch ex As Exception
        'change the <span> to show the error message'
        Span1.InnerHtml = ex.Message
    End Try
End Sub

Public Sub uploadDocuments()
    'upload files'
    For i As Integer = 0 To (uploads.Count - 1)
        Dim fileSize As Int64
        fileSize = uploads(i).ContentLength
        'get the file name from whichever file we are currently saving'
        Dim filName As String = System.IO.Path.GetFileName(uploads(i).FileName)
        'TODO: Size exception is bringing the user to an exception page. catch as my exception'
        'make sure there are no empty files, make sure file is less than 50MB'
        If (fileSize > 0 And fileSize < 52428800) Then
            Dim spfile As SPFile = SPContext.Current.Web.Files.Add("/Documents/User%20Uploads/" + IdeaId + "_" + filName, FileField.PostedFile.InputStream)
            spfile.CheckIn("Checked in by " & MyUserInfo.Name & " for Idea " & IdeaId, SPCheckinType.MajorCheckIn)
            spfile.Publish("Published by " & MyUserInfo.Name & " for Idea " & IdeaId)
            'separate files with a ";" in the database'
            strFiles = strFiles + IdeaId + "_" + filName + ";"
        Else
            Throw New Exception("Your file, " & filName & ", was not uploaded correctly due to its size.")
        End If
    Next

    Try
        conn = New SqlConnection(strDatabase)
        Dim cmd As SqlCommand = New SqlCommand()

        cmd.Connection = conn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "UPDATE WinningIdeas SET Documents = @Documents WHERE ID = " & IdeaId

        cmd.Parameters.AddWithValue("@Documents", strFiles)

        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
        cmd.Connection = Nothing

        Response.Redirect("/Pages/Submitted.aspx?IdeaId=" & IdeaId)
    Catch ex As Exception
        'change the <span> to show the error message'
        Span1.InnerHtml = ex.Message
    End Try
End Sub

0 个答案:

没有答案