Enhance context management by adding methods to refresh and clone ClientContext, ensuring proper handling of pending requests.#5308
Merged
gautamdsheth merged 3 commits intodevfrom May 5, 2026
Conversation
…entContext, ensuring proper handling of pending requests.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the SharePoint connection/context plumbing to proactively handle leftover pending CSOM requests by refreshing (cloning) the ClientContext before executing most SharePoint cmdlets, with an opt-out for Invoke-PnPQuery.
Changes:
- Add a base-cmdlet hook (
ShouldRefreshContextWithPendingRequest) and refresh the connection context duringBeginProcessingwhen pending CSOM requests are detected. - Introduce
PnPConnection.RefreshContext*helpers plus cache replacement logic, and ensureCloneContext()clones if a cached context has pending requests. - Disable the auto-refresh behavior for
Invoke-PnPQueryso it can execute pending CSOM requests.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Commands/Base/PnPSharePointCmdlet.cs | Refreshes the connection ClientContext at cmdlet start when pending requests exist; adds an overridable hook. |
| src/Commands/Base/PnPConnection.cs | Adds context refresh/clone helpers and updates context cache behavior to handle pending requests. |
| src/Commands/Base/InvokeQuery.cs | Opts out of context refresh so pending CSOM requests can be executed. |
…nd adding ShouldRefreshContextWithPendingRequest property to manage pending CSOM requests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before creating a pull request, make sure that you have read the contribution file located at
https://github.com/pnp/powerShell/blob/dev/CONTRIBUTING.md
Type
Related Issues?
Fixes #X, partially fixes #Y, mentioned in #Z, etc.
What is in this Pull Request ?
Please describe the changes in the PR.
Guidance