使用脚本任务删除 Sharepoint 文件
delete Sharepoint file using Script Task
我需要使用 SSIS 中的脚本任务删除 Sharepoint 文件。在 Visual Basic 中,我试过将 SPListItemCollection
与导入 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
class 删除文件。您可以使用 FtpWebRequest
class 来做到这一点。并发送 WebRequestMethods.Ftp.DeleteFile
请求,如下面 link 中所述:
它应该也可以与 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;
参考资料
- How To Delete a File From FTP Server in C#
- Downloading all files in a FTP folder and then deleting them
- Deleting file from FTP in C#
- How To Delete a File From FTP Server in C#
初始答案
我一直在寻找类似的问题,看起来您无法使用 File System Task
或 Execute Process Task
删除 SSIS 中的 Sharepoint 文件,唯一的方法是使用 [=19] =].网上有很多link描述这个过程比如:
- how to delete or remove only text files from share point in C# or SSIS script?
- Fastest way to delete all items with C#
- Deleting files programatically
- Deleting all the items from a large list in SharePoint
关于您提到的问题,我认为您应该确保在脚本任务中添加 Microsoft.Sharepoint.dll
作为参考。如果是这样,请尝试使用 Microsoft.Sharepoint.SPListItemCollection
而不是 SPListItemCollection
。
感谢@Hadi 的帮助。
对我来说,它不适用于 FTPWebResponse。
它与 HttpWebRequest 一起工作。这是脚本:
Dim request As System.Net.HttpWebRequest = CType(WebRequest.Create(fileUrl.ToString() & "/" & Dts.Variables("FileName").Value.ToString()), HttpWebRequest)
request.Credentials = New System.Net.NetworkCredential(Dts.Variables("$Project::UserN").Value.ToString(), Dts.Variables("$Project::Passw").Value.ToString())
request.Method = "DELETE"
Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
我需要使用 SSIS 中的脚本任务删除 Sharepoint 文件。在 Visual Basic 中,我试过将 SPListItemCollection
与导入 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
class 删除文件。您可以使用 FtpWebRequest
class 来做到这一点。并发送 WebRequestMethods.Ftp.DeleteFile
请求,如下面 link 中所述:
它应该也可以与 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;
参考资料
- How To Delete a File From FTP Server in C#
- Downloading all files in a FTP folder and then deleting them
- Deleting file from FTP in C#
- How To Delete a File From FTP Server in C#
初始答案
我一直在寻找类似的问题,看起来您无法使用 File System Task
或 Execute Process Task
删除 SSIS 中的 Sharepoint 文件,唯一的方法是使用 [=19] =].网上有很多link描述这个过程比如:
- how to delete or remove only text files from share point in C# or SSIS script?
- Fastest way to delete all items with C#
- Deleting files programatically
- Deleting all the items from a large list in SharePoint
关于您提到的问题,我认为您应该确保在脚本任务中添加 Microsoft.Sharepoint.dll
作为参考。如果是这样,请尝试使用 Microsoft.Sharepoint.SPListItemCollection
而不是 SPListItemCollection
。
感谢@Hadi 的帮助。 对我来说,它不适用于 FTPWebResponse。 它与 HttpWebRequest 一起工作。这是脚本:
Dim request As System.Net.HttpWebRequest = CType(WebRequest.Create(fileUrl.ToString() & "/" & Dts.Variables("FileName").Value.ToString()), HttpWebRequest)
request.Credentials = New System.Net.NetworkCredential(Dts.Variables("$Project::UserN").Value.ToString(), Dts.Variables("$Project::Passw").Value.ToString())
request.Method = "DELETE"
Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)