Efficient Data Access: Retrieving SharePoint List Files in .NET Core

Introduction

Accessing files stored in SharePoint lists is crucial for many .NET Core applications. In this guide, we'll demonstrate how to efficiently retrieve files from SharePoint lists using .NET Core and PnP.Framework. Follow along as we walk through the process step by step.

Step 1. Setting Up Authentication and Connection 

Step 2. Retrieving SharePoint List Files

  • Next, we'll retrieve files from the target SharePoint list. We'll employ the CamlQuery class to specify the scope and row limit for our query. Additionally, we'll include the AttachmentFiles property in our query to fetch file attachments associated with list items.
  • To handle large lists, we'll implement iterative fetching techniques. By fetching list items in manageable batches, we can efficiently retrieve files from SharePoint lists without hitting any threshold limitations.

Code Implementation

using PnP.Framework;

public async Task FetchSharepointFile(string? SiteUrl, string? ClientID, string? ClientSecret, string? ConnectionString)
{
    try
    {
        //refere this blog to generate Client ID & Client Secret
        //https://www.c-sharpcorner.com/blogs/sharepoint-integration-registering-site-for-client-id-secret

        // Setting SharePoint site URL, client ID, client secret, and database connection string
        SiteUrl = "https://(Domain Name).com/sites/(List Name)";
        ClientID = "********-****-****-****-************";
        ClientSecret= "*******************************************=";

        // Authenticating with SharePoint
        using (ClientContext context = new AuthenticationManager().GetACSAppOnlyContext(SiteUrl, ClientID, ClientSecret))
        {
            ListItemCollection listItems;

            // Retrieving target SharePoint list
            List targetList = context.Web.Lists.GetByTitle("(List Name)");
            CamlQuery query = new CamlQuery
            {
                ViewXml = $"<View Scope='RecursiveAll'><RowLimit>4800</RowLimit></View>"
            };

            // Iteratively fetching list items
            do
            {
                listItems = targetList.GetItems(query);
                context.Load(listItems);

                //Load files
                context.Load(listItems, collection => collection.Include(item => item.AttachmentFiles));
                context.ExecuteQuery();

                // Processing retrieved items
                foreach (var item in listItems)
                {
                    // Outputting item details (for demonstration purposes)
                    Console.WriteLine(item);
                }
                query.ListItemCollectionPosition = listItems.ListItemCollectionPosition;
            } while (listItems.ListItemCollectionPosition != null);
        }
    }
    catch (Exception ex)
    {
        // Handling exceptions
        Console.WriteLine("An error occurred in Process of featching the files: " + ex);
    }
}
C#
Copy

Step 3. Viewing Retrieved Files

  • Once the code implementation is complete, we'll view the retrieved files from the SharePoint list. We'll demonstrate how to access and process the fetched files within the .NET Core application.
  • Scenario: File Retrieval from SharePoint List Here's a screenshot demonstrating the successful retrieval of files from the SharePoint list:

Conclusion

By following the steps outlined in this article, you can efficiently retrieve files from SharePoint lists using .NET Core. Leveraging PnP.Framework and iterative fetching techniques, you can seamlessly integrate SharePoint data retrieval into your .NET Core applications, enhancing their functionality and data access capabilities.

Please consider liking and following me for more articles and if you find this content helpful.

Ebook Download
View all
Learn
View all