Last active
March 13, 2017 15:26
-
-
Save chrisobriensp/8f55bd693ebfc312f80ee93c686a18eb to your computer and use it in GitHub Desktop.
Adds a named SPFx web part to a SharePoint page - generic method which accepts parameters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void addCustomSPFxWebPartToPage(ClientContext siteContext, string wpName, string pageName, int wpOrder) | |
{ | |
// create page example.. | |
ClientSidePage page = new ClientSidePage(siteContext); | |
// modify existing page example.. | |
//ClientSidePage page = ClientSidePage.Load(siteContext, pageName); | |
// get the available web parts - this collection will include OOTB and custom SPFx web parts.. | |
var components = page.AvailableClientSideComponents(); | |
// add the named web part.. | |
var webPartToAdd = components.Where(wp => wp.ComponentType == 1 && wp.Name == wpName).FirstOrDefault(); | |
if (webPartToAdd != null) | |
{ | |
ClientSideWebPart clientWp = new ClientSideWebPart(webPartToAdd) { Order = wpOrder }; | |
page.AddControl(clientWp); | |
} | |
// the save method creates the page if one doesn't exist with that name in this site.. | |
page.Save(pageName); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment