2010-09-24 134 views
3

我想要的文件的自動上傳到SharePoint文檔庫。我跑過無數帖子(在這個論壇和其他論壇上),但似乎無法得到有效的東西。儘管我已經完成了一些簡單的VBA和VB腳本,但我並不是一名開發人員。VBScript來上傳文件到SharePoint DocLib

我在尋找的是一種自動從本地機器上傳文件(.xlsx和.zip類型)到特定SharePoint文檔庫的解決方案(讓我們使用「.../sharepoint/Metrics/Forms /AllItems.aspx「作爲列表)使用VBA或VB腳本。

在研究這個問題,這裏有一些其他的想法/意見,希望能幫助別人提供我一個解決方案:

  • 我不能在SharePoint服務器上做任何更改
  • 我需要能夠上傳文件
  • 我只想找VBA/VBS解決方案(沒有C#或.NET)
  • 可能需要設置元數據時上傳
  • 當憑據傳遞

預先感謝您的任何幫助。

回答

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字符。否則,上面的腳本將不起作用。

+0

謝謝你這麼多!這正是我所需要的。我已經有了'插入'並正在工作。我現在唯一的問題是:如何設置其他metedata字段的值?示例...我們需要在上傳時爲每個文檔提供描述和項目名稱...因此,我如何設置該設置? – user457338 2010-09-28 01:26:13

+1

在上面的腳本中,添加了元數據「vti_title」。您可以通過在meta_info = []括號內添加更多的元數據屬性。例如。 ' 「; meta_info = [vti_title%3bSW%7C」 +逃生(標題)+ 「;項目%3bSW%7C」 +逃生(項目)+ 「]]」'。您必須在文檔庫中有一個名爲'project'的自定義字段。 「SW」意味着它必須是一個字符串值(布爾使用「BW」(真/假),或「IW」爲整數)。 – 2010-09-28 05:24:20

0

你最好的解決辦法是使用FP RPC(這是FrontPage中遠程過程調用)。這基本上是一個Web請求,您可以將元數據和文件內容作爲參數傳遞給您。這可以從任何語言,包括VBA/VBS 這是方法的正式說明已經完成:http://msdn.microsoft.com/en-us/library/ms479623.aspx 你可以找到關於如何構建實用

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