Skip to content

Instantly share code, notes, and snippets.

View phillipharding's full-sized avatar

Phil Harding phillipharding

View GitHub Profile
@phillipharding
phillipharding / ArrayElementMove.js
Created September 8, 2024 16:15
Array Element Moving
a=[
{ sortIdx: 5, value: "E"},
{ sortIdx: 6, value: "F"},
{ sortIdx: 1, value: "A"},
{ sortIdx: 4, value: "D"},
{ sortIdx: 3, value: "C"},
{ sortIdx: 2, value: "B"},
];
function move(elToMove, targetSortIdx) {
@phillipharding
phillipharding / Azure AD Token using Certificate Secret.md
Last active August 12, 2024 22:02
Generate an Azure AD Access Token using the Client Credentials flow with a Certificate Secret to use for calling the SharePoint REST API
@phillipharding
phillipharding / Upgrading a Functions App and Code-base to the v4 Runtime.md
Created November 15, 2023 15:13
Upgrading a Functions App and Code-base to the v4 Runtime
@phillipharding
phillipharding / Ensure a SharePoint Folder Path using PnPJS.ts
Last active November 9, 2023 10:38
Create/Ensure a SharePoint folder path with nested folders using PnPJS
/*
Hit 'ctrl + d' or 'cmd + d' to run the code, view console for results
*/
import { IWeb, sp, Web } from "@pnp/sp/presets/all";
import { HttpRequestError } from "./@pnp/odata";
const TranslatePnPError = async (opText: string, e) => {
if (e?.isHttpRequestError) {
const json = await (<HttpRequestError>e).response.json();
const errorObject = typeof json["odata.error"] === "object" ? {...json["odata.error"]} : {...e};
//import { sp, Site, spGet, SharePointQueryable } from "@pnp/sp/presets/all";
import { sp } from "@pnp/sp";
import { Site } from "@pnp/sp/sites";
import "@pnp/sp/sites";
import { IFetchOptions } from "@pnp/common";
console.clear();
(async () => {
// get site using PnPJs
@phillipharding
phillipharding / DisplaySharePointGroupMembers.js
Last active July 13, 2023 17:44
Office 365 -- Get all members of a SharePoint group and show their profile information from the site collections User Information List
(function(window, undefined) {
"use strict";
/**** The following HTML (script) template is placed on the page
<script id="po1persontemplate" type="text/x-custom-template">
<li>
<div class='people_details'>
<h3>{0}</h3>
<p class='people_job_title'>{1}</p>
<div class='people_description'>{2}</div>
@phillipharding
phillipharding / spfx-get-delegated-accesstoken.md
Last active July 8, 2023 14:05
Use SPFx to get a Delegated Access Token for a Resource to Pass onto Azure Functions

Use SPFx to get a Delegated Access Token

Note the tokenAcquisitionEvent part of the code doesn't seem to work

Using this technique you can acquire a delegated access token for a resource, SharePoint or the Graph API, and pass that token onto a HTTP triggered Azure Functions endpoint for it to use to call the appropriate API.

This is a simple re-imagination of the on-behalf-of flow involving a secured Azure Functions endpoint then executing an OAuth on-behalf-of flow to exchange the received access token for one which can be used to call an API on behalf of the calling user.

Documented originally by Paolo Pialorsi and Joel Rodrigues

@phillipharding
phillipharding / Verify AAD Access Token.js
Last active July 8, 2023 14:04
Verifies an access token issued by the Azure AD identity platform
/** References:
* https://www.npmjs.com/package/azure-ad-verify-token
* https://github.com/justinlettau/azure-ad-verify-token
* https://liangjunjiang.medium.com/verify-and-decode-azure-activity-directory-token-bc72cf7010bc
* https://liangjunjiang.medium.com/azure-active-directory-api-v1-0-vs-v2-0-5c75fb2b1154
* https://www.npmjs.com/package/jwks-rsa
* https://www.npmjs.com/package/jsonwebtoken
* https://www.voitanos.io/blog/validating-azure-ad-generated-oauth-tokens
*
* NOTE:
@phillipharding
phillipharding / CreateListItemInAnotherSite.js
Created May 30, 2019 13:15
Create a ListItem in another site using the REST API
(async function() {
function getFailedResponseData(response) {
return new Promise( async (resolve) => {
const ct = response.headers.get("Content-Type") || "";
let data = {};
if (ct.match("application/json")) {
data = await response.json();
} else {
@phillipharding
phillipharding / Add-ValidateUpdateListItems.js
Last active July 7, 2023 13:49
SharePoint REST API Add/ValidateUpdateListItems
See:
- https://robertschouten.com/2018/04/30/update-file-metadata-with-rest-api-using-validateupdatelistitem-method/
- http://blog.arvosys.com/2018/07/06/list-items-system-update-options-in-sharepoint-online/index.html
FieldValues =
[
// Text field (single line and note)
{ FieldName: "TextField", FieldValue: "123" },
// Number field