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

Time:02-03

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

CodePudding user response:

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

  • Related