Skip to content

Instantly share code, notes, and snippets.

@chrisobriensp
chrisobriensp / SPFxWebPartProperty_Buttons.ts
Last active January 31, 2017 15:14
An extract showing different button types you can use in SPFx web part properties (from getPropertyPaneConfiguration() method).
groupFields: [
PropertyPaneButton('', {
text: "Normal button",
buttonType: PropertyPaneButtonType.Normal,
onClick: this.cobWPPropButtonClick
}),
PropertyPaneHorizontalRule(),
PropertyPaneButton('', {
text: "Primary button",
buttonType: PropertyPaneButtonType.Primary,
@chrisobriensp
chrisobriensp / SitePages_Feed.json
Created November 24, 2016 11:47
Shows a sample response from the [list]/_api/sitepages/pages/feed endpoint used by news headlines/news list controls in modern SharePoint sites.
{
"@odata.context": "https://chrisobriensp.sharepoint.com/sites/team1/_api/$metadata#SitePageMetadatas",
"value": [{
"@odata.editLink": "SP.Publishing.SitePageMetadata52f99f8b-a261-4372-a468-884f1933e8e4",
"@odata.id": "https://chrisobriensp.sharepoint.com/sites/team1/_api/SP.Publishing.SitePageMetadata52f99f8b-a261-4372-a468-884f1933e8e4",
"@odata.type": "#SP.Publishing.SitePageMetadata",
"AbsoluteUrl": "https://chrisobriensp.sharepoint.com/sites/team1/SitePages/Facebook details plans to combat.aspx",
"BannerImageUrl": "https://chrisobriensp.sharepoint.com/sites/team1/SiteAssets/SitePages/Facebook-details-plans-to-combat/54483-Facebook.jpg, https://chrisobriensp.sharepoint.com/sites/team1/SiteAssets/SitePages/Facebook-details-plans-to-combat/54483-Facebook.jpg",
"CreatedBy": {
"@odata.editLink": "SP.Publishing.SitePageMetadata52f99f8b-a261-4372-a468-884f1933e8e4/CreatedBy",
@chrisobriensp
chrisobriensp / SimpleDynamicDropdown.ts
Last active April 16, 2019 07:14
Showing a simple way of populating an SPFX dropdown with code. See http://cob-sp.com/SPFx-WP-Props2
private fetchOptionsSimple(): Array<IPropertyPaneDropdownOption> {
var options: Array<IPropertyPaneDropdownOption> = new Array<IPropertyPaneDropdownOption>();
options.push( { key: 'Added1', text: 'Added from code 1' });
options.push( { key: 'Added2', text: 'Added from code 2' });
return options;
}
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
var fetchedOptions: Array<IPropertyPaneDropdownOption> = this.fetchOptions();
@chrisobriensp
chrisobriensp / asyncTextBoxValidationMethod.ts
Last active February 14, 2017 11:49
TypeScript to show a validation function for PropertyPaneTextbox in SPFX which makes an async call. See http://cob-sp.com/SPFx-WP-Props1
private asyncTextBoxValidationMethod(value: string): Promise<string> {
if (value !== undefined && value.length > 3) {
var url = this.context.pageContext.web.absoluteUrl + `/_api/web/lists?$filter=Hidden eq false`;
return this.fetchLists(url).then((response) => {
var lists: ISPList[] = response.value;
var foundList: boolean = false;
lists.forEach((list: ISPList) => {
if (value === list.Title) {
foundList = true;
@chrisobriensp
chrisobriensp / WebPartPropetyPane_DynamicDropdown.ts
Last active January 23, 2017 23:09
SPFX propertyPaneSettings() code for a PropertyPaneDropdown populated dynamically. See http://cob-sp.com/SPFx-WP-Props2
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
if (!this.listsFetched) {
this.fetchOptions().then((response) => {
this.dropdownOptions = response;
this.listsFetched = true;
// now refresh the property pane, now that the promise has been resolved..
this.onDispose();
});
}
@chrisobriensp
chrisobriensp / FetchListsForWebPartPropsDropdown.ts
Last active November 8, 2019 12:13
Dynamically populate SPFX web part property dropdown list - using lists in current site. See http://cob-sp.com/SPFx-WP-Props2
/* need some imports e.g.:
import { IODataList } from '@microsoft/sp-odata-types';
import { SPHttpClient, SPHttpClientConfigurations, SPHttpClientConfiguration, SPHttpClientResponse, ODataVersion, ISPHttpClientConfiguration } from '@microsoft/sp-http';
*/
private dropdownOptions: IPropertyPaneDropdownOption[];
private listsFetched: boolean;
// these methods are split out to go step-by-step, but you could refactor and be more direct if you choose..
@chrisobriensp
chrisobriensp / WebPartPropetyPane_MultiplePages.ts
Last active January 25, 2017 17:39
Code for a SharePoint Framework property pane with multiple pages. See http://cob-sp.com/SPFx-WP-Props1
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: "Basic settings"
},
groups: [
{
groupName: "Look and feel",
export interface ISharePointSearchResults {
PrimaryQueryResult: ISharePointPrimaryQueryResult;
}
export interface ISharePointPrimaryQueryResult {
RelevantResults: ISharePointRelevantResultsTable;
}
export interface ISharePointRelevantResultsTable {
Table: ISharePointSearchResultsTable;
@chrisobriensp
chrisobriensp / SearchWithHttpClient_AndCaller.ts
Last active September 21, 2016 09:23
Demonstrates calling SharePoint search using SharePoint Framework HttpClient object with Promises (in TypeScript). See http://cob-sp.com/SPFX-Promises
// shows function first, then calling code below - note two functions are used here..
public GetResultsFromHttpClient (context: IWebPartContext, url: string): Promise<ISharePointSearchResults> {
return context.httpClient.get(url, {
// workaround for httpClient/search API issue..
headers: {
"odata-version": ""
}}).then((response: Response) => {
if (response.ok) {
console.log("Returned OK from httpClient");
@chrisobriensp
chrisobriensp / SearchWithJQueryAjax_AndCaller.ts
Last active September 21, 2016 09:23
Demonstrates calling SharePoint search using jQuery AJAX with Promises (in TypeScript). See http://cob-sp.com/SPFX-Promises
// shows function first, then calling code below..
export default class Search {
public GetResultsFromjQueryAjax(query: string, rowLimit: number): Promise<ISearchResult> {
var self: Search = this;
return new Promise<ISearchResult>((resolve, reject) => {
var results: SearchResult[] = new Array();
var url = self.getSearchUrl(query, rowLimit);