Instantly share code, notes, and snippets.

View SPFxProperyPaneDynamicModules.ts
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration,
WebPartContext
} from '@microsoft/sp-webpart-base';
import * as strings from 'MyWebPartStrings';
import MyComponent from './components/MyComponent';
View SPO Get User Alerts.js
var ctx = SP.ClientContext.get_current();
this.user = ctx.get_web().get_currentUser();
this.alerts = this.user.get_alerts();
ctx.load(this.user);
ctx.load(this.alerts);
ctx.executeQueryAsync(()=> {
var enumerator = this.alerts.getEnumerator();
View SPO List Alerts.js
var ctx = SP.ClientContext.get_current();
this.user = ctx.get_web().get_currentUser();
this.alerts = this.user.get_alerts();
this.list = ctx.get_web().get_lists().getByTitle('Documents');
ctx.load(this.user);
ctx.load(this.alerts);
ctx.load(this.list);
View Step7-LargeFileUpload-UploadChunk.js
//the final REST call is made to get the file information after it has been fully uploaded (especially the file list item id)
function getFileInformation(libraryPath, fileName, resolve, reject) {
let endpoint = String.format("{0}/_api/sp.appcontextsite(@target)/web/getfilebyserverrelativeurl(@libraryPath)/ListItemAllFields?@target='{3}'&@libraryPath='/sites/assetdatabase/{1}/{2}'",
utils.getSpContaxtUrlParams().appWebUrl, libraryPath, fileName, utils.getSpContaxtUrlParams().hostWebUrl);
const headers = {
"Accept": "application/json; odata=verbose"
};
View Step6-LargeFileUpload-Start.js
//Helper method - depending on what chunk of data we are dealing with, we need to use the correct REST method...
function getUploadMethod(offset, length, total) {
if (offset + length + 1 > total) {
return 'finishupload';
} else if (offset === 0) {
return 'startupload';
} else if (offset < total) {
return 'continueupload';
}
View Step5-LargeFileUpload-GettingContext.js
function getWebRequestExecutorFactory(appWebUrl, hostWebUrl, spLanguage) {
let context = new window.SP.ClientContext(appWebUrl);
const factory = new window.SP.ProxyWebRequestExecutorFactory(appWebUrl);
context.set_webRequestExecutorFactory(factory);
return context;
}
const jsomContext = (appWebUrl, hostWebUrl, spLanguage) => {
View Step4-LargeFileUpload-DummyFile.js
//this method sends the REST request using the SP RequestExecutor.js
function executeAsync(endPointUrl, data, requestHeaders) {
return new Promise((resolve, reject) => {
// remember that utils script we created?
let executor = new SP.RequestExecutor(utils.getSpContaxtUrlParams().appWebUrl);
// Send the request.
executor.executeAsync({
url: endPointUrl,
method: "POST",
View Step3-LargeFile-Preperation.js
import utils from './utils';
if (!ArrayBuffer.prototype.slice) {
ArrayBuffer.prototype.slice = function (begin, end) {
let len = this.byteLength;
begin = (begin|0) || 0;
end = end === (void 0) ? len : (end|0);
// Handle negative values.
if (begin < 0) begin = Math.max(begin + len, 0);
View Utils.js
function getDocumentQueryStrings() {
const sections = document.URL.split('?');
return sections.length > 1 ? sections[1].split('&') : [];
}
function S4() {
return (((1 + Math.random())*0x10000)|0).toString(16).substring(1);
}
View FileInput.html
<input
accept=".doc,.docx,.dot,.rtf,.pdf"
name="MyAwesomeFileUploader"
type="file"
id="MyAwesomeFileUploader"
className="inputFile"
multiple="false"
onchange="fileUploader.upload(this.files[0])" />