Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1054 from erwinvanhunen/PR1008
Browse files Browse the repository at this point in the history
Pr1008
  • Loading branch information
erwinvanhunen committed Sep 7, 2017
2 parents 1226481 + 4025c04 commit b94efa1
Show file tree
Hide file tree
Showing 32 changed files with 1,477 additions and 1 deletion.
74 changes: 74 additions & 0 deletions Commands/Base/PipeBinds/ClientSideComponentPipeBind.cs
@@ -0,0 +1,74 @@
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Pages;
using SharePointPnP.PowerShell.Commands.ClientSidePages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharePointPnP.PowerShell.Commands.Base.PipeBinds
{
public sealed class ClientSideComponentPipeBind
{
private readonly ClientSideComponent _component;
private string _name;
private Guid _id;

public ClientSideComponentPipeBind(ClientSideComponent component)
{
_component = component;
_id = Guid.Parse(_component.Id);
_name = _component.Name;
}

public ClientSideComponentPipeBind(string nameOrId)
{
_component = null;
if (!Guid.TryParse(nameOrId, out _id))
{
_name = nameOrId;
}
}

public ClientSideComponentPipeBind(Guid id)
{
_id = id;
_name = null;
_component = null;
}

public ClientSideComponent Component => _component;

public string Name => _component?.Name;

public string Id => _component == null ? Guid.Empty.ToString() : _component.Id;

public override string ToString() => Name;

internal ClientSideComponent GetComponent(ClientSidePage page)
{
if (_component != null)
{
return _component;
}
else if (!string.IsNullOrEmpty(_name))
{
ClientSideComponent com = page.AvailableClientSideComponents(_name).FirstOrDefault();
return com;
}
else if (_id != Guid.Empty)
{
string idAsString = _id.ToString();
var comQuery = from c in page.AvailableClientSideComponents(_name)
where c.Id == idAsString
select c;
return comQuery.FirstOrDefault();
}
else
{
return null;
}
}
}
}
58 changes: 58 additions & 0 deletions Commands/Base/PipeBinds/ClientSidePagePipeBind.cs
@@ -0,0 +1,58 @@
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Pages;
using SharePointPnP.PowerShell.Commands.ClientSidePages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharePointPnP.PowerShell.Commands.Base.PipeBinds
{
public sealed class ClientSidePagePipeBind
{
private readonly ClientSidePage _page;
private string _name;

public ClientSidePagePipeBind(ClientSidePage page)
{
_page = page;
_name = page.PageTitle;
}

public ClientSidePagePipeBind(string name)
{
_page = null;
_name = name;
}

public ClientSidePage Page => _page;

public string Name => ClientSidePageUtilities.EnsureCorrectPageName(_name);

public override string ToString() => Name;

internal ClientSidePage GetPage(ClientContext ctx)
{
if (_page != null)
{
return _page;
}
else if (!string.IsNullOrEmpty(_name))
{
try
{
return ClientSidePage.Load(ctx, Name);
}
catch (ArgumentException ex)
{
return null;
}
}
else
{
return null;
}
}
}
}
67 changes: 67 additions & 0 deletions Commands/Base/PipeBinds/GenericPropertiesPipeBind.cs
@@ -0,0 +1,67 @@
using Microsoft.SharePoint.Client;
using Newtonsoft.Json.Linq;
using OfficeDevPnP.Core.Pages;
using SharePointPnP.PowerShell.Commands.ClientSidePages;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;

namespace SharePointPnP.PowerShell.Commands.Base.PipeBinds
{
public sealed class PropertyBagPipeBind
{
private readonly Hashtable _hashtable;
private string _jsonString;
private JObject _jsonObject;

public PropertyBagPipeBind(Hashtable hashtable)
{
_hashtable = hashtable;
_jsonString = null;
_jsonObject = null;
}

public PropertyBagPipeBind(string json)
{
_hashtable = null;
_jsonString = json;
_jsonObject = JObject.Parse(json);
}

public string Json => _jsonString;

public JObject JsonObject => _jsonObject ?? HashtableToJsonObject(_hashtable);

public Hashtable Properties => _hashtable;

public override string ToString() => Json ?? HashtableToJsonString(_hashtable);

private string HashtableToJsonString(Hashtable hashtable)
{
return HashtableToJsonObject(hashtable).ToString();
}

private JObject HashtableToJsonObject(Hashtable hashtable)
{
var obj = new JObject();

foreach (var key in hashtable.Keys)
{
var rawValue = hashtable[key];

// To ensure the value is not serialized as PSObject
object value = rawValue is PSObject
? ((PSObject)rawValue).BaseObject
: rawValue;

obj[key] = JToken.FromObject(value);
}
return obj;
}

}
}
99 changes: 99 additions & 0 deletions Commands/ClientSidePages/AddClientSidePage.cs
@@ -0,0 +1,99 @@
#if !ONPREMISES
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core.Pages;
using SharePointPnP.PowerShell.CmdletHelpAttributes;
using System;
using System.Management.Automation;

