使用脚本任务删除Sharepoint文件

时间:2019-02-09 19:34:11

标签: vb.net sharepoint ssis etl 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

2 个答案:

答案 0 :(得分:1)

更新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;

参考


初始答案

一段时间以来,我一直在寻找类似的问题,看来您无法使用File System TaskExecute Process Task在SSIS中删除Sharepoint文件,唯一的方法是使用Script Task 。在线上有许多描述此过程的链接,例如:

关于您提到的问题,我认为您应该确保在脚本任务中添加了Microsoft.Sharepoint.dll作为参考。如果是这样,请尝试使用Microsoft.Sharepoint.SPListItemCollection代替SPListItemCollection

答案 1 :(得分:0)

感谢@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)