Get Sharepoint "Site URL", when you have a "Site Id"

Mantas Antanaitis 5 Reputation points
2023-10-11T06:54:28.6966667+00:00

Hello,

How do I get a Sharepoint Site URL when I have a Site Id using powershell&graphAPI? I'm using an app registration to autohorize.

This is a part of powershell script:

$CustomerGraphToken = New-PartnerAccessToken -ApplicationId $ApplicationId -Credential $credential -RefreshToken $refreshtoken -Scopes 'https://graph.microsoft.com/.default' -ServicePrincipal -Tenant $customer.TenantID
$Header = @{
	Authorization = "Bearer $($CustomerGraphToken.AccessToken)"
}
$SharepointURI = "https://graph.microsoft.com/v1.0/reports/getSharePointSiteUsageDetail(period='D7')"

$spoResult = (Invoke-RestMethod -Headers $Header -Uri $SharepointURI -Method Get -ContentType "application/json; charset=utf-8") | ConvertFrom-Csv

This gives me a correct result. The problem is that almost every site's "Site URL" field are empty.
That result gives every site's "Site Id" though. So I believe I should be able to use "Site Id" to get an information about Sharepoint Site with a different Graph API URI.

I tried using this Graph API URI "https://graph.microsoft.com/v1.0/sites/{site-id}"
The app registration does has a permission of "Sites.Read.All, Sites.ReadWrite.All" (type Application), but I get an error "(401) Unautohrized".

What am I doing wrong and why some of Graph API URI's work and some say "Unauthorized" even though the permissions are correct?

Thanks.

Windows for business Windows Server User experience PowerShell
Microsoft Security Microsoft Graph
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Eliot Cole 71 Reputation points
    2024-02-14T14:14:34.0666667+00:00

    This probably isn't the right answer to your question as you are powershell focused and specifically looking at using Microsoft Graph ... however, if you use the SharePoint API you can get the URL of a site using:

    https://TENANT_SUBDOMAIN.sharepoint.com/_api/SP.Site.GetUrlById(guid'SITE_ID')
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.