Access sharepoint document in c# application (re-asking)

[Disclaimer] : This is a repost from https://learn.microsoft.com/en-us/answers/questions/1109948/access-sharepoint-document-in-c-application, but I had no further response after the first one, and I do really need an answer on this problem.

I thought that since it took me a long time to answer the solution, the ticket has been close or something. Please accept my apologies for duplicating the demand.

Best regards, Pierre

I would like to access a online public's sharepoint's excel's document, inside a dotnet c# win application. So far, by providing my sharepoint's user's credentials, it worked perfectly. However, I would like to access the document with no credentials to provide.

For this, I created a direct public link to the document in our Sharepoint, following the provided tuto to do it correctly. When i copy-paste the link in a browser (while beeing NOT connected to any sharepoint's profile), it works wonder.

However, when i try to access the same provided link in my c# code, I do encounter a 403 error. Can't really figure it out why.

Could you provide me some informations ?

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

10,816 questions Sign in to follow SharePoint Development

SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications. Development: The process of researching, productizing, and refining new or existing technologies.

2,896 questions Sign in to follow 0 comments No comments Report a concern I have the same question I have the same question 0 votes Add comment

1 answer

Sort by: Most helpful Most helpful Newest Oldest Alan Farias 750 Reputation points 2023-03-01T09:30:19.5566667+00:00

It's possible that the public link you created still requires some sort of authentication in order to access the document programmatically, even though it works in a browser without being signed in to SharePoint. One way to test this would be to try accessing the document using the public link in a REST API client like Postman or Insomnia, and see if you get the same 403 error. If so, it's likely that you will need to provide some sort of authentication in order to access the document programmatically. In that case, you could consider using the SharePoint REST API to authenticate and access the document. You can use the SharePoint REST API to authenticate with SharePoint Online using OAuth and then access files, lists, and other resources using the API. Here's an example of how to authenticate with the SharePoint REST API using C#:

using Microsoft.IdentityModel.Clients.ActiveDirectory; using System.Net.Http; using System.Net.Http.Headers; // Replace these values with your own SharePoint Online site and app registration details string siteUrl = "https://yourtenant.sharepoint.com"; string clientId = "your-client-id"; string clientSecret = "your-client-secret"; // Authenticate with SharePoint using the app registration string authorityUrl = "https://login.microsoftonline.com/yourtenant.onmicrosoft.com"; string resourceUrl = "https://yourtenant.sharepoint.com"; string apiUrl = "https://yourtenant.sharepoint.com/_api/web"; AuthenticationContext authContext = new AuthenticationContext(authorityUrl); ClientCredential credentials = new ClientCredential(clientId, clientSecret); AuthenticationResult authResult = await authContext.AcquireTokenAsync(resourceUrl, credentials); // Call the SharePoint REST API to get the document HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken); HttpResponseMessage response = await client.GetAsync("https://yourtenant.sharepoint.com/sites/your-site/_api/web/GetFileByServerRelativeUrl('/sites/your-site/Shared%20Documents/your-excel-file.xlsx')/$value"); string fileContent = await response.Content.ReadAsStringAsync(); 
  1. Go to the SharePoint Online Admin Center and sign in as a Global Administrator.
  2. Select the "App Catalog" option from the left navigation menu.
  3. Click on the "Apps for SharePoint" option and select "New" to create a new app.
  4. In the "Add an App" dialog box, choose "Provider-hosted" and provide a title and description for the app.
  5. Choose a website URL for the app, which will be used for the remote web application.
  6. In the "Client ID" section, click on the "Generate" button to generate a new client ID for your app.
  7. Save the generated client ID and click on the "Generate" button in the "Client Secret" section to generate a new client secret for your app.
  8. Save the generated client secret in a secure location as you will not be able to view it again.

Once you have generated the clientId and clientSecret, you can use them in your C# code to authenticate with SharePoint and access the Excel document without requiring the user's credentials.

Please, if this answer is helpful, click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please let me know.

1 comment Show comments for this answer Report a concern Saurabh Singh 0 Reputation points 2023-09-09T06:00:10.1+00:00

Can we access the customer's SharePoint documents using the above approach from the cloud application? won't we get the cross domain issue? or we have to implement some Auth0 token or Azure AD application proxy connector. My requirement is to access the documents stored in specific folder at customer SharePoint