11

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?

PCHC
  • 177
  • 1
  • 2
  • 9

4 Answers4

43

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 one you can try on the Graph Explorer.

What you get back in the id is in this format:

{hostname},{spsite.id},{spweb.id}

For more info here is the link to the docs: https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/site_get

user124114
  • 7,597
  • 9
  • 38
  • 56
Yina - MSFT
  • 1,695
  • 1
  • 12
  • 10
  • 5
    Culmination of 3 hours of searching. [This](https://support.cloudhq.net/how-to-find-guids-of-sharepoint-sites/) was partially useful but only provides the site-id – mlhDev Jul 17 '20 at 18:50
  • 7
    There is always something kind of OFF with Microsoft documentation. They are usually vague or detailed to the point where they cannot be understood. – WouldBeNerd Sep 11 '20 at 01:02
  • 2
    I hate Microsoft specifically for documentation – TiredOfProgramming Feb 10 '21 at 18:19
  • What if path contains spaces, say, instead of Operations in the above example we have 'Operations ternary'? So the url turns out to be "https://graph.microsoft.com/v1.0/sites/cie493742.sharepoint.com:/sites/Contoso/Operations ternary/Manufacturing?$select=id", which doesn't work!! @WouldBeNerd – Khushboo Mulani Apr 14 '21 at 14:07
  • 1
    @KhushbooMulani in a url spaces are usually represented by %20 but I can't be sure that that would fix it for you. – WouldBeNerd Apr 15 '21 at 06:38
2

On classic sites using spPageContextInfo:

(location.host + "," + _spPageContextInfo.siteId + "," + _spPageContextInfo.webId).replace(/[\{\}]/g, "")
rlv-dan
  • 876
  • 1
  • 10
  • 18
  • This works on Modern Sites too. Note that you only get the right site id if you've navigated to the Site Collection in question (or one of its Site Pages) – Glenn Faison Jan 11 '22 at 09:52
1

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
RoelVB
  • 1,526
  • 11
  • 12
1

Another way you can get the Site-id by below Graph Http Get API.

https://graph.microsoft.com/v1.0/sites

Ajay Kumar
  • 4,078
  • 35
  • 37