将文件上载到SharePoint DocLib的VBScript

时间:2010-09-24 14:09:56

标签: sharepoint file-upload vbscript

我正在尝试自动将文件上传到SharePoint文档库。我遇到了无数的帖子(在这个论坛上和其他人),但似乎无法获得有用的东西。我不是真正的开发人员,尽管我已经做了一些简单的VBA和VB脚本。

我正在寻找的解决方案是自动将文件(特别是.xlsx和.zip类型)从本地计算机上传到特定的SharePoint文档库(让我们使用“... / sharepoint / Metrics / Forms / AllItems.aspx“作为列表”使用VBA或VB脚本。

在研究这个问题时,这里有一些其他想法/评论,希望能帮助某人为我提供解决方案:

  • 我无法更改SharePoint服务器上的任何内容
  • 我需要能够在上传文件时传递凭据
  • 我只是在寻找VBA / VBS解决方案(没有C#或.NET)
  • 可能需要在上传时设置元数据

提前感谢您的帮助。

4 个答案:

答案 0 :(得分:8)

以下VBScript使用FrontPage RPC上传文件:

Function StringToByteArray(str)
   Set stream = CreateObject("ADODB.Stream")
   stream.Open
   stream.Type = 2 ''adTypeText
   stream.Charset = "ascii"
   stream.WriteText str
   stream.Position = 0
   stream.Type = 1 ''adTypeBinary
   StringToByteArray = stream.Read()
   stream.Close
End Function

Sub UploadFile(sourcePath, siteUrl, docName, title, checkincomment, userName, password)

   strHeader = "method=put+document%3a12.0.4518.1016" + _
      "&service_name=%2f" + _
      "&document=[document_name=" + Escape(docName) + _
      ";meta_info=[vti_title%3bSW%7c" + Escape(title) + "]]" + _
      "&put_option=overwrite,createdir,migrationsemantics" + _
      "&comment=" + _
      "&keep%5fchecked%5fout=false" + vbLf
   bytearray = StringToByteArray(strHeader)

   Set stream = CreateObject("ADODB.Stream")
   stream.Open
   stream.Type = 1 ''adTypeBinary
   stream.Write byteArray

   Set stream2 = CreateObject("ADODB.Stream")
   stream2.Open
   stream2.Type = 1 ''adTypeBinary
   stream2.LoadFromFile sourcePath
   stream2.CopyTo stream, -1
   stream.Position = 0

   Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
   xmlHttp.open "POST", siteUrl + "/_vti_bin/_vti_aut/author.dll", false, userName, password
   xmlhttp.setRequestHeader "Content-Type","application/x-vermeer-urlencoded"
   xmlhttp.setRequestHeader "X-Vermeer-Content-Type","application/x-vermeer-urlencoded"
   xmlhttp.setRequestHeader "User-Agent", "FrontPage"
   xmlHttp.send stream

   If xmlHttp.status = 200 Then

       If Instr(xmlHttp.responseText, "successfully") = 0 Then

          MsgBox "ERROR: " & vbCrLf & xmlHttp.responseText       

       Else

          ''Checkin

          strHeader = "method=checkin+document%3a12.0.4518.1016" + _
             "&service_name=%2f" + _
             "&document_name=" & Escape(docName) + _
             "&comment=" + Escape(checkincomment) + _
             "&keep%5fchecked%5fout=false" + vbLf

          Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
          xmlHttp.open "POST", siteUrl + "/_vti_bin/_vti_aut/author.dll", false, userName, password
          xmlhttp.setRequestHeader "Content-Type","application/x-vermeer-urlencoded"
          xmlhttp.setRequestHeader "X-Vermeer-Content-Type","application/x-vermeer-urlencoded"
          xmlhttp.setRequestHeader "User-Agent", "FrontPage"
          xmlHttp.send strHeader



       End If

   End If

   If xmlHttp.status / 100 <> 2 Then
      MsgBox "ERROR: status = " & xmlHttp.status & vbCrLf & xmlHttp.responseText
   End If

End Sub

UploadFile "C:\Users\myusername\Desktop\Test File.zip", _
    "http://computername/Sites/sitename", _
    "Requirements/Test File.zip", _
    "Test title", _
    "Test checkin comment", _
    "MYDOMAIN\myusername", "mypassword"
MsgBox "Done"

请注意,文件名应仅包含ASCII字符。否则,上述脚本将无效。

答案 1 :(得分:0)

将驱动器号映射到SharePoint文档库怎么样?然后像平常一样复制/移动文件?

http://blog.crowe.co.nz/archive/2005/08/31/244.aspx

答案 2 :(得分:0)

您最好的解决方案是使用FP RPC(这是首页远程过程调用)。这基本上是一个Web请求,您可以将元数据和文件内容作为参数传递给您。这可以通过任何语言完成,包括VBA / VBS 这是该方法的正式描述:http://msdn.microsoft.com/en-us/library/ms479623.aspx 您可以找到有关如何构建实用程序的相当多的资源和代码示例

答案 3 :(得分:0)

以下是我完成此任务的方法,现在我需要找到一种通过vbscript检入文档的方法。

function SPCopy(InFileName, InPath, oFS)

    SPURL = ""
    SPSource = InPath & "\" & InFileName

    ' determine which sharepoint path to copy the excel workbook to
    If InStr(InFileName, "Email") > 0 Then
        SPURL = "\\sharepoint\sites\IS\Shared Documents\Email Reports"
    ElseIf InStr(InFileName, "FTP") > 0 Then
        SPURL = "\\sharepoint\sites\IS\Shared Documents\FTP Reports"
    ElseIf InStr(InFileName, "SSL") > 0 Then
        SPURL = "\\sharepoint\sites\IS\Shared Documents\SSL Reports"
    End If 

    If SPURL = "" Then
        MsgBox "File: " & SPSource & " is not a valid file from STRM..." & vbCrLf & _
            "Not sure where to upload it to SharePoint... " & vbCrLf & vbCrLf & _
            SPSource & " will be deleted...", 48, "myScript"
        Exit Function
    End If

    ' build the final part of the path based on the year and month
    MyDate = Left(InFileName, 4) & "_" & MonthName(Mid(InFileName, 5, 2))
    MyTitle = Mid(InFileName, 10, InStr(InFileName, ".") - 10)
    SPURL = SPURL & "\" & MyDate
    DestFile = SPURL & "\" & InFileName

    ' copy the file(s) to the sharepoint path if its not already there
    If Not oFS.FileExists(DestFile) Then
        oFS.CopyFile SPSource, SPURL, True
    End If
end function