powershell: Write access to term store using App-only Service Principal[BUG]
Reporting an Issue or Missing Feature
When doing write operations on Term Store using PnP cmdlet I am getting access denied when using a SharePoint App-only service principal that has Taxonomy write (and also tenant admin) permissions.
I am seeing this issue on both SharePointPnPPowerShellOnline and PnP.PowerShell
The permissions for everything else works, so permissions are not the issue here.
Eg. i can do a Set-PnPTenant -SharingCapability Disabled without getting errors.
When I use a cloud-id user instead and give that user Termstore Administrator permissions everything works as expected (as long as I connect to root of tenant, not admin), so issue is only for App-only service principals.
I also have tried any combination of setting permissions using AppPermissionRequests:
- Setting both tenant scope FullControl and taxonomy Write, granted from admin site
- Setting only taxonomy Write, granted from admin site
- Setting both site collection scope FullControl and taxonomy Write, granted from root of tenant
- Setting only taxonomy Write, granted from root of tenant
Expected behavior
It should be possible to update term store with a service principal.
Actual behavior
All cmdlets that do write operations fail. Here are an example where I try to import term set using CSV file
# here using root of tenant, but also tried with tenant-admin url
$connection = Connect-PnPOnline -ReturnConnection -Url "https://tenant.sharepoint.com" -ClientSecret $clientSecret -ClientId $clientId
Import-PnPTermSet -GroupName Organisation -Path .\test.csv -SynchronizeDeletions -Connection $connection
Example of exception (this one from PnP.PowerShell but getting same error on old framework)
> $Error[0] | fl * -force
PSMessageDetails :
Exception : System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction,
Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location ---
at Microsoft.SharePoint.Client.SPWebRequestExecutor.ExecuteAsync()
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServerAsync(ChunkStringBuilder sb)
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryAsync()
at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQueryAsync()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQueryAsync()
at
Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryImplementation(ClientRuntimeContext
clientContext, Int32 retryCount, String userAgent)
at
Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryImplementation(ClientRuntimeContext
clientContext, Int32 retryCount, String userAgent)
at Microsoft.SharePoint.Client.ClientContextExtensions.ExecuteQueryRetry(ClientRuntimeContext
clientContext, Int32 retryCount, String userAgent)
at PnP.PowerShell.Commands.Base.PnPConnection.CloneContext(String url) in
D:\a\powershell\powershell\src\Commands\Base\PnPConnection.cs:line 653
at PnP.PowerShell.Commands.Base.PnPAdminCmdlet.BeginProcessing() in
D:\a\powershell\powershell\src\Commands\Base\PnPAdminCmdlet.cs:line 76
at System.Management.Automation.Cmdlet.DoBeginProcessing()
at System.Management.Automation.CommandProcessorBase.DoBegin()
TargetObject :
CategoryInfo : NotSpecified: (:) [Get-PnPTenant], WebException
FullyQualifiedErrorId : System.Net.WebException,PnP.PowerShell.Commands.Admin.GetTenant
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}
Steps to reproduce behavior
Create an app-only service principal using https://tenant.sharepoint.com/_layouts/15/appregnew.aspx Assign and grant Taxonomy Write permissions (and more) on either https://tenant.sharepoint.com/_layouts/15/appinv.aspx or https://tenant.sharepoint.com/_layouts/15/appinv.aspx depending on access
Connect to either admin or root of tenant with Connect-PnPOnline using ClientID/-secret Try and Set-PnPTermSet, Import-PnPTermSet or any write operation and it will fail.
What is the version of the Cmdlet module you are running?
PnP.PowerShell 0.3.25 SharePointPnPPowerShellOnline 3.28.2012.0
Which operating system/environment are you running PnP PowerShell on?
- [X ] Windows
- Linux
- MacOS
- Azure Cloud Shell
- Azure Functions
- Other : please specify
Tried with both PowerShell 7.1, 7.1.1 and 5.1
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 28 (15 by maintainers)
And actually this is resolved š Apparently, term store is a separate permission section in App Registration
Sorry for bothering!
Iām having the same issue with a āmanaged identityā from within Azure Function (PowerShell). Within my PowerShell, I can connect to the ADMIN site - and create a new site. And also - add a list/content type, and add a SPFeature.
This is using Connect-PnPOnline $siteUrl -ManagedIdentity
Iāve included āSites.FullControl.Allā and also āTermStore.ReadWrite.Allā But - I can āreadā from TermStore - but get access denied for āNew-PnPTermā.
YES - Iāve added app@sharepoint - but it still doesnāt work.
š¤ Iād let all devs create their own certificate, @tleclaire. You can upload all these CER files to Azure AD anyway (see blog) no reason to share it.
Hi @tleclaire, correct, for dev purposes you need another way of authentication.
I was kind of triggered by your remarks, and I can understand that the total picture can get pretty unclear. So I quickly wrote a blog post on how to loop all these things together:
https://www.blimped.nl/running-dotnet-function-apps-or-app-services-accessing-microsoft-365/
Iām interested to know what you think of it!
Feel free to ask again if youāre not getting it running!
Thanks, Iāll check that out, very much appreciated.
We have noticed a similar thing, it seems that MS has changed something behind the scenes, but weāre still investigating. What you can do is the following:
navigate to the classic termstore administration page. Then add the following user as an administrator
app@sharepointnotice, not
app@sharepoint.com. After that it should work again. In the mean time we will keep investigating.