使用XMLHTTP删除Sharepoint文件 [英] Deleting Sharepoint Files using XMLHTTP

查看:175
本文介绍了使用XMLHTTP删除Sharepoint文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这个优秀的工作,在这里:
批量复制文件到SharePoint网站

Following on from this excellent piece of work, here: Batch copy files to SharePoint site

我现在可以通过点击按钮将压缩文件上传到Sharepoint。

I can now upload my zipped files to Sharepoint with a click of a button.

我现在的问题是:如何使用相同的方法删除我上传的文件?

My problem is now thus: How do I delete the files I upload using the same method?

我稍后修改了代码,以将不同的文件保存到不同的SharePoint文件夹。
以下示例:

I've amended the code slightly to save different files to different SharePoint folders. Sample below:

 
Public Sub CopyToSharePoint()

Dim xmlhttp
Dim sharepointUrl
Dim sharepointFolder
Dim sharepointFileName
Dim LstrFileName,strFilePath,strMonthYear,PstrFullfileName,PstrTargetURL As String
Dim LlFileLength As Long
Dim Lvarbin()As Byte
Dim LvarBinData As Variant
Dim fso,LobjXML As Object
Set fso = CreateObject(Scripting.FileSystemObject)
Dim fldr作为文件夹
Dim f As File

Dim xmlhttp Dim sharepointUrl Dim sharepointFolder Dim sharepointFileName Dim LstrFileName, strFilePath, strMonthYear, PstrFullfileName, PstrTargetURL As String Dim LlFileLength As Long Dim Lvarbin() As Byte Dim LvarBinData As Variant Dim fso, LobjXML As Object Set fso = CreateObject("Scripting.FileSystemObject") Dim fldr As folder Dim f As File

'父Sharepoint
sharepointUrl =[SHAREPOINT PATH HERE]

'Parent Sharepoint sharepointUrl = "[SHAREPOINT PATH HERE]"

'设置月份%20年
strMonthYear =格式(Now(),mmmm yyyy)& \

'Sets the Month%20Year strMonthYear = Format(Now(), "mmmm yyyy") & "\"

'文件路径
strFilePath =[ARCHIVE DRIVE]& strMonthYear

'File Path strFilePath = "[ARCHIVE DRIVE]" & strMonthYear

'检查当前月份的DRA是否存在$ 20 $存在
如果Len(Dir(strFilePath,vbDirectory))= 0则
MkDir strFilePath
如果

'Check to see if DRA for current month%20year exists If Len(Dir(strFilePath, vbDirectory)) = 0 Then MkDir "strFilePath" End If

设置LobjXML = CreateObject(Microsoft.XMLHTTP)

Set LobjXML = CreateObject("Microsoft.XMLHTTP")

'我们从
上传文件我们设置fldr = fso.GetFolder(strFilePath)

'Where we're uploading files from Set fldr = fso.GetFolder(strFilePath)

对于每个f在fldr.Files中

For Each f In fldr.Files

如果格式(f.DateCreated,dd / mm / yyyy)=格式(Now(),dd / mm / yyyy)然后

If Format(f.DateCreated, "dd/mm/yyyy") = Format(Now(), "dd/mm/yyyy") Then

If InStr(1, f.Name, "[FILESTRING1]", vbTextCompare) > 0 Then
sharepointFolder = "[SHAREPOINTSTRING1]/"
    ElseIf InStr(1, f.Name, "[FILESTRING2]", vbTextCompare) > 0 Then
    sharepointFolder = "[SHAREPOINTSTRING2]"
        ElseIf InStr(1, f.Name, "[DONOTUPLOADTHISFILE]", vbTextCompare) > 0 Then
        GoTo NextF:
            Else
            sharepointFolder = "[SHAREPOINTMAINFOLDER]"
End If

sharepointFileName = sharepointUrl& sharepointFolder& f.Name

sharepointFileName = sharepointUrl & sharepointFolder & f.Name

PstrFullfileName = strFilePath & 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 = sharepointUrl & sharepointFolder & f.Name

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

'发送文件
LobjXML.Send LvarBinData

' Send the file in. LobjXML.Send LvarBinData

结束如果

NextF:
下一个f

NextF: Next f

Set LobjXML = Nothing
设置fso = Nothing

Set LobjXML = Nothing Set fso = Nothing

End Sub

End Sub

推荐答案

我没有关闭服务器的请求,
在单独的实例中设置它为我解决了它。

I'd not closed the request to the server, d'oh! Setting it up in a separate instance solved it for me.

我没有将文件名转换为二进制文件,然后转换为变体,只将其保存为串。
您必须从LastCoder示例中给出的最后一个 LobjXML.SEND 中省略 NOTHING 。添加此项将重现上述运行时错误。

I didn't convert the filename to binary and then to variant, merely kept it as a string. You must omit the NOTHING from the last LobjXML.SEND given in LastCoder's example. Adding this in reproduces the Run-time error I give above.

感谢您的帮助LastCoder。
这是修改后的代码:

Thanks for the help, LastCoder. Here's the amended code:


Public Sub DeleteFromSharePoint()

Dim xmlhttp
Dim sharepointUrl, sharepointFolder, sharepointFileName
Dim f, strZip As String
Dim LobjXML As Object

' Parent Sharepoint
sharepointUrl = "[SHAREPOINT URL]"

' In this test module, we're just deleting from the parent directory
sharepointFolder = ""

' Sets the report name we want to remove
f = "test"

' Sets the full .ZIP filename
' This is how reports are archived by date
strZip = f & "%20-%20" & Format(Now() - 1, "YYYY.MM.DD") & ".zip"

Set LobjXML = CreateObject("Microsoft.XMLHTTP")

    sharepointFileName = sharepointUrl & sharepointFolder & strZip

    ' Removes the data from the server, false means synchronous
    LobjXML.Open "DELETE", sharepointFileName, False

    ' Sends the request to remove the file
    LobjXML.Send

  Set LobjXML = Nothing

End Sub

这篇关于使用XMLHTTP删除Sharepoint文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