Download file from SharePoint 365

string remoteUri = "http://www.contoso.com/library/homepage/images/"; string fileName = "ms-banner.gif", myStringWebResource = null; // Create a new WebClient instance. WebClient myWebClient = new WebClient(); // Concatenate the domain with the Web resource filename. myStringWebResource = remoteUri + fileName; Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource); // Download the Web resource and save it into the current filesystem folder. myWebClient.DownloadFile(myStringWebResource,fileName); Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource); Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath); 

I'am Using This Code From MSDN Website

But I came across an error: 403 is forbidden

Can someone help me put this to work?

+3
c # sharepoint office365
source share
3 answers

This error occurs because the request is not authenticated. To authenticate with Ofice365 / SharePoint Online, you can use the SharePointOnlineCredentials class from the SharePoint Server 2013 Client Component Component .

The following example shows how to download a file from SPO:

 const string username = " username@tenant.onmicrosoft.com "; const string password = "password"; const string url = "https://tenant.sharepoint.com/"; var securedPassword = new SecureString(); foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c); var credentials = new SharePointOnlineCredentials(username, securedPassword); DownloadFile(url,credentials,"/Shared Documents/Report.xslx"); private static void DownloadFile(string webUrl, ICredentials credentials, string fileRelativeUrl) { using(var client = new WebClient()) { client.Credentials = credentials; client.DownloadFile(webUrl, fileRelativeUrl); } } 
+5
source share

I ran into the same problem and tried the answer suggested by Vadim Gremyachev. However, it still kept error 403. I added two additional headers for force forms-based authentication, as shown below:

 client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); client.Headers.Add("User-Agent: Other"); 

After that, he began to work. Thus, the complete code is as follows:

 const string username = " username@tenant.onmicrosoft.com "; const string password = "password"; const string url = "https://tenant.sharepoint.com/"; var securedPassword = new SecureString(); foreach (var c in password.ToCharArray()) securedPassword.AppendChar(c); var credentials = new SharePointOnlineCredentials(username, securedPassword); DownloadFile(url,credentials,"/Shared Documents/Report.xslx"); private static void DownloadFile(string webUrl, ICredentials credentials, string fileRelativeUrl) { using(var client = new WebClient()) { client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); client.Headers.Add("User-Agent: Other"); client.Credentials = credentials; client.DownloadFile(webUrl, fileRelativeUrl); } } 
+3
source share

You need to add credentials to your request.
In SharePoint Online, you need to add SharePointOnlineCredentials .

You can also use the new Office 365 API

0
source share

All Articles