vb.net - 使用脚本任务删除 Sharepoint 文件
问题描述
我需要使用 SSIS 中的脚本任务删除 Sharepoint 文件。在 Visual Basic 中,我尝试使用SPListItemCollection
with 导入Microsoft.Sharepoint
,但它无法识别命名空间。我没有找到很多关于这个主题的线程,或者我发现的内容与脚本任务无关,所以任何帮助都将不胜感激。非常感谢
基于@Hadi 回答的更新
感谢哈迪的回答。我已经放弃了使用 SPListCollection 的想法,因为它看起来太复杂了。相反,我试图在将文件从 Sharepoint 下载到本地文件夹后删除该文件。我需要在实际删除文件的那一行获得帮助。这是代码:
Public Sub Main()
Try
' get location of local folder
Dim dir As DirectoryInfo = New DirectoryInfo(Dts.Variables("DestP").Value.ToString())
If dir.Exists Then
' Create the filename for local storage
Dim file As FileInfo = New FileInfo(dir.FullName & "\" & Dts.Variables("FileName").Value.ToString())
If Not file.Exists Then
' get the path of the file to download
Dim fileUrl As String = Dts.Variables("SHP_URL").Value.ToString()
If fileUrl.Length <> 0 Then
Dim client As New WebClient()
If Left(fileUrl, 4).ToLower() = "http" Then
'download the file from SharePoint
client.Credentials = New System.Net.NetworkCredential(Dts.Variables("$Project::UserN").Value.ToString(), Dts.Variables("$Project::Passw").Value.ToString())
client.DownloadFile(fileUrl.ToString() & "/" & Dts.Variables("FileName").Value.ToString(), file.FullName)
Else
System.IO.File.Copy(fileUrl.ToString() & Dts.Variables("FileName").Value.ToString(), file.FullName)
End If
'delete file from Sharepoint
client.(fileUrl.ToString() & "/" & Dts.Variables("FileName").Value.ToString(), file.FullName).delete()
Else
Throw New ApplicationException("EncodedAbsUrl variable does not contain a value!")
End If
End If
Else
Throw New ApplicationException("No ImportFolder!")
End If
Catch ex As Exception
Dts.Events.FireError(0, String.Empty, ex.Message, String.Empty, 0)
Dts.TaskResult = ScriptResults.Failure
End Try
Dts.TaskResult = ScriptResults.Success
End Sub
解决方案
更新 1 - 使用 FtpWebRequest 删除
您不能使用WebClient
类删除文件。你可以使用FtpWebRequest
类来做到这一点。并发送WebRequestMethods.Ftp.DeleteFile
如下链接中所述的请求:
它也应该与 Sharepoint 一起使用。
这是VB.NET中的函数
Private Function DeleteFile(ByVal fileName As String) As String
Dim request As FtpWebRequest = CType(WebRequest.Create(fileUrl.ToString() & "/" & fileName), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DeleteFile
request.Credentials = New NetworkCredential(Dts.Variables("$Project::UserN").Value.ToString(), Dts.Variables("$Project::Passw").Value.ToString())
Using response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
Return response.StatusDescription
End Using
End Function
您应该替换以下行:
client.(fileUrl.ToString() & "/" & Dts.Variables("FileName").Value.ToString(), file.FullName).delete()
和
DeleteFile(Dts.Variables("FileName").Value.ToString())
您也可以使用以下凭据:
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
参考
- 如何将凭据传递给 httpwebrequest 以访问 SharePoint 库
- 如何在 C# 中从 FTP 服务器中删除文件
- 下载 FTP 文件夹中的所有文件,然后删除它们
- 在 C# 中从 FTP 删除文件
- 如何在 C# 中从 FTP 服务器中删除文件
初步答案
我一直在寻找类似的问题,看起来您无法使用File System Task
or删除 SSIS 中的 Sharepoint 文件Execute Process Task
,唯一的方法是使用Script Task
. 网上有很多链接描述了这个过程,例如:
关于您提到的问题,我认为您应该确保将Microsoft.Sharepoint.dll
其添加为脚本任务中的参考。如果是这样,请尝试使用Microsoft.Sharepoint.SPListItemCollection
而不是SPListItemCollection
.
推荐阅读
- flutter - 如何通过单击flutter中的排序按钮对卡片列表进行排序(根据名称)
- qt - 如何在 Qt Creator 的 ui.qml 文件中调用函数
- react-native - 在 React Native Expo 上添加用于身份验证流程的加载器
- amazon-web-services - AWS ALB 中的超时值
- javascript - 尝试使用动态道具渲染相同的反应组件(道具来自 json 文件)
- javascript - 失败:模块“DynamicTestModule”导入的意外值“TreeviewConfig”。请在单元测试中添加@NgModule 注解
- sql - Hive - 季度中的最大(而不是最后一个)日期
- python - 如何在不下载文档的情况下将文档从一个 REST API 传递到另一个 REST API
- typescript - 如何使用问题运算符优化代码
- javascript - 从 Material UI TextField 拨打电话或打开电子邮件客户端