Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve a specific SharePoint online site using graph api and PowerShell

Am currently creating a PowerShell script to retrieve a specific SharePoint online site using Microsoft graph api. The goal is, once i retrieve the site, then i can grab the siteid. My script fails on the api call. Have tried different search and filter combination, but it's not working. All of my api calls just retrieves all the sites. I got all required permissions assigned to the app registration.

Below is the entire script.

$siteUrl = "https://bernardcomms.sharepoint.com/sites/Project1"
$tenantId = "" 
$clientId = "" 
$clientSecret = "" 

# Get an access token for the Microsoft Graph API
$tokenAuthUri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$tokenRequestBody = @{
    grant_type = "client_credentials"
    client_id = $clientId
    client_secret = $clientSecret
    scope = "https://graph.microsoft.com/.default"
}
$tokenResponse = Invoke-RestMethod -Method Post -Uri $tokenAuthUri -Body $tokenRequestBody
$accessToken = $tokenResponse.access_token

# Get the ID of the SharePoint site
$requesturi = "https://graph.microsoft.com/v1.0/sites?$search=$siteUrl"
$siteIdResponse = Invoke-RestMethod -Method Get -Uri $requesturi -Headers @{Authorization=("bearer {0}" -f $accessToken)}

$siteId = $siteIdResponse.value.id
$siteId

Have tried to use below calls but it just returns all the sites.

$requesturi = "https://graph.microsoft.com/v1.0/sites?$search=weburl eq '$siteUrl'"
$requesturi = "https://graph.microsoft.com/v1.0/sites?$filter=weburl eq '$siteUrl'"

How can I retrieve a site that has a matching specific siteurl.

permissions assigned. enter image description here

like image 901
Bernietechy Avatar asked Oct 17 '25 14:10

Bernietechy


2 Answers

I agree with @Cpt.Whale, you need to change your graph query to https://graph.microsoft.com/v1.0/sites/bernardcomms.sharepoint.com:/sites/Project1

I tried to reproduce the same in my environment and got below results:

I registered one Azure AD application and granted same API permissions like below:

enter image description here

I have one SharePoint site named sritestsite like below:

enter image description here

When I ran below PowerShell script by making few changes, I got siteID successfully like below:

$hostName = "mytenant.sharepoint.com"
$siteName = "sitename"
$tenantId = <tenantID> 
$clientId = <appID> 
$clientSecret = <secret> 

# Get an access token for the Microsoft Graph API
$tokenAuthUri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$tokenRequestBody = @{
    grant_type = "client_credentials"
    client_id = $clientId
    client_secret = $clientSecret
    scope = "https://graph.microsoft.com/.default"
}
$tokenResponse = Invoke-RestMethod -Method Post -Uri $tokenAuthUri -Body $tokenRequestBody
$accessToken = $tokenResponse.access_token

# Get the ID of the SharePoint site
$requesturi = "https://graph.microsoft.com/v1.0/sites/$hostName`:/sites/$siteName"
$siteIdResponse = Invoke-RestMethod -Method Get -Uri $requesturi -Headers @{Authorization=("bearer {0}" -f $accessToken)}

$siteId = $siteIdResponse.id
$siteId

Response:

enter image description here

To get full response, you can run $siteIdResponse like below:

enter image description here

like image 100
Sridevi Avatar answered Oct 19 '25 06:10

Sridevi


simplifying a bit, the code goes like this (using the graph query as @Sridevi and @Cpt.Whale proposed)

This way, you can filter the site's collection ID and the top-level site's ID (created by default associated with the site collection of course)

PowerShell

Import-Module Microsoft.Graph.Sites

Connect-MgGraph -Scopes "Sites.ReadWrite.All"

$siteName = "SiteName"

$requestUrl = "https://graph.microsoft.com/v1.0/sites/mytenant.sharepoint.com:/sites/$siteName "

$site = Invoke-MgGraphRequest -Method GET -Uri $requestUrl
$siteId = $site.Id

$siteIdComponents = $siteId.Split(',')
$tenantName = $siteIdComponents[0]
$siteCollectionId = $siteIdComponents[1]
$TopLevelSiteId = $siteIdComponents[2]

Write-Output "Tenant Name: $tenantName `nSite Collection ID: $siteCollectionId `nTop Level Site ID: $TopLevelSiteId"

Response:

Tenant Name: mytenant.sharepoint.com
Site Collection ID: 1234-3456-5678-mhgfd
Top Level Site ID: 112iu3-45dfvv-4324cf-334-x323
like image 40
lQ.r3m Avatar answered Oct 19 '25 06:10

lQ.r3m



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!