you can use sharepoint csom when you want to connect sharepoint by C#.
you can refer the below link, and I think it's easy to you.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
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 ?
Best regards,
Pierre
you can use sharepoint csom when you want to connect sharepoint by C#.
you can refer the below link, and I think it's easy to you.
Hello Raytheon Xie,
First of all, thank you for your quick answer. Unfortunately, even if the piece of code you provided runs, I am unable to extract the correct data from it.
By this, I mean that i can load the steam in any library for reading .xlsx file, but the result is an empty sheet, with the default's .xlsx's sheet's name "Sheet1", and no data in it.
I also tried with a word document, and also a .txt document with similar result: code runs, but the actual data i want to to read is inexistant. For the latter documents, i get back a ton of html, but without any of the actual text in the corps, in the whole string.
Could you maybe help me further, since I am running out of options.
I stay alert.
Best regards, Pierre
Hi @Pierre Velluet
Per my test, I access sharepoint document by following code without 403 error
var uri = $"https://xxx.sharepoint.com/:w:/s/xiexin/EQVkdVfmxC5Fgthsy0WWMYEBrDoTDPOg09mf_geU9NOpWQ?e=mmcwaE";
var handler = new HttpClientHandler { AllowAutoRedirect = false, UseCookies = true };
while (true)
{
var request = new HttpRequestMessage(HttpMethod.Post, uri);
var response = new HttpClient(handler).SendAsync(request).ConfigureAwait(false).GetAwaiter().GetResult();
var statusCode = (int)response.StatusCode;
if (statusCode >= 300 && statusCode <= 399)
{
var redirectUri = response.Headers.Location;
if (!redirectUri.IsAbsoluteUri)
{
redirectUri = new Uri(request.RequestUri.GetLeftPart(UriPartial.Authority) + redirectUri);
}
uri = redirectUri.AbsoluteUri;
continue;
}
if (response.IsSuccessStatusCode)
{
var data = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
}
}
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.