Sharepoint Online - CSOM получает все файлы в папке, используя GetFolderByServerRelativeUrl и caml-запрос, чтобы ограничить результаты - PullRequest
0 голосов
/ 14 января 2019

Здравствуйте, мои друзья из Stackoverflow. Я застрял в этой проблеме при получении имен файлов из sharepoint, когда в каталоге более 5000 файлов. Мне удалось успешно получить файлы, пока я не достиг предела в 5000 файлов, теперь результат содержит 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 ("Имя") Я получаю сообщение об ошибке, в котором говорится, что он не был инициализирован. Я немного застрял здесь ... помочь?

Я приму ответы на C # или VB. Этот конкретный проект VB, но я свободно владею обоими.

Это был мой оригинальный код, но он больше не работает из-за ограничения в 5000 строк. Я проверял свой локальный каталог, чтобы убедиться, что у меня есть файл, прежде чем загружать его в локальный каталог.

 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
...