namespace SharePointPnP.PowerShell.Commands.ClientSidePages
{
[Cmdlet(VerbsCommon.Add, "PnPClientSidePage")]
[CmdletHelp("Adds a Client-Side Page",
Category = CmdletHelpCategory.ClientSidePages, SupportedPlatform = CmdletSupportedPlatform.Online)]
[CmdletExample(
Code = @"PS:> Add-PnPClientSidePage -PageName ""OurNewPage""",
Remarks = "Creates a new Client-Side page called 'OurNewPage'",
SortOrder = 1)]
public class AddClientSidePage : PnPWebCmdlet
{
[Parameter(Mandatory = true, HelpMessage = "Specifies the name of the page.")]
public string Name = null;

[Parameter(Mandatory = false, HelpMessage = "Specifies the layout type of the page.")]
public ClientSidePageLayoutType LayoutType = ClientSidePageLayoutType.Article;

[Parameter(Mandatory = false, HelpMessage = "Allows to promote the page for a specific purpose (HomePage | NewsPage)")]
public ClientSidePagePromoteType PromoteAs = ClientSidePagePromoteType.None;

[Parameter(Mandatory = false, HelpMessage = "Enables or Disables the comments on the page")]
public bool? CommentsEnabled = null;

[Parameter(Mandatory = false, HelpMessage = "Publishes the page once it is saved. Applicable to libraries set to create major and minor versions.")]
public SwitchParameter Publish;

[Parameter(Mandatory = false, HelpMessage = "Sets the message for publishing the page.")]
public string PublishMessage = string.Empty;

protected override void ExecuteCmdlet()
{

ClientSidePage clientSidePage = null;

// Check if the page exists

string name = ClientSidePageUtilities.EnsureCorrectPageName(Name);

bool pageExists = false;
try
{
ClientSidePage.Load(ClientContext, name);
pageExists = true;
}
catch { }

if(pageExists)
{
throw new Exception($"Page {name} already exists");
}

// Create a page that persists immediately
clientSidePage = SelectedWeb.AddClientSidePage(name);
clientSidePage.LayoutType = LayoutType;
clientSidePage.Save(name);

// If a specific promote type is specified, promote the page as Home or Article or ...
switch (PromoteAs)
{
case ClientSidePagePromoteType.HomePage:
clientSidePage.PromoteAsHomePage();
break;
case ClientSidePagePromoteType.NewsArticle:
clientSidePage.PromoteAsNewsArticle();
break;
case ClientSidePagePromoteType.None:
default:
break;
}

if (CommentsEnabled.HasValue)
{
if (CommentsEnabled.Value)
{
clientSidePage.EnableComments();
}
else
{
clientSidePage.DisableComments();
}
}

if (Publish)
{
clientSidePage.Publish(PublishMessage);
}

WriteObject(clientSidePage);
}
}
}
#endif
56 changes: 56 additions & 0 deletions Commands/ClientSidePages/AddClientSidePageSection.cs
@@ -0,0 +1,56 @@
#if !ONPREMISES
using OfficeDevPnP.Core.Pages;
using SharePointPnP.PowerShell.CmdletHelpAttributes;
using SharePointPnP.PowerShell.Commands.Base.PipeBinds;
using System;
using System.Management.Automation;

namespace SharePointPnP.PowerShell.Commands.ClientSidePages
{
[Cmdlet(VerbsCommon.Add, "PnPClientSidePageSection")]
[CmdletHelp("Adds a new section to a Client-Side page",
Category = CmdletHelpCategory.ClientSidePages, SupportedPlatform = CmdletSupportedPlatform.Online)]
[CmdletExample(
Code = @"PS:> Add-PnPClientSidePageSection -Page ""MyPage"" -SectionTemplate OneColumn",
Remarks = "Adds a new one-column section to the Client-Side page 'MyPage'",
SortOrder = 1)]
[CmdletExample(
Code = @"PS:> Add-PnPClientSidePageSection -Page ""MyPage"" -SectionTemplate ThreeColumn -Order 10",
Remarks = "Adds a new Three columns section to the Client-Side page 'MyPage' with an order index of 10",
SortOrder = 2)]
[CmdletExample(
Code = @"PS:> $page = Add-PnPClientSidePage -Name ""MyPage""
PS> Add-PnPClientSidePageSection -Page $page -SectionTemplate OneColumn",
Remarks = "Adds a new one column section to the Client-Side page 'MyPage'",
SortOrder = 2)]
public class AddClientSidePageSection : PnPWebCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0, HelpMessage = "The name of the page")]
public ClientSidePagePipeBind Page;

[Parameter(Mandatory = true, HelpMessage = "Specifies the columns template to use for the section.")]
public CanvasSectionTemplate SectionTemplate;

[Parameter(Mandatory = false, HelpMessage = "Sets the order of the section. (Default = 1)")]
public int Order = 1;


protected override void ExecuteCmdlet()
{
var clientSidePage = Page?.GetPage(ClientContext);

if (clientSidePage != null)
{
clientSidePage.AddSection(SectionTemplate, Order);
clientSidePage.Save();
}
else
{
// If the client side page object cannot be found
throw new Exception($"Page {Page} cannot be found.");
}

}
}
}
#endif

0 comments on commit b94efa1

Please sign in to comment.