首页 > 解决方案 > 使用脚本任务删除 Sharepoint 文件

问题描述

我需要使用 SSIS 中的脚本任务删除 Sharepoint 文件。在 Visual Basic 中,我尝试使用SPListItemCollectionwith 导入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

标签: vb.netsharepointssisetlscript-task

解决方案


更新 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 Taskor删除 SSIS 中的 Sharepoint 文件Execute Process Task,唯一的方法是使用Script Task. 网上有很多链接描述了这个过程,例如:

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


推荐阅读