[BUG] Unsuccessful operation on a SPO site changes PnP context URL to admin site
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 904
- Forks
- 407
- Avg merge
- 9h 16m
- Merged PRs (30d)
- 10
Description
Reporting an Issue or Missing Feature
I am trying to reuse an existing SPO session to access multiple sites. When I change the URL in the connection object, I can access a different SPO site without having to re-authenticate. However, when one command fails, PnP PowerShell automatically changes the PnP Context URL back to the URL of the admin center.
Expected behavior
The URL in the PnP connection object should not change back to the admin SPO site if command exeuction fails.
Actual behavior
As soon as one command execution fails, the connection object changes the destination URL back to the SharePoint admin site. Further commands are no longer executed on the target SPO site but instead on the admin site.
Steps to reproduce behavior
PS >$conobj = Connect-PnPOnline -Url 'https://contoso-admin.sharepoint.com' -ClientId 'b929ffe0-5843-4582-96f6-a3cce2fac393' -ReturnConnection:$true
PS >get-pnpcontext -Connection $conobj | Select-Object -Property "Url"
Url
---
https://contoso-admin.sharepoint.com/
PS >$temp_context = [Microsoft.SharePoint.Client.ClientContextExtensions]::Clone($(get-pnpcontext -Connection $conobj),"https://contoso.sharepoint.com/sites/ThisSiteDoesNotExist")
PS >set-pnpcontext -Context $temp_context -connection $conobj
PS >get-pnpcontext -Connection $conobj | Select-Object -Property "Url"
Url
---
https://contoso.sharepoint.com/sites/ThisSiteDoesNotExist
PS >get-pnpsite -connection $conobj
Get-PnPSite: Unexpected response from the server. The content type of the response is "text/plain; charset=utf-8". The status code is "NotFound".
PS >get-pnpcontext -Connection $conobj | Select-Object -Property "Url"
Url
---
https://contoso-admin.sharepoint.com/
PS >
What is the version of the Cmdlet module you are running?
3.1.0
Which operating system/environment are you running PnP PowerShell on?
- Windows
- Linux
- MacOS
- Azure Cloud Shell
- Azure Functions
- Other : please specify
If this bug report is in the wrong repository, please comment and I will close it here and re-create it in the correct repo.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the behavior with Connect-PnPOnline, Set-PnPContext, Get-PnPSite, and Get-PnPContext using the commands shown. Trace the connection/context handling around the failed Get-PnPSite call and verify that the target site URL remains unchanged after the failure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, powershell
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100