how to resolve a user's baseURL for sharepoint for filepicker

Jonathon Love 1 Reputation point
2022-11-05T05:25:10.853+00:00

hi,

we would like to provide the ability for user's to open files from their onedrive/sharepoint in our web application.

i have got the following example working:

https://github.com/OneDrive/samples/tree/master/samples/file-picking/javascript-basic

however, the example requires you to provide the baseUrl for the user who is going to be interacting. that's fine for testing purposes, i can just enter the baseUrl that corresponds to my organisation ... i.e. https://xxxxxxx-my.sharepoint.com

however, this clearly won't work for anyone not in my organization. what is the correct way to do this, that will let anyone (irrespective of their organization) open files from the sharepoints they have access to?

i wondered if i'd find the user's sharepoint address somewhere inside https://graph.microsoft.com/v1.0/me but this doesn't seem to be the case.

ideally there'd be a way to determine whether the user preferred to use onedrive or sharepoint, and a way to resolve the correct url.

anyone have any ideas?

with thanks

SharePoint Development
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.
3,132 questions
{count} votes

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 36,821 Reputation points Microsoft Vendor
    2022-11-07T02:47:55.31+00:00

    Hi @Jonathon Love
    You need to create a AAD App Registration to authentication file picker. You can see to required setup in the document

    Create a new AAD App Registration, note the ID of the application
    Under authentication, create a new Single-page application registry
    Set the redirect uri to https://localhost (this is for testing the samples)
    Ensure both Access tokens and ID tokens are checked
    You may optionally configure this application for multitenant but this is outside the scope of this article
    Under API permissions
    Add Files.Read.All, Sites.Read.All, Leave User.Read for Graph delegated permissions
    Add AllSites.Read, MyFiles.Read for SharePoint delegated permissions

    For more details, please refer to the document
    https://learn.microsoft.com/en-us/onedrive/developer/controls/file-pickers/?view=odsp-graph-online


    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.



  2. Batdorj Jargalsaikhan 5 Reputation points
    2024-10-24T03:49:35.4033333+00:00

    I found a rough solution using this MS Graph client: https://learn.microsoft.com/en-us/graph/sdks/create-client?tabs=csharp after you initializing the Client, you can request the baseUrl from the following api:

    client.api('/organization').get();
    

    https://learn.microsoft.com/en-us/graph/api/organization-get?view=graph-rest-1.0&tabs=http retrieve the orgName from the get request, from the key: verifiedDomains[0].name. And split the .com or whatever the domain suffix is, and you can attach it to the https://xxxxxxx-my.sharepoint.com url. I am assuming there is a way better solution for this. But in the meantime, this is the best I could find.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.