Vikesh Gupta
Vikesh Gupta

Reputation: 61

Download excel files from sharepoint site to local system using C#

I have a requirement of downloading an excel file from sharepoint to my local system. I have written the below method.

    internal static void DownloadFilesFromSharePoint(string siteUrl, string folderPath, string tempLocation)
    {
        ClientContext ctx = new ClientContext(siteUrl);
        ctx.Credentials = new NetworkCredential("username", "password");

        FileCollection files = ctx.Web.GetFolderByServerRelativeUrl(folderPath).Files;

        ctx.Load(files);
        ctx.ExecuteQuery();----FAILED HERE

        foreach (File file in files)
        {
            FileInformation fileInfo = File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
            ctx.ExecuteQuery();

            var filePath = tempLocation + file.Name;
            using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
            {
                fileInfo.Stream.CopyTo(fileStream);
            }
        }
    }

To call the above method i have the following in Main method:

    public static void Main()
    {
    Program.DownloadFilesFromSharePoint("http://sp.frk.com/teams/ResearchSystemsSupport/GT%20Resources/Forms/AllItems.aspx", "/TeamDocuments", @"c:\");

    }

But the code above is not working. I have marked the point where the code gave an exception(by marking----FAILED HERE) while debugging. Can anybody point out the problem here. Can anybody help me on this.Please give me a detailed explanation as well if you could.

Upvotes: 1

Views: 8310

Answers (2)

Vikesh Gupta
Vikesh Gupta

Reputation: 61

I finally figured out the code that works which is pasted below. As pointed in comment by LZ_MSFT ,i rechecked the sharepoint link that i was passing and they were wrong so changed it and it worked. Also in the network credentials,added domain.

    static void DownloadFilesFromSharePoint(string siteUrl, string siteFolderPath, string localTempLocation)
    {
        ClientContext ctx = new ClientContext(siteUrl);
        ctx.Credentials = new NetworkCredential("username", "password", "Domain");

        FileCollection files = ctx.Web.GetFolderByServerRelativeUrl(siteFolderPath).Files;

        ctx.Load(files);
        if (ctx.HasPendingRequest)
        {
            ctx.ExecuteQuery();
        }

        foreach (File file in files)
        {                
                FileInformation fileInfo = File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
                ctx.ExecuteQuery();

                var filePath = localTempLocation + "\\" + file.Name;
                System.IO.FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);

                fileInfo.Stream.CopyTo(fileStream);

        }
    } 

Calling function:

    static void Main(string[] args)
    {
        Program.DownloadFilesFromSharePoint(@"http://sp.frk.com/teams/ResearchSystemsSupport", @"http://sp.frk.com/teams/ResearchSystemsSupport/Global%20Equity%20Group/Daily_Checks", @"C:\Temp");       
    }

Upvotes: 3

LZ_MSFT
LZ_MSFT

Reputation: 4208

Replace the code below

Program.DownloadFilesFromSharePoint("http://sp.frk.com/teams/ResearchSystemsSupport/GT%20Resources/Forms/AllItems.aspx", "/TeamDocuments", @"c:\");

with

Program.DownloadFilesFromSharePoint("http://sp.frk.com/teams/ResearchSystemsSupport", "GT%20Resources/TeamDocuments", @"c:\");

The site Url and folder path are not right.

Upvotes: 0

Related Questions