gpt4 book ai didi

sharepoint - 批量复制文件到 SharePoint 站点

转载 作者:行者123 更新时间:2023-12-04 02:58:43 29 4
gpt4 key购买 nike

我搜索了 SO、SU 和 SP.SE寻求解决方案,但找不到我需要的东西。我正在寻找一个解决方案,它可能是一个脚本一些其他非编码方法/工具。

我正在尝试编写一个脚本(供其他人使用)或某种其他形式的自动化,以将各种报告自动上传到 SharePoint 网站。我已经设法使以下 (VBScript) 代码起作用,但仅适用于基于文本的文件——在本例中为 .CSV,尽管这也适用于 .TXT 等。

Option Explicit

Dim sCurPath
sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
UploadAllToSP sCurPath

Sub UploadAllToSP(sFolder)
Dim fso, folder, fil
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(sFolder)
For Each fil In folder.Files
If fso.GetExtensionName(fil) = "csv" Then
UploadFileToSP fil
End If
Next
End Sub

Sub UploadFileToSP(ofile)
Dim xmlhttp
Dim sharepointUrl
Dim sharepointFileName
Dim tsIn
Dim sBody

Set tsIn = ofile.openAsTextstream
sBody = tsIn.readAll
tsIn.close
sharepointUrl = "http://SHAREPOINT URL HERE"

sharepointFileName = sharepointUrl & ofile.name
set xmlHttp = createobject("MSXML2.XMLHTTP.4.0")
xmlhttp.open "PUT", sharepointFileName, false
xmlhttp.send sBody
If xmlhttp.status < 200 Or xmlhttp.status > 201 Then
wscript.echo "There was a problem uploading " & ofile.name & "!"
End If
End Sub

这只适用于文本文件,因为它将文本数据通过管道传输到 SP 站点上的文件中。但是,如果我想传输任何类型的二进制文件(.XLS、.PDF),这会导致上传垃圾。

我试着看一下 Shell.Application ==> .Namespace(),但这似乎不适用于 URL,但只能物理驱动器。这是我尝试过的一些其他方法(经过修剪以显示相关部分):

Set oApp = CreateObject("Shell.Application")

If oApp.NameSpace(sharepointUrl) <> Null then ' Always Null!
' Copy here
' Some lines omitted
oApp.NameSpace(sharepointUrl).CopyHere ofile.Name ' This also fails when not surrounded by the Null check
Else
MsgBox "SharePoint directory not found!"
End If

我还尝试了使用 xcopy 的批处理文件,但它也无法连接到 http://。我看了this method ,这可能对我有用,但我不想处理映射/NET USE,因为我们公司有多个网络共享,其映射因登录者而异。

因为这些都不能完全按照我需要的方式工作:有没有一种方法可以自动执行这种功能?

我有使用 VBA/VBscript 的经验,所以最好是像上面这样的脚本,或者内置到 MS Office 应用程序中的东西(Outlook 是最好的,但我可能可以适应我所提供的任何东西)。话虽这么说,我对任何允许我执行此操作的方法持开放态度,在 Windows 或 Office 中 native 运行。但是,我无权访问 Visual Studio,因此无法使用任何 .NET 功能。

最佳答案

感谢Sean Cheshire指出我没有看到的明显答案。发布相关代码,因为我不相信它还存在于 SO 上。

Sub UploadFilesToSP(sFolder)

Dim sharepointUrl
Dim sharepointFileName
Dim LlFileLength
Dim Lvarbin()
Dim LobjXML
Dim LvarBinData
Dim PstrFullfileName
Dim PstrTargetURL
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fldr
Dim f

'This has not been successfully tested using an "https" connection.
sharepointUrl = "http://SHAREPOINT URL HERE"
Set LobjXML = CreateObject("Microsoft.XMLHTTP")
Set fldr = fso.GetFolder(sFolder)

For Each f In fldr.Files
sharepointFileName = sharepointUrl & f.Name

PstrFullfileName = sFolder & f.Name
LlFileLength = FileLen(PstrFullfileName) - 1

' Read the file into a byte array.
ReDim Lvarbin(LlFileLength)
Open PstrFullfileName For Binary As #1
Get #1, , Lvarbin
Close #1

' Convert to variant to PUT.
LvarBinData = Lvarbin
PstrTargetURL = sharepointFileName

' Put the data to the server, false means synchronous.
LobjXML.Open "PUT", PstrTargetURL, False

' Send the file in.
LobjXML.Send LvarBinData
Next f

Set LobjXML = Nothing
Set fso = Nothing

End Sub

这是 VBA 代码,格式化为主要与 VBScript 一起使用,但我无法正确传输此 block 。作为 VBA,这可以通过分配数据类型等进行一些改进。

' Read the file into a byte array.
ReDim Lvarbin(LlFileLength)
Open PstrFullfileName For Binary As #1
Get #1, , Lvarbin
Close #1

关于sharepoint - 批量复制文件到 SharePoint 站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12039730/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com