How can I get the siteId of the current site using the Microsoft Graph API?
From the documentation, I cannot find any ways to get the siteId of the site where I hosted the webpage.
For example,
My current site is: https://{hostname}/sites/main1
<- NOT the root site, but I want to get this siteId
and I am testing my website here: https://{hostname}/sites/main1/_layouts/15/workbench.aspx
How can I achieve this? From the documentation,
The site destination is a unique identifier that is a composite identifier of the following values:
Site hostname (contoso.sharepoint.com)
Site collection unique identifier (guid)
Unique site identifier (guid)
I can easily get the hostname using location.hostname
(Yes, I am using JavaScript + React to build my website), but how can I easily get the site ID using the Graph APIs?
source to share
Try this: https://graph.microsoft.com/v1.0/sites/{hostname}:/sites/{path}?$select=id
For example:
https://graph.microsoft.com/v1.0/sites/cie493742.sharepoint.com:/sites/Contoso/Operations/Manufacturing?$select=id
(This example can be tried in the graph explorer.
What will you return the id in this format:
{hostname},{spsite.id},{spweb.id}.
See docs link for details: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/site_get
source to share
You don't need to make a Graph API call to get the site id of the current site. This is available in PageContext .
In the main class of your site, you can find it at:
this.context.pageContext.site.id
source to share