Connect Azure Service Principal to Specific M365 SharePoint Online Site
Hello. Working with Azure and M365, we have an ask to have a new Service Principal (App Registration) that was created in Azure access to read the contents from a Specific SharePoint Site List. The SPO module has been installed via PowerShell
The App Registration has been given the API permission - SharePointTenantSettings.Read.All
We would like to tie this App Registration to a specific site. Been looking and referencing MS's documentation, trying, https://learn.microsoft.com/en-us/powershell/module/sharepoint-server/set-spappprincipalpermission?view=sharepoint-server-ps and another site which I cannot find.
Here is the syntax the articles recommended:
$clientID = "11111111-2222-3333-4444-555555555555"
$site = Get-SPSite https://siteUrl
$realm = Get-SPAuthenticationRealm -ServiceContext $site
$appIdentifier = $clientID + "@" + $realm $appPrincipal = Get-SPAppPrincipal -NameIdentifier $appIdentifier -Site $site.RootWeb
Set-AppPrincipalPermission -AppPrincipal $appPrincipal -Site $site.RootWeb -Scope Site -Level Manage
Here are the errors:
Get-SPAuthenticationRealm : The term 'Get-SPAuthenticationRealm' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At line:1 char:10
+ $realm = Get-SPAuthenticationRealm -ServiceContext $site
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-SPAuthenticationRealm:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Using the Syntax in the Referenced MS document, there are even more syntax issues.
Also tried the following:
Grant-PnPAzureADAppSitePermission -AppId '11111111-1111-1111-1111-111111111' -DisplayName 'App Registration/Service Principal Name' -Site 'https://Site URL" -Permissions Read
Error:
Set-AppPrincipalPermission : The term 'Set-AppPrincipalPermission' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
At line:1 char:1
+ Set-AppPrincipalPermission -AppPrincipal $appPrincipal -Site $site.Ro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-AppPrincipalPermission:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Another was to reference this article, https://learn.microsoft.com/en-us/answers/questions/2116616/service-principal-access-to-sharepoint-online
The recommended syntax in the article:
Grant-SPOSiteDesignRights -Identity "[Site URL]" -Principals "[App ID]" -Rights View
I used the name and the GUID to try but get errors both ways.
Grant-SPOSiteDesignRights : Cannot bind parameter 'Identity'. Cannot convert value "https:/domain.sharepoint.com/sites/Siteitself/" to type
"Microsoft.Online.SharePoint.PowerShell.SPOSiteDesignPipeBind". Error: "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)."
At line:1 char:37
+ ... s -Identity "hhttps:/domain.sharepoint.com/sites/Siteitself/ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
***+ CategoryInfo : InvalidArgument: (:) [Grant-SPOSiteDesignRights], ParameterBindingException***
***+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Online.SharePoint.PowerShell.GrantSPOSiteDesignRights***
If I substitute the ObjectID which I see in Azure:
Grant-SPOSiteDesignRights : File Not Found.
At line:1 char:1
+ Grant-SPOSiteDesignRights -Identity "11111aaa-a111a-11a1-1111-1a1aaaaa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
***+ CategoryInfo : NotSpecified: (:) [Grant-SPOSiteDesignRights], ServerException***
***+ FullyQualifiedErrorId : Microsoft.SharePoint.Client.ServerException,Microsoft.Online.SharePoint.PowerShell.GrantSPOSiteDesignRights***
Seems the issue is I am not finding a good reference for M365 SharePoint Online and some of these articles are intended for on-premise versions of SharePoint.
Does anyone have a good reference for the syntax to be able to assign the App Registration to the specific site in M365 (SharePoint Online)? You assistance is appreciated. Thanks in advance as this is holding up a project.