Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3447 : issue with page scheduling feature. #3469

Merged
merged 2 commits into from
Oct 8, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed `New-PnPTeamsTeam` cmdlet not working well with a managed identity [#3351](https://github.com/pnp/powershell/pull/3351)
- Fixed `Copy-PnPFile`, `Copy-PnPFolder` and `Move-PnPFile` to better handle copying or moving operations to OneDrive or Multi-geo environments. [#3245](https://github.com/pnp/powershell/pull/3245)
- Fixed `Get-PnPTenantTemplate` not doing anything when the `-SiteUrl` parameter had not been specified. It will now use the currently connected site when the parameter is omitted. [#3431](https://github.com/pnp/powershell/pull/3431)
- Fixed `Enable-PnPPageScheduling` and `Disable-PnPPageScheduling` cmdlets not working due to changes in backend code. [#3469](https://github.com/pnp/powershell/pull/3469)

### Changed

Expand Down
6 changes: 3 additions & 3 deletions src/Commands/Features/DisablePageScheduling.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Management.Automation;
using System;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Utilities;

namespace PnP.PowerShell.Commands.Features
{
Expand All @@ -10,7 +9,8 @@ public class DisablePageScheduling : PnPWebCmdlet
{
protected override void ExecuteCmdlet()
{
CurrentWeb.DeactivateFeature(new Guid("E87CA965-5E07-4A23-B007-DDD4B5AFB9C7"));
var pagesList = PagesUtility.GetModernPagesLibrary(PnPContext.Web);
Utilities.REST.RestHelper.PostAsync(Connection.HttpClient, $"{PnPContext.Web.Url}/_api/sitepages/pagesinlib(guid'{pagesList.Id}')/setscheduling(false)", ClientContext, null, "application/json", "application/json;odata=nometadata").GetAwaiter().GetResult();
}
}
}
6 changes: 3 additions & 3 deletions src/Commands/Features/EnablePageScheduling.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Management.Automation;
using System;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Utilities;

namespace PnP.PowerShell.Commands.Features
{
Expand All @@ -10,7 +9,8 @@ public class EnablePageScheduling : PnPWebCmdlet
{
protected override void ExecuteCmdlet()
{
CurrentWeb.ActivateFeature(new Guid("E87CA965-5E07-4A23-B007-DDD4B5AFB9C7"));
var pagesList = PagesUtility.GetModernPagesLibrary(PnPContext.Web);
Utilities.REST.RestHelper.PostAsync(Connection.HttpClient, $"{PnPContext.Web.Url}/_api/sitepages/pagesinlib(guid'{pagesList.Id}')/setscheduling(true)", ClientContext, null, "application/json", "application/json;odata=nometadata").GetAwaiter().GetResult();
}
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion src/Commands/Model/Graph/MicrosoftSearch/ExternalItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ExternalItem
/// <summary>
/// The managed properties to provide to Microsoft Search about this external item
/// </summary>
[JsonConverter(typeof(JsonConverters.MicrosoftSearchExternalItemPropertyConverter))]
[JsonConverter(typeof(Utilities.Json.MicrosoftSearchExternalItemPropertyConverter))]
[JsonPropertyName("properties")]
public Hashtable Properties { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace PnP.PowerShell.Commands.Utilities.Json
{
internal sealed class MicrosoftSearchExternalItemPropertyConverter: JsonConverter<Hashtable>
{
public override Hashtable Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var converter = (JsonConverter<Hashtable>)options.GetConverter(typeof(Hashtable));
return converter.Read(ref reader, typeToConvert, options);
}

public override void Write(Utf8JsonWriter writer, Hashtable hashtable, JsonSerializerOptions options)
{
writer.WriteStartObject();
foreach (DictionaryEntry value in hashtable)
{
switch (value.Value)
{
case string:
writer.WritePropertyName($"{value.Key}@odata.type");
writer.WriteStringValue("String");

writer.WritePropertyName(value.Key.ToString());
writer.WriteStringValue(value.Value.ToString());
break;

case DateTime dateTime:
writer.WritePropertyName($"{value.Key}@odata.type");
writer.WriteStringValue("DateTimeOffset");

writer.WritePropertyName(value.Key.ToString());
writer.WriteRawValue($"\"{dateTime:o}\"");
break;

case IEnumerable ieNumerable:
writer.WritePropertyName($"{value.Key}@odata.type");
writer.WriteStringValue("Collection(String)");

writer.WritePropertyName(value.Key.ToString());
writer.WriteStartArray();
foreach (object item in ieNumerable)
{
writer.WriteStringValue(item.ToString());
}
writer.WriteEndArray();
break;

default:
writer.WritePropertyName(value.Key.ToString());
writer.WriteStringValue(value.Value.ToString());
break;
}
}
writer.WriteEndObject();
}
}
}
41 changes: 41 additions & 0 deletions src/Commands/Utilities/PagesUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using PnP.Core.Model;
using PnP.Core.Model.SharePoint;
using PnP.Core.QueryModel;
using System;
using System.Linq;
using System.Linq.Expressions;

namespace PnP.PowerShell.Commands.Utilities
{
internal static class PagesUtility
{
private static readonly Expression<Func<IList, object>>[] getPagesLibraryExpression = new Expression<Func<IList, object>>[] {p => p.Title, p => p.TemplateType, p => p.Id,
p => p.RootFolder.QueryProperties(p => p.Properties, p => p.ServerRelativeUrl), p => p.Fields };
internal static IList GetModernPagesLibrary(IWeb web)
{
IList pagesLibrary = null;
var libraries = web.Lists.QueryProperties(getPagesLibraryExpression)
.Where(p => p.TemplateType == ListTemplateType.WebPageLibrary)
.ToListAsync()
.GetAwaiter().GetResult();

if (libraries.Count == 1)
{
pagesLibrary = libraries[0];
}
else
{
foreach (var list in libraries)
{
if (list.IsPropertyAvailable(p => p.Fields) && list.Fields.AsRequested().FirstOrDefault(p => p.InternalName == "CanvasContent1") != null)
{
pagesLibrary = list;
break;
}
}
}

return pagesLibrary;
}
}
}