Refactor ClientContext execution flow in list item commands to ensure proper loading and updating of items#5354
Merged
Merged
Conversation
… proper loading and updating of items
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes the CSOM “object is used in a context different from the one associated with the object” failure (Issue #5348) by ensuring cmdlets don’t leave pending CSOM requests on the shared ClientContext, which previously caused the next cmdlet invocation to refresh/clone the context and invalidate passed-in CSOM objects (e.g., -List from Get-PnPList).
Changes:
- Reorders
Load()/ExecuteQueryRetry()in list item update cmdlets so the finalLoad(item)is actually executed and no pending request is left behind. - Adds a safety drain in
PnPSharePointCmdlet.EndProcessing()to execute any remaining pending CSOM request before the cmdlet ends. - Adjusts
Get-PnPTenantRestrictedSearchModeoutput behavior (note: current change may inadvertently convert a typed value to string).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Commands/Lists/SetListItem.cs | Executes the queued Load(item) by moving ExecuteQueryRetry() after Load(item) to avoid leaving a pending request. |
| src/Commands/Lists/SetImageListItem.cs | Same Load(item)/ExecuteQueryRetry() ordering fix for image list item updates. |
| src/Commands/Base/PnPSharePointCmdlet.cs | Drains ClientContext pending requests during EndProcessing() to prevent context refresh/clone on subsequent cmdlets. |
| src/Commands/Admin/GetTenantRestrictedSearchMode.cs | Changes how restricted search mode is written to the pipeline (currently outputs a string). |
Comment on lines
15
to
18
| var results = Tenant.GetSPORestrictedSearchMode(); | ||
| AdminContext.ExecuteQueryRetry(); | ||
| WriteObject(results, true); | ||
| WriteObject(results.Value.ToString()); | ||
| } |
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.
Type
Related Issues?
Fixes #5348
What is in this Pull Request ?
Drain clientcontext after processing