Skip to content

Commit

Permalink
Refactor File cmdlets to use Connection.PnPContext instead of PnPCont…
Browse files Browse the repository at this point in the history
…ext (#4389)

Co-authored-by: Gautam Sheth <[email protected]>
  • Loading branch information
gautamdsheth and Gautam Sheth authored Oct 5, 2024
1 parent 32677fd commit 29531d7
Show file tree
Hide file tree
Showing 33 changed files with 68 additions and 71 deletions.
7 changes: 4 additions & 3 deletions src/Commands/Apps/RemoveApplicationCustomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public class RemoveApplicationCustomizer : PnPWebCmdlet
protected override void ExecuteCmdlet()
{
var actions = new List<IUserCustomAction>();
var pnpContext = Connection.PnPContext;
if (Identity != null)
{
var rawActions = Identity.GetCustomActions(PnPContext, Scope);
var rawActions = Identity.GetCustomActions(pnpContext, Scope);

// Only take the customactions which are application customizers
actions = rawActions.Where(a => a.Location == "ClientSideExtension.ApplicationCustomizer").ToList();
Expand All @@ -42,11 +43,11 @@ protected override void ExecuteCmdlet()
{
if (Scope == CustomActionScope.Web || Scope == CustomActionScope.All)
{
actions.AddRange(PnPContext.Web.UserCustomActions.ToList());
actions.AddRange(pnpContext.Web.UserCustomActions.ToList());
}
if (Scope == CustomActionScope.Site || Scope == CustomActionScope.All)
{
actions.AddRange(PnPContext.Site.UserCustomActions.ToList());
actions.AddRange(pnpContext.Site.UserCustomActions.ToList());
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/Commands/Apps/SetApplicationCustomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public class SetApplicationCustomizer : PnPWebCmdlet
protected override void ExecuteCmdlet()
{
IEnumerable<IUserCustomAction> actions = null;
var pnpContext = Connection.PnPContext;
if (Identity != null)
{
actions = Identity.GetCustomActions(PnPContext, Scope);
actions = Identity.GetCustomActions(pnpContext, Scope);
}

else
Expand All @@ -52,11 +53,11 @@ protected override void ExecuteCmdlet()

if (Scope == CustomActionScope.Web || Scope == CustomActionScope.All)
{
customActions.AddRange(PnPContext.Web.UserCustomActions.ToList());
customActions.AddRange(pnpContext.Web.UserCustomActions.ToList());
}
if (Scope == CustomActionScope.Site || Scope == CustomActionScope.All)
{
customActions.AddRange(PnPContext.Site.UserCustomActions.ToList());
customActions.AddRange(pnpContext.Site.UserCustomActions.ToList());
}

actions = customActions.AsEnumerable();
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Base/NewBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class NewBatch : PnPWebCmdlet
public SwitchParameter RetainRequests;
protected override void ExecuteCmdlet()
{
var batch = new PnPBatch(PnPContext, RetainRequests);
var batch = new PnPBatch(Connection.PnPContext, RetainRequests);
WriteObject(batch);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Branding/RemoveCustomAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class RemoveCustomAction : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var customActions = Identity.GetCustomActions(PnPContext, Scope);
var customActions = Identity.GetCustomActions(Connection.PnPContext, Scope);
if (customActions != null && customActions.Any())
{
foreach (var customAction in customActions)
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Branding/RemoveJavaScriptLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RemoveJavaScriptLink : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var rawActions = Identity.GetCustomActions(PnPContext, Scope);
var rawActions = Identity.GetCustomActions(Connection.PnPContext, Scope);

var actions = rawActions.Where(ca => ca.Location == "ScriptLink");

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Fields/RemoveField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private void RemoveFieldBatch()
{
if (List != null)
{
var list = List.GetList(PnPContext);
var list = List.GetList(Connection.PnPContext);
list.EnsureProperties(l => l.Fields);
var fieldCollection = list.Fields.AsRequested();
var f = Identity.Field;
Expand Down
5 changes: 2 additions & 3 deletions src/Commands/InformationManagement/GetLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ protected override void ExecuteCmdlet()
{
if (!ParameterSpecified(nameof(List)))
{

var tags = PnPContext.Site.GetAvailableComplianceTags();
var tags = Connection.PnPContext.Site.GetAvailableComplianceTags();
WriteObject(tags, true);
}
else
{
var list = List.GetList(PnPContext);
var list = List.GetList(Connection.PnPContext);
if (null != list)
{
var tag = list.GetComplianceTag();
Expand Down
7 changes: 4 additions & 3 deletions src/Commands/InformationManagement/ResetRetentionLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ protected override void ExecuteCmdlet()
}
}

var list = List.GetList(PnPContext);
var pnpContext = Connection.PnPContext;
var list = List.GetList(pnpContext);
if (list != null)
{
if (ParameterSetName == ParamSet_BulkItems)
{
PnPContext.Web.LoadAsync(i => i.Url);
pnpContext.Web.LoadAsync(i => i.Url);
list.LoadAsync(i => i.RootFolder);

var rootUrl = PnPContext.Web.Url.GetLeftPart(UriPartial.Authority);
var rootUrl = pnpContext.Web.Url.GetLeftPart(UriPartial.Authority);

var rangeIndex = 0;

Expand Down
10 changes: 5 additions & 5 deletions src/Commands/InformationManagement/SetRetentionLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ protected override void ExecuteCmdlet()
return;
}
}


var list = List.GetList(PnPContext);
var availableTags = PnPContext.Site.GetAvailableComplianceTags();
var pnpContext = Connection.PnPContext;
var list = List.GetList(pnpContext);
var availableTags = pnpContext.Site.GetAvailableComplianceTags();

if (list != null)
{
Expand All @@ -61,10 +61,10 @@ protected override void ExecuteCmdlet()
{
if (ParameterSetName == ParamSet_BulkItems)
{
PnPContext.Web.LoadAsync(i => i.Url);
pnpContext.Web.LoadAsync(i => i.Url);
list.LoadAsync(i => i.RootFolder);

var rootUrl = PnPContext.Web.Url.GetLeftPart(UriPartial.Authority);
var rootUrl = pnpContext.Web.Url.GetLeftPart(UriPartial.Authority);

var rangeIndex = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Lists/AddListItemAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class AddListItemAttachment : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
IList list = List.GetList(PnPContext);
IList list = List.GetList(Connection.PnPContext);

if (list == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Lists/AddListItemComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class AddListItemComment : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var list = List.GetList(PnPContext);
var list = List.GetList(Connection.PnPContext);

if (list == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Lists/GetListItemAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class GetListItemAttachment : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
IList list = List.GetList(PnPContext);
IList list = List.GetList(Connection.PnPContext);

if (list == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Lists/GetListItemComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class GetListItemComments : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var list = List.GetList(PnPContext);
var list = List.GetList(Connection.PnPContext);

if (list == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Lists/GetListItemVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class GetListItemVersion : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var list = List.GetList(PnPContext);
var list = List.GetList(Connection.PnPContext);

if (list is null)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Commands/Lists/GetListPermissions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using PnP.Core.Model.Security;
using PnP.PowerShell.Commands.Base.PipeBinds;
Expand All @@ -18,7 +17,7 @@ public class GetListPermissions : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var list = Identity.GetListOrThrow(nameof(List), PnPContext);
var list = Identity.GetListOrThrow(nameof(Core.Model.SharePoint.IList), PnPContext);
WriteObject(list.GetRoleDefinitions(PrincipalId).RequestedItems, true);
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/Commands/Lists/MoveListItemToRecycleBin.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Management.Automation;

using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Model.SharePoint;

Expand All @@ -22,7 +19,7 @@ public class MoveListItemToRecycleBin : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var list = List.GetList(CurrentWeb);
var list = List.GetList(Connection.PnPContext);
if (list == null)
throw new PSArgumentException($"No list found with id, title or url '{List}'", "List");
if (Identity != null)
Expand All @@ -31,8 +28,7 @@ protected override void ExecuteCmdlet()
if (Force || ShouldContinue(string.Format(Properties.Resources.MoveListItemWithId0ToRecycleBin, item.Id), Properties.Resources.Confirm))
{
var recycleBinResult = item.Recycle();
ClientContext.ExecuteQueryRetry();
WriteObject(new RecycleResult { RecycleBinItemId = recycleBinResult.Value });
WriteObject(new RecycleResult { RecycleBinItemId = recycleBinResult });
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Lists/RemoveListItemAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class RemoveListItemAttachment : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
IList list = List.GetList(PnPContext);
IList list = List.GetList(Connection.PnPContext);

if (list == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Lists/RemoveListItemComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class RemoveListItemComment : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var list = List.GetList(PnPContext);
var list = List.GetList(Connection.PnPContext);

if (list == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Lists/RemoveListItemVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RemoveListItemVersion : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var list = List.GetList(PnPContext);
var list = List.GetList(Connection.PnPContext);

if (list is null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Pages/DisablePageScheduling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class DisablePageScheduling : PnPWebCmdlet
{
protected override void ExecuteCmdlet()
{
var pagesList = PagesUtility.GetModernPagesLibrary(PnPContext.Web);
Utilities.REST.RestHelper.Post(Connection.HttpClient, $"{PnPContext.Web.Url}/_api/sitepages/pagesinlib(guid'{pagesList.Id}')/setscheduling(false)", ClientContext, null, "application/json", "application/json;odata=nometadata");
var pagesList = PagesUtility.GetModernPagesLibrary(Connection.PnPContext.Web);
Utilities.REST.RestHelper.Post(Connection.HttpClient, $"{Connection.PnPContext.Web.Url}/_api/sitepages/pagesinlib(guid'{pagesList.Id}')/setscheduling(false)", ClientContext, null, "application/json", "application/json;odata=nometadata");
}
}
}
4 changes: 2 additions & 2 deletions src/Commands/Pages/EnablePageScheduling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class EnablePageScheduling : PnPWebCmdlet
{
protected override void ExecuteCmdlet()
{
var pagesList = PagesUtility.GetModernPagesLibrary(PnPContext.Web);
Utilities.REST.RestHelper.Post(Connection.HttpClient, $"{PnPContext.Web.Url}/_api/sitepages/pagesinlib(guid'{pagesList.Id}')/setscheduling(true)", ClientContext, null, "application/json", "application/json;odata=nometadata");
var pagesList = PagesUtility.GetModernPagesLibrary(Connection.PnPContext.Web);
Utilities.REST.RestHelper.Post(Connection.HttpClient, $"{Connection.PnPContext.Web.Url}/_api/sitepages/pagesinlib(guid'{pagesList.Id}')/setscheduling(true)", ClientContext, null, "application/json", "application/json;odata=nometadata");
}
}
}
4 changes: 2 additions & 2 deletions src/Commands/Pages/GetPageSchedulingEnabled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class GetPageSchedulingEnabled : PnPWebCmdlet
{
protected override void ExecuteCmdlet()
{
var pagesList = PagesUtility.GetModernPagesLibrary(PnPContext.Web);
var pagesList = PagesUtility.GetModernPagesLibrary(Connection.PnPContext.Web);
var payload = new
{
parameters = new
Expand All @@ -20,7 +20,7 @@ protected override void ExecuteCmdlet()
}
};

var results = Utilities.REST.RestHelper.Post<JsonElement>(Connection.HttpClient, $"{PnPContext.Web.Url}/_api/web/lists(guid'{pagesList.Id}')/RenderListDataAsStream", ClientContext, payload, false);
var results = Utilities.REST.RestHelper.Post<JsonElement>(Connection.HttpClient, $"{Connection.PnPContext.Web.Url}/_api/web/lists(guid'{pagesList.Id}')/RenderListDataAsStream", ClientContext, payload, false);

var frameworkClientInfo = results.GetProperty("SPFrameworkClientInfo");
var pageContextJson = frameworkClientInfo.GetProperty("PageContextJson");
Expand Down
5 changes: 3 additions & 2 deletions src/Commands/Principals/AddGroupMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ protected override void ExecuteCmdlet()
}
else
{
var group = Group.GetGroup(PnPContext);
var user = PnPContext.Web.EnsureUser(LoginName);
var pnpContext = Connection.PnPContext;
var group = Group.GetGroup(pnpContext);
var user = pnpContext.Web.EnsureUser(LoginName);

if (ParameterSetName == ParameterSet_BATCHED)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Principals/GetGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override void ExecuteCmdlet()
// Get group by name using Core SDK because of
// case sensitivity difference between Core SDK and CSOM
// Loads group using CSOM to bypass a breaking change
var pnpGroup = Identity.GetGroup(PnPContext);
var pnpGroup = Identity.GetGroup(Connection.PnPContext);

if (pnpGroup != null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Principals/GetGroupPermissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class GetGroupPermissions : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var group = Identity.GetGroup(PnPContext);
var group = Identity.GetGroup(Connection.PnPContext);

if (group == null)
throw new PSArgumentException("Site group not found", nameof(Identity));
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Principals/RemoveGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class RemoveGroup : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var group = Identity.GetGroup(PnPContext);
var group = Identity.GetGroup(Connection.PnPContext);

if (Force || ShouldContinue(string.Format(Properties.Resources.RemoveGroup0, group.Title), Properties.Resources.Confirm))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Principals/RemoveGroupMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public class RemoveUserFromGroup : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var group = Group.GetGroup(PnPContext);
var group = Group.GetGroup(Connection.PnPContext);

if (group != null)
{
try
{
var user = PnPContext.Web.EnsureUser(LoginName);
var user = Connection.PnPContext.Web.EnsureUser(LoginName);
group.RemoveUser(user.Id);
}
catch (PnP.Core.SharePointRestServiceException ex)
Expand Down
9 changes: 4 additions & 5 deletions src/Commands/Principals/SetGroupPermissions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Linq;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using System.Management.Automation;
using PnP.PowerShell.Commands.Base.PipeBinds;

namespace PnP.PowerShell.Commands.Principals
Expand All @@ -24,15 +22,16 @@ public class SetGroupPermissions : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var group = Identity.GetGroup(PnPContext);
var pnpContext = Connection.PnPContext;
var group = Identity.GetGroup(pnpContext);

if (group == null)
throw new PSArgumentException("Site group not found", nameof(Identity));

PnP.Core.Model.SharePoint.IList list = null;
if (ParameterSpecified(nameof(List)))
{
list = List.GetListOrThrow(nameof(List), PnPContext);
list = List.GetListOrThrow(nameof(List), pnpContext);
}
if (AddRole != null)
{
Expand Down
Loading

0 comments on commit 29531d7

Please sign in to comment.