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

5 Answers5

48

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://learn.microsoft.com/en-us/graph/api/resources/sharepoint?view=graph-rest-1.0

Stephen Romero
  • 2,812
  • 4
  • 25
  • 48
Yina - MSFT
  • 1,756
  • 1
  • 12
  • 10
  • 6
    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
  • 10
    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
  • 6
    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
3

Execute the below query in Graph explorer

https://graph.microsoft.com/v1.0/sites/{tenantname}.sharepoint.com:/sites/{sitename}

replace the tenantname and sitename placeholders.

The key 'Id' in the JSON response holds the site Id value

2

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,586
  • 11
  • 12
2

On classic sites using spPageContextInfo:

(location.host + "," + _spPageContextInfo.siteId + "," + _spPageContextInfo.webId).replace(/[\{\}]/g, "")
rlv-dan
  • 976
  • 1
  • 10
  • 21
  • 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

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

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

Ajay Kumar
  • 4,864
  • 1
  • 41
  • 44