sharepointgraphsharepoint-onlineonedrivecsom

Read/Download contents from shared link to a folder in SharePoint/Onedrive for Business using C#


Background: I have a requirement where I need to download files from a SharePoint folder link which is public and anyone can view it in web browser. This will act as windows update application which will update existing files on Filesystem from sharepoint/onedrive.

enter image description here

I can successfully download file if a link is to a file using httpWebRequest calls.

Problem: I need to find a solution which can download files within the shared folder. Preferably without any user authentication as link would be public

I saw Graph solution below that could give me details of shared items but this would need azure app for authentication whereas I need something transparent and minimal user intervention. https://learn.microsoft.com/en-us/graph/api/shares-get?view=graph-rest-1.0&tabs=csharp#code-try-2

Essentially, I only need to get relative path of the items inside shared folder which I can use to download by using same FedAuth cookie that is generated when shared link is opened.

Any ideas would be greatly appreciated. Thanks in Advance


Solution

  • Finally, I solved this issue using REST calls with authentication headers generated after executing web request call to the shared link.

    Code steps

    1. Web Request call to shared link generates 'Location' response header which consists of 'id' and 'parent' parameters. id = relative url of file\folder parent = relative url of folder (if shared link is of file)

    2. Use REST api to get files/folders using above folder relative url and authentication cookies generated in step 1.

    3. using server relative url of files generated in step 2 we can download content.