.netsharepointdownload

How to Download a file from Sharepoint using .NET?


I am trying to build a .NET API to download SharePoint documents. I am using Pnp framework for authentication. I am able to authenticate and generate access token and get fields like Web Title. But, I am unable to perform any other operations like Download or view the List of Folders and Files etc.

I tried lot of ways on Stack Overflow and others, none of them helped. Any idea how to download a file from SharePoint? I am ok to move from Pnp Framework if it makes my life easier.

I tried the following ways to access the list of files and to download a file:

static void Main(string[] args)
{
    using (ClientContext context = new PnP.Framework.AuthenticationManager().GetACSAppOnlyContext("https://xxxxx.sharepoint.com/sites/xxxx", "clientID", "clientSecret"))
    {
        Web oWeb = context.Web;
        context.Load(oWeb);
        context.ExecuteQuery();
        Folder oFolder = oWeb.GetFolderByServerRelativeUrl("folderName/nestedFolderName");
        context.Load(oFolder);
        context.ExecuteQuery();
        Console.WriteLine("Folder Name: " + oFolder.Name);

        var library = context.Web.Lists.GetByTitle("folderName");
        context.Load(library.RootFolder);
        context.ExecuteQuery();

        Console.WriteLine(library.RootFolder.ServerRelativeUrl); 
        Console.WriteLine("Finisihed");
    }
}

I get this error if I try to download or create a file:

Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: Access denied

I get this error if I want to list of folders or list items:

The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.Operations that exceed the list view threshold


Solution

  • You can Use CAML to execute CAML queries in large SharePoint lists to avoid the exceptions:

    The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator

    Please try to use the following code to list of Folders or List items:

    using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
                 {
                     Web web = ctx.Web;
                     ctx.Load(web);
                     ctx.Load(web.Lists);
                     ctx.ExecuteQueryRetry();
     
                     List myList = web.Lists.GetByTitle("MyList");
                     ctx.Load(myList);
     
                     ctx.ExecuteQueryRetry();
     
     
                     List<string> strTempList = new List<string>();
     
                     ListItemCollectionPosition position = null;
                     var page = 1;
     
                     do
                     {
                         CamlQuery camlQuery = new CamlQuery();
                         camlQuery.ViewXml = @"<View Scope='Recursive'>
                                          <Query>
                                          </Query><RowLimit>5000</RowLimit>
                                      </View>";
                         camlQuery.ListItemCollectionPosition = position;
     
                         var listItems = myList.GetItems(camlQuery);
                         ctx.Load(listItems);
                         ctx.ExecuteQueryRetry();
     
                         position = listItems.ListItemCollectionPosition;
                         foreach (var listItem in listItems)
                         {
                             strTempList.Add(Convert.ToString(listItem["Title"]));
                         }
     
                         page++;
                     }
                     while (position != null);
    
                 }
    

    Also, you need to check if your account has permissions to download and create files.

    More information for reference:

    How To Execute CAML Query In Large SharePoint Lists Using CAML To Avoid The Exception – The Attempted Operation Is Prohibited Because It Exceeds The List View Threshold Enforced By The Administrator – SharePoint Office 365

    Large list issue with CSOM

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.