Skip to content

Instantly share code, notes, and snippets.

@chrisobriensp
Last active September 7, 2017 13:56
Show Gist options
  • Save chrisobriensp/505bb18d46d545bd7b260de201faf1aa to your computer and use it in GitHub Desktop.
Save chrisobriensp/505bb18d46d545bd7b260de201faf1aa to your computer and use it in GitHub Desktop.
C# code to add/remove/list globally-deployed SPFx extensions (with skipFeatureDeployment=true), specifically Application Customizers, using OfficeDevPnP.Core
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using OfficeDevPnP.Core.Entities;
using System;
using System.Linq;
namespace COB.SPFx.Extensions.Manage
{
/// <summary>
/// Console app code to add/remove/list SPFx Application Customizer extensions..
/// </summary>
class Program
{
// TODO: provide details of a single site here, or provide some code to iterate several sites..
private static readonly string SITE_URL = "TODO";
private static readonly string USERNAME = "TODO";
private static readonly string PWD = "TODO";
static void Main(string[] args)
{
AuthenticationManager authMgr = new AuthenticationManager();
ClientContext ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(SITE_URL, USERNAME, PWD);
Web currentWeb = ctx.Web;
ctx.Load(currentWeb);
ctx.ExecuteQueryRetry();
Console.WriteLine(string.Format("Main: Authenticated to site - '{0}'.", ctx.Web.Url));
// TODO: comment in/out method calls here as you need..
getCustomActions(ctx);
//addSpfxExtensionCustomAction(ctx);
//removeSpfxExtensionCustomAction(ctx, "COB-SPFx-GlobalHeader");
Console.ReadLine();
}
private static void addSpfxExtensionCustomAction(ClientContext ctx)
{
Console.WriteLine(string.Format("addSpfxExtensionCustomAction: Adding extension to web - '{0}'.", ctx.Web.Url));
Guid spfxExtension_GlobalHeaderID = new Guid("4a79203e-287d-4173-82c9-0111232c6c2e");
string spfxExtName = "COB-SPFx-GlobalHeader";
string spfxExtTitle = "COB-SPFx-GlobalHeader";
string spfxExtGroup = "";
string spfxExtDescription = "Adds a global header/footer to the site";
string spfxExtLocation = "ClientSideExtension.ApplicationCustomizer";
CustomActionEntity ca = new CustomActionEntity
{
Name = spfxExtName,
Title = spfxExtTitle,
Group = spfxExtGroup,
Description = spfxExtDescription,
Location = spfxExtLocation,
ClientSideComponentId = spfxExtension_GlobalHeaderID
};
ctx.Web.AddCustomAction(ca);
ctx.ExecuteQueryRetry();
Console.WriteLine(string.Format("addSpfxExtensionCustomAction: Successfully added extension to web - '{0}'.", ctx.Web.Url));
Console.WriteLine(string.Format("addSpfxExtensionCustomAction: Leaving."));
}
private static void removeSpfxExtensionCustomAction(ClientContext ctx, string extensionName)
{
Console.WriteLine(string.Format("removeSpfxExtensionCustomAction: Removing extension with name '{0}' from web - '{1}'.",
extensionName, ctx.Web.Url));
var customActionsToRemove = ctx.Web.GetCustomActions().Where(ca =>
ca.Location == "ClientSideExtension.ApplicationCustomizer" &&
ca.Name == extensionName);
if (customActionsToRemove.Count() > 0)
{
foreach (var ca in customActionsToRemove)
{
ctx.Web.DeleteCustomAction(ca.Id);
ctx.Web.Update();
ctx.ExecuteQueryRetry();
Console.WriteLine(string.Format("removeSpfxExtensionCustomAction: Successfully removed extension '{0}' from web '{1}'.",
extensionName, ctx.Web.Url));
}
}
else
{
Console.WriteLine(string.Format("removeSpfxExtensionCustomAction: Did not find any extension to remove named '{0}' on web '{1}'.",
extensionName, ctx.Web.Url));
}
Console.WriteLine("removeSpfxExtensionCustomAction: Leaving.");
}
private static void getCustomActions(ClientContext ctx)
{
Console.WriteLine(string.Format("getCustomActions: Retrieving extensions for web - '{0}'.", ctx.Web.Url));
var customActions = ctx.Web.GetCustomActions().Where(ca => ca.Location == "ClientSideExtension.ApplicationCustomizer");
if (customActions.Count() > 0)
{
foreach (var ca in customActions)
{
Console.WriteLine(string.Format("getCustomActions: Found extension with name - '{0}', ClientSideComponentId ='{1}'.",
ca.Name, ca.ClientSideComponentId));
}
}
else
{
Console.WriteLine("getCustomActions: No extensions found.");
}
Console.WriteLine("getCustomActions: Leaving.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment