Skip to content

Instantly share code, notes, and snippets.

@JarbasHorst
Created August 1, 2017 18:19
Show Gist options
  • Save JarbasHorst/6070ef275ce0c9a9b60e708dc0429afc to your computer and use it in GitHub Desktop.
Save JarbasHorst/6070ef275ce0c9a9b60e708dc0429afc to your computer and use it in GitHub Desktop.
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Jarbas Horst wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return.
* ----------------------------------------------------------------------------
*/
import { Version } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration,
PropertyPaneTextField
} from '@microsoft/sp-webpart-base';
import { HttpClient, HttpClientResponse, IHttpClientOptions } from '@microsoft/sp-http';
import { escape } from '@microsoft/sp-lodash-subset';
import styles from './RetrieveAdmins.module.scss';
import * as strings from 'retrieveAdminsStrings';
import { IRetrieveAdminsWebPartProps } from './IRetrieveAdminsWebPartProps';
export default class RetrieveAdminsWebPart extends BaseClientSideWebPart<IRetrieveAdminsWebPartProps> {
private azureFunctionUrl: string = 'https://jhorst-demo-functions.azurewebsites.net/api/RetrieveAdmins?code=qmhvYC3RWvaPTuLTgZyXUC3JNZH9L2BkDzteUztSjCfA/yXUC3JNZH==';
public render(): void {
this.domElement.innerHTML = `
<div class="${styles.retrieveAdmins}">
<div class="${styles.container}">
<div class="ms-Grid-row ms-bgColor-themeDark ms-fontColor-white ${styles.row}">
<div class="ms-Grid-col ms-u-lg10 ms-u-xl8 ms-u-xlPush2 ms-u-lgPush1">
<span class="ms-font-xl ms-fontColor-white">Welcome to SharePoint!</span>
<p class="ms-font-l ms-fontColor-white">Here the site collection administrators:</p>
<p id="adminsContainer"></p>
</div>
</div>
</div>
</div>`;
this._retrieveAdministrators();
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyPaneTextField('description', {
label: strings.DescriptionFieldLabel
})
]
}
]
}
]
};
}
private _retrieveAdministrators(): void {
const reqHeaders: Headers = new Headers();
reqHeaders.append('Content-type', 'application/json');
var siteUrl: string = this.context.pageContext.web.absoluteUrl;
console.log(`The site URL is: '${siteUrl}'`);
const reqOptions: IHttpClientOptions = {
headers: reqHeaders,
body: `{ SiteUrl: '${siteUrl}' }`
};
let adminsContainer: HTMLElement = document.getElementById("adminsContainer");
this.context.httpClient.post(this.azureFunctionUrl, HttpClient.configurations.v1, reqOptions)
.then((resp: HttpClientResponse) => {
resp.json().then((jsonResp: string) =>{
let respMessage = jsonResp;
if(resp.ok){
adminsContainer.className = 'ms-font-l ms-fontColor-black';
}else{
adminsContainer.className = 'ms-font-l ms-fontColor-red';
}
adminsContainer.innerText = respMessage;
});
}).catch ((resp: any) => {
adminsContainer.className = 'ms-font-l ms-fontColor-red';
adminsContainer.innerText = `Something went wrong :) Error = ${resp.message}`;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment