Skip to content

Instantly share code, notes, and snippets.

@chrisobriensp
chrisobriensp / addSPFxWebPart_SetProperties.cs
Created March 13, 2017 15:04
Shows adding modern web parts to a page and setting properties..
// add video embed web part..
ClientSideWebPart videoEmbedWp = page.InstantiateDefaultWebPart(DefaultClientSideWebParts.VideoEmbed);
videoEmbedWp.Properties["videoSource"] = "https://chrisobriensp.sharepoint.com/portals/hub/_layouts/15/PointPublishing.aspx?app=video&p=p&chid=4b7ef33e-ed56-4c75-b3ad-61488cc82474&vid=1e07b31d-21fa-4326-9033-91adf1a90708";
videoEmbedWp.Properties["captionText"] = "ALM video";
videoEmbedWp.Properties["showInfo"] = false;
videoEmbedWp.Properties["embedCode"] = "<iframe width=853 height=480 src='https://chrisobriensp.sharepoint.com/portals/hub/_layouts/15/VideoEmbedHost.aspx?chId=4b7ef33e%2Ded56%2D4c75%2Db3ad%2D61488cc82474&amp;vId=1e07b31d%2D21fa%2D4326%2D9033%2D91adf1a90708&amp;width=853&amp;height=480&amp;autoPlay=false&amp;showInfo=true' allowfullscreen></iframe>";
videoEmbedWp.Title = "Associated video";
page.AddControl(videoEmbedWp);
// add Yammer embed web part..
@chrisobriensp
chrisobriensp / addSPFxWebPart_Simple.cs
Created March 13, 2017 14:52
Shows the simplest example of adding a modern web part to a SharePoint page.
private static void addDefaultWebPartToPageSimple(ClientContext siteContext, string pageName)
{
ClientSidePage page = new ClientSidePage(siteContext);
ClientSideWebPart videoEmbedWp = page.InstantiateDefaultWebPart(DefaultClientSideWebParts.VideoEmbed);
page.AddControl(videoEmbedWp);
page.Save(pageName);
}
@chrisobriensp
chrisobriensp / createOrModifyModernPage.cs
Created March 13, 2017 14:43
Shows PnP core methods to create/modify modern SharePoint pages.
private static void createPage(ClientContext siteContext, string pageName)
{
ClientSidePage page = new ClientSidePage(siteContext);
ClientSideText txt1 = new ClientSideText() { Text = "COB test" };
page.AddControl(txt1, 0);
// page will be created if it doesn't exist, otherwise overwritten if it does..
page.Save(pageName);
}
@chrisobriensp
chrisobriensp / addCustomSPFxWebPartToPage.cs
Last active March 13, 2017 15:26
Adds a named SPFx web part to a SharePoint page - generic method which accepts parameters.
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();
@chrisobriensp
chrisobriensp / addSPFxWebPart.cs
Last active March 12, 2017 10:56
Adds a named SPFx web part to a SharePoint page - simple example with hard-coded values.
private static void addModernWebPartToPage(ClientContext siteContext)
{
Console.WriteLine(string.Format("addModernWebPartToPage(): Entered."));
// get page context - the actual name/page instance is specified in the Save() method..
ClientSidePage page = new ClientSidePage(siteContext);
// get the available web parts - this collection will include OOTB and custom SPFx web parts..
var components = page.AvailableClientSideComponents();
@chrisobriensp
chrisobriensp / SPFxWebPartProperty_Link.ts
Created February 12, 2017 19:35
An extract showing the Link control you can use in SPFx web part properties (from getPropertyPaneConfiguration() method).
@chrisobriensp
chrisobriensp / SPFxWebPartProperty_Slider.ts
Created February 12, 2017 19:21
An extract showing the Slider control you can use in SPFx web part properties (from getPropertyPaneConfiguration() method).
PropertyPaneSlider('maxResultsProp', {
label: 'Max results',
min: 1,
max: 20,
step: 2,
showValue: true,
value: 10
})
@chrisobriensp
chrisobriensp / SPFxWebPartProperty_Toggle.ts
Last active February 12, 2017 19:23
An extract showing the Toggle control you can use in SPFx web part properties (from getPropertyPaneConfiguration() method).
PropertyPaneToggle('toBeProp', {
key: 'toBeToggle',
label: 'To be or not to be?',
onText: 'To be!',
offText: 'Not to be'
})
@chrisobriensp
chrisobriensp / SPFxWebPartProperty_ChoiceGroup.ts
Last active February 12, 2017 19:23
An extract showing the Choice Group control you can use in SPFx web part properties (from getPropertyPaneConfiguration() method) - used here in 'standard' mode without images.
PropertyPaneChoiceGroup('layoutProp', {
label: 'Choices',
options: [
{ key: '2Cols', text: 'Two columns' },
{ key: '3Cols', text: 'Three columns', checked: true },
{ key: 'Horizontal', text: 'Horizontal' }
]
}),
@chrisobriensp
chrisobriensp / SPFxWebPartProperty_ChoiceGroup_WithImages.ts
Created February 7, 2017 21:49
An extract showing the Choice Group control you can use in SPFx web part properties (from getPropertyPaneConfiguration() method) - used here with images.
PropertyPaneChoiceGroup('FileType', {
label: 'File type:',
options: [
{ key: 'Word', text: 'Word',
imageSrc: 'https://static2.sharepointonline.com/files/fabric/assets/brand-icons/document/png/docx_32x1.png',
imageSize: { width: 32, height: 32 },
selectedImageSrc: 'https://static2.sharepointonline.com/files/fabric/assets/brand-icons/document/png/docx_32x1.png'
},
{ key: 'Excel', text: 'Excel',
imageSrc: 'https://static2.sharepointonline.com/files/fabric/assets/brand-icons/document/png/xlsx_32x1.png',