Sharepoint Online-CSOM使用GetFolderByServerRelativeUrl和caml查询来获取文件夹中的所有文件以限制结果

时间:2019-01-14 13:57:16

标签: c# vb.net sharepoint sharepoint-online csom

你好,我的Stackoverflow朋友们。当目录中有5,000个以上的文件时,我在从sharepoint检索文件名时遇到了这个问题。我能够成功获取文件,直到达到5,000个文件限制,现在结果包含0个文件。

我知道我必须使用caml查询来获取结果,但是我不确定语法是否正确。

    Private Function GetSharePointFilesinFolder(siteURL As String, serverRelativePath As String, username As String, password As String) As String


    Using cContext = New ClientContext(siteURL) With {.Credentials = New SharePointOnlineCredentials(username, getSecurePass(password)), .RequestTimeout = 90000}

        Dim listfolder = cContext.Web.GetFolderByServerRelativeUrl(serverRelativePath)
        cContext.Load(listfolder.Properties)

        cContext.ExecuteQuery()

        Dim list = cContext.Web.Lists.GetById(New Guid(listfolder.Properties("vti_listname").ToString()))
        cContext.Load(list)
        cContext.ExecuteQuery()



        Dim items = New List(Of ListItem)
        Dim camlQuery = New CamlQuery()
        Dim rowLimit = 4999
        camlQuery.ViewXml = "<View Scope='RecursiveAll'>" + "<ViewFields><FieldRef Name='FileLeafRef' /></ViewFields><RowLimit Paged='TRUE'>" & rowLimit & "</RowLimit></View>"
        Dim position As ListItemCollectionPosition = Nothing
        Do
            If Not position Is Nothing Then
                camlQuery.ListItemCollectionPosition = position
            End If

            Dim listitems = list.GetItems(camlQuery)

            cContext.Load(listitems)
            cContext.ExecuteQuery()

            items.AddRange(listitems.ToList())

            position = listitems.ListItemCollectionPosition

        Loop While Not position Is Nothing

        For Each listItem As ListItem In items
            Dim aa = listItem("Name")
        Next

    End Using

    Return ""
End Function

调用listItem(“ Name”)我收到一条错误消息,指出它尚未初始化。我有点困在这里...帮助吗?

我将用C#或VB回答。这个特定的项目是VB,但我两个人都很流利。

这是我的原始代码,但是由于5,000行的限制,它不再起作用。在将文件下载到本地目录之前,我正在检查本地目录以确保我拥有该文件。

 Private Sub GetSharePointFilesinFolder(siteURL As String, serverRelativePath As String, username As String, password As String, LocalPath As String)

    Using clientContext = New ClientContext(siteURL) With {.Credentials = New SharePointOnlineCredentials(username, getSecurePass(password)), .RequestTimeout = 5000}

        Try
            clientContext.Load(clientContext.Web.GetFolderByServerRelativeUrl(serverRelativePath).Files)
            clientContext.ExecuteQuery()

            For Each b As Microsoft.SharePoint.Client.File In clientContext.Web.GetFolderByServerRelativeUrl(serverRelativePath).Files

                LocalPath = Path.Combine(LocalPath, b.Name)

                If Not System.IO.File.Exists(LocalPath) Then

                    Dim fileinfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, b.ServerRelativeUrl)
                    Using fs = System.IO.File.Create(LocalPath)
                        fileinfo.Stream.CopyTo(fs)
                    End Using

                End If

            Next

        Catch ex As Exception

        End Try

    End Using

End Sub

0 个答案:

没有答案