Access sharepoint document in c# application

Pierre Velluet 1 Reputation point
2022-11-30T07:02:17.54+00:00

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

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,872 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,270 questions
C#
C#
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,640 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    2022-12-01T02:16:57.513+00:00

    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.


    1 person found this answer helpful.

  2. Pierre Velluet 1 Reputation point
    2023-01-12T13:24:02.6433333+00:00

    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

    0 comments No comments

  3. 杨建宇 11 Reputation points MVP
    2023-02-13T16:06:19.4266667+00:00

    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.

    https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/sharepoint-net-server-csom-jsom-and-rest-api-index

    0 comments No comments