Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Commands/Base/InvokeQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace PnP.PowerShell.Commands.Base
[OutputType(typeof(void))]
public class InvokeQuery : PnPSharePointCmdlet
{
protected override bool ShouldRefreshContextWithPendingRequest => false;

[Parameter(Mandatory = false)]
public int RetryCount = 10;

Expand Down
54 changes: 50 additions & 4 deletions src/Commands/Base/PnPConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ private PnPConnection(ClientContext context,

PSCredential = credential;
PnPVersionTag = pnpVersionTag;
ContextCache = new List<ClientContext> { context };
ContextCache = context != null ? new List<ClientContext> { context } : new List<ClientContext>();
if (!string.IsNullOrEmpty(url))
{
Url = new Uri(url).AbsoluteUri;
Expand All @@ -821,30 +821,76 @@ private PnPConnection(ClientContext context,
#region Methods
internal void RestoreCachedContext(string url)
{
Context = ContextCache.FirstOrDefault(c => new Uri(c.Url).AbsoluteUri == new Uri(url).AbsoluteUri);
Context = ContextCache.FirstOrDefault(c => c != null && new Uri(c.Url).AbsoluteUri == new Uri(url).AbsoluteUri);
_pnpContext = null;
}

internal void CacheContext()
{
if (Context == null) return;

var c = ContextCache.FirstOrDefault(cc => new Uri(cc.Url).AbsoluteUri == new Uri(Context.Url).AbsoluteUri);
ContextCache ??= new List<ClientContext>();
var c = ContextCache.FirstOrDefault(cc => cc != null && new Uri(cc.Url).AbsoluteUri == new Uri(Context.Url).AbsoluteUri);
if (c == null)
{
ContextCache.Add(Context);
}
}

internal bool RefreshContextIfHasPendingRequest()
{
if (Context?.HasPendingRequest != true)
{
return false;
}

RefreshContext();
return true;
}

internal void RefreshContext()
{
if (Context == null)
{
return;
}

var context = Context.Clone(Context.Url);
ReplaceCachedContext(context);

Context = context;
_pnpContext = null;
}

private static void ReplaceCachedContext(ClientContext context)
{
ContextCache ??= new List<ClientContext>();

var contextIndex = ContextCache.FindIndex(c => c != null && new Uri(c.Url).AbsoluteUri == new Uri(context.Url).AbsoluteUri);
if (contextIndex >= 0)
{
ContextCache[contextIndex] = context;
}
else
{
ContextCache.Add(context);
}
}

internal ClientContext CloneContext(string url)
{
var context = ContextCache.FirstOrDefault(c => new Uri(c.Url).AbsoluteUri == new Uri(url).AbsoluteUri);
var context = ContextCache.FirstOrDefault(c => c != null && new Uri(c.Url).AbsoluteUri == new Uri(url).AbsoluteUri);
if (context == null)
{
context = Context.Clone(url);
Comment thread
gautamdsheth marked this conversation as resolved.
context.ExecuteQueryRetry();
ContextCache.Add(context);
}
else if (context.HasPendingRequest)
{
context = context.Clone(context.Url);
ReplaceCachedContext(context);
}
_pnpContext = null;
return context;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Commands/Base/PnPSharePointCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ public abstract class PnPSharePointCmdlet : PnPConnectedCmdlet
/// </summary>
public ClientContext ClientContext => Connection?.Context;

Comment thread
gautamdsheth marked this conversation as resolved.
/// <summary>
/// Controls whether a cmdlet should replace the current SharePoint context when it already contains pending CSOM requests before the cmdlet starts.
/// Override this for cmdlets that intentionally execute caller-created pending requests.
/// </summary>
protected virtual bool ShouldRefreshContextWithPendingRequest => true;

/// <summary>
/// Reference the the PnP context on the current connection. If NULL it means there is no PnP context available on the current connection.
/// </summary>
Expand Down Expand Up @@ -128,6 +134,12 @@ protected override void BeginProcessing()
throw new InvalidOperationException(Resources.NoDefaultSharePointConnection);
}
}

if (ShouldRefreshContextWithPendingRequest && Connection.RefreshContextIfHasPendingRequest())
{
LogDebug("Refreshing the SharePoint context because it contained pending CSOM requests before cmdlet execution.");
}
Comment thread
gautamdsheth marked this conversation as resolved.

var resourceUri = new Uri(Connection.Url);
var defaultResource = $"{resourceUri.Scheme}://{resourceUri.Authority}/.default";
SharePointRequestHelper = new ApiRequestHelper(GetType(), Connection, defaultResource);
Expand Down
Loading