Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Note : I want a bulk download not a single file from document library.

Here is the code I am using now but it is throwing me some error like "Unauthorized with status code 401"

C#
class Program
    {
        static void Main(string[] args)
        {
            // give the excel file path.
            string filePath = "exel file path";

            DownloadXLFile(filePath, "my path");

        }



public static void DownloadXLFile(string strPath, string strDest)
        {
            try
            {
                ExcelService xlService = new ExcelService();
                xlService.Url = "http://mysiteurl/_vti_bin/ExcelService.asmx";
                System.Net.CredentialCache.DefaultNetworkCredentials.UserName = "username";
                System.Net.CredentialCache.DefaultNetworkCredentials.Password = "password";
                System.Net.CredentialCache.DefaultNetworkCredentials.Domain = "http://siteurl/path";
                //xlService.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                Status[] status;
                string sessionId = xlService.OpenWorkbook(strPath, String.Empty, String.Empty, out status); // Here iam getting exception
                byte[] workbook = xlService.GetWorkbook(sessionId, WorkbookType.FullWorkbook, out status);
                FileStream fs = new FileStream(strDest, FileMode.Create);
                fs.Write(workbook, 0, workbook.Length);
                fs.Dispose();
            }
            catch (Exception ex)
            {
                throw new Exception("CreateFile - " + ex.Message);
            }
        } 
}
Posted
Updated 2-Mar-15 18:03pm
v2
Comments
Pradip R 3-Mar-15 5:08am    
Did you check the service URL you are accessing, does have the access with your credentials?
KaushalJB 3-Mar-15 5:09am    
You need to use Client Object Model of SharePoint to access data from SharePoint site.

1 solution

The way you are passing network credentils is wrong .You need to pass it like this

C#
xlService.Credentials = new NetworkCredential(username,password);

e.g:- xlService.Credentials = new NetworkCredential("myName","myPassword");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900