Skip to content

Refactor ClientContext execution flow in list item commands to ensure proper loading and updating of items#5354

Merged
gautamdsheth merged 1 commit into
devfrom
fix/5348-fix-regression
Jun 13, 2026
Merged

Refactor ClientContext execution flow in list item commands to ensure proper loading and updating of items#5354
gautamdsheth merged 1 commit into
devfrom
fix/5348-fix-regression

Conversation

@gautamdsheth

Copy link
Copy Markdown
Collaborator

Type

  • Bug Fix
  • New Feature
  • Sample

Related Issues?

Fixes #5348

What is in this Pull Request ?

Drain clientcontext after processing

Copilot AI review requested due to automatic review settings June 13, 2026 19:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 final Load(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-PnPTenantRestrictedSearchMode output 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());
}
@gautamdsheth gautamdsheth merged commit 8f1fc02 into dev Jun 13, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Set-PnPListItem fails when updating list items when using -List from Get-PnPList

2 participants