0

I am trying to find the best way to get files inside a specific sub folder in the sharepoint website, but the only links I find relate to the root folders.

I have a Sharepoint site which is really just a fileAttachments repository.

The Documents page of the website has folder for each customer. Then In each Customer folder there is another folder named for a CaseNumber. And in that Case Number folder, I drop all file attachments.

I am able to create the Customer folders and then a sub folder for the Case Number folder. And then I can upload files just fine through code.

I just can't seem to use the Context to retrieve the list of files in one of those sub folders (NOT THE ROOT FOLDER).

Can someone please advise how I can get the list of files with the following inputs?

sharePointBaseURL = "https://mySample.sharepoint.com" siteName = "CustomerCaseAttachments" libraryName = "Documents"

FOR EXAMPLE, Assume there is a ROOT folder for customer CustomerName = "Customer_A" Assume that in the Customer_A folder, there is a subfolder for the CaseNumber = "123456" Finally, assume there a two files in that folder "logFile1.txt" and "Capture.jpg"

So I need to write a function that will take CustomerName and CaseNumber to identify the two folder and subfolder on the site. Then Return the list of files in that subfolder

enter image description here

##############
##  UPDATE  ##
##############

I figured out how to do it, but still ask the question if there is not a better way?

Here is the function I came up with.

Public Function GetFileListFromSharepoint_Datatable(CustomerName As String, CaseNumber As String) As DataTable
    Dim dtAttachments As DataTable = FileAttachmentDatatableWithHeaders()
    Dim spCon As New SharepointConnector()
    Try
        Dim siteUrl As String = $"{spCon.config.SharePointBaseURL}/sites/{spCon.config.SiteName}"
        Dim serverRelativeUrl As String = $"/sites/{spCon.config.SiteName}/{spCon.config.libraryName}"
        Dim FileIsOnSharepoint As Boolean = False
        Using ctx As ClientContext = spCon.CreateSharepointClientContext()
            Dim ctxWeb As Microsoft.SharePoint.Client.Web = ctx.Web
            Dim rootFolderList As Microsoft.SharePoint.Client.List = ctxWeb.Lists.GetByTitle("Documents")
            ctx.Load(rootFolderList)
            ctx.Load(rootFolderList.RootFolder.Folders)
            ctx.Load(rootFolderList.RootFolder.Files)
            ctx.ExecuteQuery()
            Dim listOfCustomerFolders As FolderCollection = rootFolderList.RootFolder.Folders
            For Each customerFolder In listOfCustomerFolders
                If customerFolder.Name = CustomerName Then
                    ctx.Load(customerFolder)
                    ctx.Load(customerFolder.Folders)
                    ctx.ExecuteQuery()
                    Dim listOfCaseFolders As FolderCollection = customerFolder.Folders()
                    For Each caseFolder In listOfCaseFolders
                        If caseFolder.Name = CaseNumber Then
                            ctx.Load(caseFolder)
                            ctx.Load(caseFolder.Files)
                            ctx.ExecuteQuery()
                            Dim listOfFiles As FileCollection = caseFolder.Files
                            For Each f In listOfFiles
                                Dim dr As DataRow = dtAttachments.Rows.Add
                                dr("Id") = f.ServerRelativeUrl
                                dr("FileName") = f.Name
                                dr("Size") = BytesToString(f.Length)
                                dr("ChangedDate") = f.TimeLastModified
                            Next
                        End If
                    Next
                End If
            Next
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
        logger.Error(ex)
    End Try
    Return dtAttachments
End Function

2 Answers 2

0

You can use the CamlQuery.FolderServerRelativeUrl property to indicate the target folder.

Screen capture from Complete basic operations using SharePoint client library code | Retrieve items from a SharePoint list.

enter image description here

In your case you would use something like:

List documentsList = context.Web.Lists.GetByTitle("Documents");

CamlQuery query = CamlQuery.CreateAllItemsQuery();
query.FolderServerRelativeUrl = "/Documents/Customer_A/123456";
ListItemCollection items = documentsList.GetItems(query);
0

Get-PnPListItem -List "Shared Documents" -FolderServerRelativeUrl "/sites/CustomerCaseAttachments/Shared Documents/Customer_A/123456"

Retrieves all list items from the 123456 folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.