2

I've got a URL to a OneDrive folder (https://1drv.ms/f/s!AtXoQFW327DIyMwPjZhmauUCSSHXUA). Everyone with that link can access the folder via browser.

Now, my goal is to create an .NET application that, given that link, is able to get a list of the files/folders inside that folder.

Is that even possible?

3
  • We do not need to download files, right? Core Package 1.0.0 (old).zip and Core Package 1.1.0.zip are enough?
    – isydmr
    Sep 1, 2018 at 10:44
  • I'm implementing an "update checker", so first I need to list the contents to discover which files are inside. After I know the latest version, I will need to download it (to disk).
    – SuperJMN
    Sep 1, 2018 at 10:53
  • Yes :) and I intend to use C# to create the app.
    – SuperJMN
    Sep 1, 2018 at 11:00

2 Answers 2

2

The best way to do this is to use the OneDrive API exposed via Graph.

You can read the "Using Sharing Links" documentation for full details, but you'd essentially make a call to:

https://graph.microsoft.com/v1.0/shares/u!aHR0cHM6Ly8xZHJ2Lm1zL2YvcyFBdFhvUUZXMzI3REl5TXdQalpobWF1VUNTU0hYVUE/driveItem/children

You can also use the .NET SDK to avoid making the calls to the API yourself, in which case your code would look something like:

client.Shares["u!aHR0cHM6Ly8xZHJ2Lm1zL2YvcyFBdFhvUUZXMzI3REl5TXdQalpobWF1VUNTU0hYVUE"].DriveItem.Children.Request().GetAsync();
3
  • Hey Brad! I'm interested in the second option. What's client? What class library are you using? A specific NuGet Package? I would be really thankful if you shared a bit more code :) Thanks in advance!
    – SuperJMN
    Sep 1, 2018 at 18:06
  • 1
    The nuget package is Microsoft.Graph and the client I referred to is a GraphServiceClient. I didn't show it because the constructor is unfortunately a little complicated (you need to have an auth provider). Have a look at the samples with the SDK for some examples.
    – Brad
    Sep 2, 2018 at 0:40
  • Is there a way to achieve this without having to authenticate user accessing the link. Ideally, user opening the link does'nt need to have any accounts. I achieved this by using c# HttpWebRequest and able to read shared filename and download. I am struggling to do same if link is a folder. I need a way to read and download folder contents. Links in question are anonymous and anyone can view shared items.
    – Mani
    Oct 6, 2022 at 1:26
0

Selenium Web Driver is good option for tasks like that.

  1. Open Solution Explorer.
  2. Right Click on your project.
  3. Select Manage NuGet Packages..
  4. Browse and install these two : Selenium.Chrome.WebDriver and Selenium.WebDriver.
  5. You have just installed selenium to your project!

So now, we need to create a driver service, and find needed elements in our website. As far as i see, filenames are stored as a span class named signalFieldValue_03700093.

But "Last Modified infos" are stored under this class too, i needed to skip "Last Modified infos" using the code below:

        bool skip = false;
        List<string> myFiles = new List<string>();

        ChromeDriverService service = ChromeDriverService.CreateDefaultService();
        ChromeOptions option = new ChromeOptions();
        var driver = new ChromeDriver(service, option);

        driver.Url = "https://1drv.ms/f/s!AtXoQFW327DIyMwPjZhmauUCSSHXUA";
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

        foreach (IWebElement element in driver.FindElements(By.XPath("//span[@class='signalFieldValue_03700093']")))
        {
            if (!skip)
            {
                myFiles.Add(element.Text);
                skip = true;
            }
            else
                skip = false;
        }

As a result, we have our filenames in string array named myFiles.

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.