How can I get the siteId of the current site with Microsoft Graph API?

Viewed 38334

From the documentation, I can't find any ways to get the siteId of the site where I put the webpart in.

For example,

My current site is: https://{hostname}/sites/main1 <-- NOT root site, but I want to get this siteId

and I test my webpart here: https://{hostname}/sites/main1/_layouts/15/workbench.aspx

How can I achieve this? From the documentation,

A site is addressed be a unique identifier which is a composite ID of the following values:

Site collection hostname (contoso.sharepoint.com)

Site collection unique ID (guid)

Site unique ID (guid)

I can get the hostname easily by using location.hostname (Yes, I am using JavaScript + React to build my webpart) but how to get the site-id easily with Graph API?

5 Answers

On classic sites using spPageContextInfo:

(location.host + "," + _spPageContextInfo.siteId + "," + _spPageContextInfo.webId).replace(/[\{\}]/g, "")

You don't need to do a Graph API call to get the siteId of the current site. It is available in the PageContext.

In the main class of your webpart you can find it at:

this.context.pageContext.site.id
Related