How to find {sitesId} to pass to MS Graph API call?

Summary

SharePoint related MS Graph API call requires {sitesid}, for example for the following API call we need {sitesId}

Create permission – Microsoft Graph v1.0 | Microsoft Docs

POST /sites/{sitesId}/permissions

Solution

Login to your SharePoint site. Go to Graph Explorer, https://aka.ms/GE, enter the following request call, assuming tenant as m365x162783 and site as Test1.

https://graph.microsoft.com/v1.0/sites/m365x162783.sharepoint.com:/sites/Test1?$select=id

You will get a response something like the following.

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites(id)/$entity",
    "id": "m365x162783.sharepoint.com,2e0ad342-19b2-4116-b6a5-2dc4867cb498,0efde304-9646-43d8-8eee-5b1a55ab17f1"
}

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

{hostname},{spsite.id},{spweb.id}
The Graph Explorer request-response

Conclusion

This is a simple way to get this sites-id information for any future MS Graph API calls if you may want to explore using PostMan or RESTClient.

However, if you want to make this call in your code you will need an access token and make a similar call as above to get the information.

$tenantPrefix = "m365x162783";
$clientId = "Client-ID";
$clientSecret = "Client-Secret";
$tenantName = $tenantPrefix +".onmicrosoft.com";
$tenantDomain = $tenantPrefix +".sharepoint.com";

#Provide site url
$sitePath = "https://m365x162783.sharepoint.com/sites/Test1"
$siteName = $sitePath.Split("/")[4]

# use splatting
$ReqTokenBody = @{
Grant_Type    = "client_credentials"
Scope         = "https://graph.microsoft.com/.default"
client_Id     = $clientID
Client_Secret = $clientSecret
} 
$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody

$apiUrl = 'https://graph.microsoft.com/v1.0/sites/'+ $tenantDomain +':/sites/'+ $siteName +'?$select=id,displayName'
try {
  $spoResult = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri  $apiUrl -Method Get 
  Write-Host "Site:" $spoResult.displayName
}
catch {
  Write-Output "Failed to enumerate the site"
  Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
  Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription 
  Exit
}    

# NOTE THE SITES-ID is passed for the next call.
$apiUrl = 'https://graph.microsoft.com/v1.0/sites/'+ $spoResult.id +'/permissions'

About Pankaj

I am a Developer and my linked profile is https://www.linkedin.com/in/pankajsurti/
This entry was posted in MS Graph, SharePoint. Bookmark the permalink.

3 Responses to How to find {sitesId} to pass to MS Graph API call?

  1. $apiUrl = ‘https://graph.microsoft.com/v1.0/sites/’+ $spoResult.id +’/permissions’
    when i have used this i am getting the 403 error

    • Pankaj says:

      This article was about finding site-id value. The permission call was an example, you are getting 403 because that call requires an access token.

  2. BA1968 says:

    Thanks for the post. I have a SharePoint site in the EMEA region and used (for example) m365x162783emea.sharepoint.com to get the sitesID. However, when I try to assign permissions, I get an “Item not found” error for this EMEA SharePoint site. My “North America” sites do not generate an error.

    Is there a limitation to the Graph API for EMEA sites?

Leave a reply to Pankaj Cancel reply