2010-09-24 128 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