Skip to content

Commit

Permalink
Refactor code to use PnPCore for Features and Content Types where (#4390
Browse files Browse the repository at this point in the history
)

Co-authored-by: Gautam Sheth <[email protected]>
  • Loading branch information
gautamdsheth and Gautam Sheth authored Oct 5, 2024
1 parent 29531d7 commit dd24c9b
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 44 deletions.
6 changes: 4 additions & 2 deletions src/Commands/Base/ConnectOnline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ public class ConnectOnline : BasePSCmdlet
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_OSLOGIN)]
public SwitchParameter OSLogin;

private static readonly string[] sourceArray = ["stop", "ignore", "silentlycontinue"];

protected override void ProcessRecord()
{
cancellationTokenSource = new CancellationTokenSource();
Expand Down Expand Up @@ -461,7 +463,7 @@ protected void Connect(ref CancellationToken cancellationToken)
}

// If the ErrorAction is not set to Stop, Ignore or SilentlyContinue throw an exception, otherwise just continue
if (!new[] { "stop", "ignore", "silentlycontinue" }.Contains(ErrorActionSetting.ToLowerInvariant()))
if (!sourceArray.Contains(ErrorActionSetting.ToLowerInvariant()))
{
throw new PSInvalidOperationException(errorMessage);
}
Expand Down Expand Up @@ -1124,7 +1126,7 @@ private void ReuseAuthenticationManager()
PnPConnection.CachedAuthenticationManager = contextSettings?.AuthenticationManager;
}

private SecureString StringToSecureString(string inputString)
private static SecureString StringToSecureString(string inputString)
{
SecureString secPassword = new SecureString();
foreach (char ch in inputString)
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/ContentTypes/AddContentType.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client;
using System.Management.Automation;


namespace PnP.PowerShell.Commands.ContentTypes
Expand All @@ -23,7 +23,7 @@ public class AddContentType : PnPWebCmdlet
public ContentType ParentContentType;

[Parameter(Mandatory = false)]
public string DocumentTemplate;
public string DocumentTemplate;

protected override void ExecuteCmdlet()
{
Expand Down
5 changes: 2 additions & 3 deletions src/Commands/ContentTypes/AddContentTypeToList.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.ContentTypes
{
Expand Down
14 changes: 5 additions & 9 deletions src/Commands/ContentTypes/RemoveContentType.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;
using Resources = PnP.PowerShell.Commands.Properties.Resources;

namespace PnP.PowerShell.Commands.ContentTypes
{
[Cmdlet(VerbsCommon.Remove, "PnPContentType")]
public class RemoveContentType : PnPWebCmdlet
{

[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
public ContentTypePipeBind Identity;

Expand All @@ -18,11 +15,10 @@ public class RemoveContentType : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var ct = Identity?.GetContentTypeOrThrow(nameof(Identity), CurrentWeb);
if (Force || ShouldContinue($"Remove Content Type '{ct.EnsureProperty(c => c.Name)}'?", Resources.Confirm))
var ct = Identity?.GetContentTypeOrThrow(nameof(Identity), Connection.PnPContext);
if (Force || ShouldContinue($"Remove Content Type '{ct.Name}'?", Resources.Confirm))
{
ct.DeleteObject();
ClientContext.ExecuteQueryRetry();
ct.Delete();
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions src/Commands/ContentTypes/RemoveContentTypeFromList.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;

namespace PnP.PowerShell.Commands.ContentTypes
{

[Cmdlet(VerbsCommon.Remove, "PnPContentTypeFromList")]
public class RemoveContentTypeFromList : PnPWebCmdlet
{
Expand All @@ -19,13 +16,13 @@ public class RemoveContentTypeFromList : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var list = List.GetListOrThrow(nameof(List), CurrentWeb);
var list = List.GetListOrThrow(nameof(List), Connection.PnPContext);
var ct = ContentType.GetContentTypeOrWarn(this, list);
if (ct != null)
{
CurrentWeb.RemoveContentTypeFromList(list, ct);
ct.Delete();
list.Update();
}
}

}
}
10 changes: 5 additions & 5 deletions src/Commands/Features/DisableFeature.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Enums;

namespace PnP.PowerShell.Commands.Features
Expand All @@ -22,13 +19,16 @@ public class DisableFeature : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
var pnpContext = Connection.PnPContext;
if (Scope == FeatureScope.Web)
{
CurrentWeb.DeactivateFeature(Identity);
pnpContext.Web.EnsureProperties(w => w.Features);
pnpContext.Web.Features.Disable(Identity);
}
else
{
ClientContext.Site.DeactivateFeature(Identity);
pnpContext.Site.EnsureProperties(s => s.Features);
pnpContext.Site.Features.Disable(Identity);
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/Commands/Features/EnableFeature.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using System;
using PnP.PowerShell.Commands.Enums;

Expand All @@ -9,7 +8,7 @@ namespace PnP.PowerShell.Commands.Features
[OutputType(typeof(void))]
public class EnableFeature : PnPWebCmdlet
{
[Parameter(Mandatory = true, Position=0, ValueFromPipeline=true)]
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
public Guid Identity;

[Parameter(Mandatory = false)]
Expand All @@ -20,13 +19,16 @@ public class EnableFeature : PnPWebCmdlet

protected override void ExecuteCmdlet()
{
if(Scope == FeatureScope.Web)
var pnpContext = Connection.PnPContext;
if (Scope == FeatureScope.Web)
{
CurrentWeb.ActivateFeature(Identity);
pnpContext.Web.EnsureProperties(w => w.Features);
pnpContext.Web.Features.Enable(Identity);
}
else
{
ClientContext.Site.ActivateFeature(Identity);
pnpContext.Site.EnsureProperties(s => s.Features);
pnpContext.Site.Features.Enable(Identity);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Commands/InformationManagement/GetLabel.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.PowerShell.Commands.Base.PipeBinds;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Text.Json.Serialization;

namespace PnP.PowerShell.Commands.Model.PowerPlatform.PowerApp
Expand Down
5 changes: 1 addition & 4 deletions src/Commands/Model/Teams/TeamsChannelFilesFolder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System;

namespace PnP.PowerShell.Commands.Model.Teams
{
Expand Down

0 comments on commit dd24c9b

Please sign in to comment.