1

When im trying to use following code to download file from sharepoint site using rest services I was getting Remote server returned 403 forbidden - Please help

String fileurl = "exact sharepoint file url"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fileurl); NetworkCredential credential = new NetworkCredential("username", "password","domain"); //request.Credentials = credential; //request.ContinueTimeout = 10000; request.Credentials = credential; request.Headers["UserAgent"] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4"; request.Accept = "/"; // request.Headers["Accept"] = "/"; WebResponse resp = await request.GetResponseAsync(); 
3
  • HTTP error 403 means that you authenticated successfully but that the user you authenticated with does not have the right role or permissions. Check your user. Commented May 21, 2014 at 17:22
  • Thanks David, but i am able to download file from web browser using the same user. Commented May 22, 2014 at 4:29
  • Another reason would be that you are not authorized using the download technique you are using. Is it a different way? e.g. REST or SOAP? Commented May 22, 2014 at 7:41

3 Answers 3

1

To be able to download file from SharePoint Online/Office 365 using CSOM or REST the authentication has to be performed.

About SharePoint Online authentication

Since SharePoint Online (SPO) uses claims based authentication, you could consider the following options:

Consume SharePoint REST service using .NET

In order to consume SharePoint REST service using .NET you could consider the following approaches:

  • HttpClient - Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI. (.NET Framework 4.5)
  • WebClient - provides common methods for sending data to and receiving data from a resource identified by a URI. (.NET Framework 1.1)
  • HttpWebRequest - provides an HTTP-specific implementation of the WebRequest class, more low-level then the previous ones (.NET Framework 1.1)

All of them allows to download a file from SharePoint Online.

Examples

Example 1. How to specify SPO credentials using SharePointOnlineCredentials class

var request = (HttpWebRequest)WebRequest.Create(endpointUri); request.Credentials = new SharePointOnlineCredentials(username, securedPassword); //... 

Example 2. How to specify SPO authentication cookies using MsOnlineClaimsHelper class:

var claimshelper = new MsOnlineClaimsHelper(webUri, userName, password); var endpointUri = new Uri(webUri,string.Format("/_api/web/getfilebyserverrelativeurl('{0}')/$value", fileUrl)); var request = (HttpWebRequest)WebRequest.Create(endpointUri); request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f"); request.Method = "GET"; request.CookieContainer = claimshelper.CookieContainer; //... 

Key points:

  • SharePoint Auth cookies are passed via CookieContainer property
  • According to Files and folders REST API reference the following endpoint is used to retrieve file content: <app web url>/_api//web/getfilebyserverrelativeurl('<file url>')/$value
Sign up to request clarification or add additional context in comments.

Comments

0

try this one :

using System.Net; using (WebClient webClient = new WebClient ()) { webClient.DownloadFile(fileurl , filename); } 

1 Comment

im trying to download sharepoint files using windows metro app
0

try this one. let me know it will work or not?

HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StreamContent(new FileStream(HttpContext.Current.Server.MapPath("~/Documents/" + documentFileName.Name), FileMode.Open, FileAccess.Read)); response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = documentFileName.Name; response; 

1 Comment

this works for normal file download from server.we are looking solution for downloading files from sharepoint thru rest services.when we connect to sharepoint site and pass urls and try to get stream we are getting 403 forbidden exception.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.