There's no way for an application with Sites.Selected permissions to get this info. The code above will only work if you have the necessary permissions (Sites.Read.All at minimum), which your app does not. Moreover, if your app is running in the context of a user (delegate permissions), the /sites query will not work altogether - it's only supported for application permissions.
Graph API - Get list of SharePoint sites with Sites.Selected
Hello:
I am trying to integrate with Sharepoint via the Microsoft Graph API. I have already set up an Azure app with Sites.Selected
permissions and configured the app to have read
access to a specific list of sites. For context, I am using raw HTTP calls to the MS graph v.1.0 API to build the integration.
The problem is that I can access the files/folders of a site provided that the user manually inputs the ID of said site, but there is no way to list all the sites that my application has access to.
I have already tried the solution proposed in this thread:
But it does not work as of october 2024, doing a call to the /sites/ endpoint returns an empty list:
# setup auth session
auth_headers = {
"Authorization": f"Bearer {access_token}",
}
session = requests.Session()
session.headers.update(auth_headers)
# list all sites
sharepoint_site_list_endpoint = "https://graph.microsoft.com/v1.0/sites"
response = session.get(sharepoint_site_list_endpoint)
sites_list = response.json()["value"]
print(f"Got {len(sites_list)} sites") # this always returns 0
Even if the permissions expansion is added to the query params.
# setup auth session
auth_headers = {
"Authorization": f"Bearer {access_token}",
}
session = requests.Session()
session.headers.update(auth_headers)
# list all sites
sharepoint_site_list_endpoint =
response = session.get(sharepoint_site_list_endpoint)
sites_list = response.json()["value"]
print(f"Got {len(sites_list)} sites") # this prints returns 0
It seems like there is no way to get a list of sites that the application has access to in order to display them to the end user, which is not ideal, since getting the site ID of a sharepoint site is not an easy process.
Given the above, my questions are:
- Are there other ways of finding out which sites my app has access to?
- Is it possible to setup another permission to be able to list Sharepoint sites without having access to its contents?
2 answers
Sort by: Most helpful
-
-
Yanli Jiang - MSFT 27,086 Reputation points Microsoft Vendor
2024-10-09T09:38:27.6466667+00:00 Hi @Manuel Suarez ,
If you want to get all the files accessible under this app, unfortunately, there is no such API yet.
Good day!
If the answer is helpful, please click "Accept as 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.