VBScript将文件上传到SharePoint

VBScript将文件上传到SharePoint

本文介绍了VBScript将文件上传到SharePoint DocLib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图自动将文件上传到SharePoint文档库。我跑过无数帖子(在这个论坛和其他人),但似乎无法得到的东西,工程。我不是一个真正的开发者,虽然我已经做了一些简单的VBA和VB脚本。

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



在研究这个问题时,以下是一些其他想法/意见,希望能帮助某人为我提供一个解决方案:


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

>

预先感谢您的帮助。

解决方案

使用FrontPage RPC的文件:

  Fu nction 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
$ b $ 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 =+ _
&保持%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.openPOST,siteUrl +/_vti_bin/_vti_aut/author.dll,false,userName,密码
xmlhttp.setRequestHeaderContent-Type,application / x-vermeer-urlencoded
xmlhttp.setRequestHeaderX-Vermeer-Content-Type,application / x-vermeer-urlencoded
xmlhttp.setRequestHeaderUser-Agent,FrontPage
xmlHttp.send stream

如果xmlHttp.status = 200那么

如果Instr(xmlHttp.responseText,successful)= 0那么

MsgBoxERROR:& vbCrLf& xmlHttp.responseText

其他
$ b $'签入

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.openPOST,siteUrl +/_vti_bin/_vti_aut/author.dll,false,userName,密码
xmlhttp.setRequestHeaderContent-Type ,application / x-vermeer-urlencoded
xmlhttp.setRequestHeaderX-Vermeer-Content-Type,application / x-vermeer-urlencoded
xmlhttp.setRequestHeaderUser-Agent, FrontPage
xmlHttp.send strHeader



End If

End If

如果xmlHttp.status / 100< 2然后
MsgBoxERROR:status =& xmlHttp.status& vbCrLf& xmlHttp.responseText
End If

End Sub

UploadFileC:\Users\myusername\Desktop\Test File.zip,_
http:// computername / Sites / sitename,_
Requirements / Test File.zip,_
Test title,_
Test checkin comment _
MYDOMAIN \myusername,mypassword
MsgBox完成




I'm trying to automate the uploading of files to a SharePoint document library. I've run across countless posts (on this forum and others) but can't seem to get something that works. I'm not really a developer, though I have done some simple VBA and VB Script.

What I'm looking for is a solution to automatically upload a file (.xlsx and .zip types specifically) from a local machine to a specific SharePoint document library (let's use ".../sharepoint/Metrics/Forms/AllItems.aspx" as the list) using VBA or VB Script.

In researching the issue, here are some other thoughts/comments that hopefully will help someone in providing me a solution:

  • I cannot change anything on the SharePoint server
  • I need to be able to pass credentials when uploading the file
  • I am only looking for VBA/VBS solutions (no C# or .NET)
  • I might need to set metadata when uploading

Thank you in advance for any help.

解决方案

The following VBScript uploads a file using 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"

Please note that the file name should consist of ASCII characters only. Otherwise, the above script will not work.

这篇关于VBScript将文件上传到SharePoint DocLib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:56