Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save phillipharding/7f65371caea2d86dfa8851a892dabc44 to your computer and use it in GitHub Desktop.

Select an option

Save phillipharding/7f65371caea2d86dfa8851a892dabc44 to your computer and use it in GitHub Desktop.
Useful for updating the vti_indexedpropertykeys property bag item in SharePoint Online, where the key name required is the Base64 encoded Unicode bytes of the property key name
// ==============================================
// ENCODE
/** NODEJS */
const encodedKeyName = Buffer.from("PropertyKeyName", 'utf16le').toString('base64');
/** BROWSER */
function encodeToUtf16LE(str: string): Uint8Array {
const buffer = new ArrayBuffer(str.length * 2);
const view = new Uint16Array(buffer);
for (let i = 0; i < str.length; i++) {
view[i] = str.charCodeAt(i);
}
return new Uint8Array(buffer);
}
/** replace _x005f in key names with "" */
keyBytes = encodeToUtf16LE("PropertyKeyName");
encodedKeyName = btoa(String.fromCharCode(...keyBytes));
// ==============================================
// DECODE
/** NODEJS */
const text = Buffer.from(base64String, 'base64').toString('utf16le');
/** BROWSER */
const text = new TextDecoder('utf-16le').decode(
new Uint8Array([...atob(base64String)].map(c => c.charCodeAt(0)))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment