Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add/Set and List PropertyBag Values for a List Folder
/*
Hit 'ctrl + d' or 'cmd + d' to run the code, view console for results
*/
import { spPost, SharePointQueryable } from '@pnp/sp';
import { sp } from "@pnp/sp/presets/all";
const getBody = (siteid, webid, listid, folderid, propertyName, propertyValue) => `
<Request AddExpandoFieldTypeSuffix="true" ApplicationName=".NET Library" LibraryVersion="16.0.0.0" SchemaVersion="15.0.0.0" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">
<Actions>
<Method Id="62" Name="SetFieldValue" ObjectPathId="59">
<Parameters>
<Parameter Type="String">${propertyName}</Parameter>
<Parameter Type="String">${propertyValue}</Parameter>
</Parameters>
</Method>
<Method Id="63" Name="Update" ObjectPathId="52"/>
<Method Id="64" Name="Update" ObjectPathId="32" Version="16"/>
</Actions>
<ObjectPaths>
<Property Id="59" Name="Properties" ParentId="52"/>
<Identity Id="52" Name="3341e39e-b0eb-8000-9ccc-e934ba84f390|740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:${siteid}:web:${webid}:folder:${folderid}"/>
<Identity Id="32" Name="3341e39e-b0eb-8000-9ccc-e934ba84f390|740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:${siteid}:web:${webid}:list:${listid}"/>
</ObjectPaths>
</Request>`;
const setListFolderProperty = async (siteUrl, siteid, webid, listid, folderid, propertyName, propertyValue) => {
const endpoint = `${siteUrl}/_vti_bin/client.svc/ProcessQuery`;
const options = {
url: `${endpoint}`,
headers: {
'Accept': '*/*',
'Content-Type': 'text/xml;charset="UTF-8"',
'X-Requested-With': 'XMLHttpRequest'
},
body: getBody(siteid, webid, listid, folderid, propertyName, propertyValue)
};
const response = await spPost(SharePointQueryable(`${siteUrl}`, "_vti_bin/client.svc/ProcessQuery"), options);
console.log("ProcessQuery response", response);
return response;
};
(async () => {
console.clear();
const site = await sp.site.select("Id", "Title","RootWeb/Id","RootWeb/Title","RootWeb/ServerRelativeUrl").expand("RootWeb").get();
const web = await sp.web.select("Title", "ServerRelativeUrl", "Id", "SiteID").get();
console.log("Site ID: ", site.Id);
console.log("Web Title: ", site.RootWeb.Title);
console.log("Web URL: ", site.RootWeb.ServerRelativeUrl);
console.log("Web ID: ", site.RootWeb.Id);
const list = await sp.web.getList(`${web.ServerRelativeUrl}/DataExtractArchive`).select("ID", "Title", "RootFolder").expand("RootFolder").get();
console.log("List", list);
/* const folder = await sp.web.getFolderByServerRelativePath(`${web.ServerRelativeUrl}/DataExtractArchive`).select("UniqueID", "ServerRelativeUrl").get();
console.log("Folder", folder);
*/
//const folderProps = await sp.web.getFolderByServerRelativePath(`${list.RootFolder.ServerRelativeUrl}`).properties.get();
const folderProps = await sp.web.getFolderByServerRelativePath(`${list.RootFolder.ServerRelativeUrl}`).properties.select("DataExtractSessionEES","DataExtractSessionLMS","DataExtractSessionCH","DataExtractSessionLB","DataExtractSessionCM").get();
console.log("Folder Props", folderProps);
for(let key in folderProps) {
if ((key.match(/^TESTPROPERTY|^DATAEXTRACTSESSION/gi)) && (folderProps[key].match(/^\{/gi))) {
console.log(`- ${key} (JSON) = `, JSON.parse(folderProps[key]));
} else {
console.log(`- ${key} = ${folderProps[key]}`);
}
}
const extractOperation = {
status: "Pending",
servicename: "EES",
dataextracttoken: "EES_1a26258e1d1c4cce9042578580ab5bdf",
date: "2021-02-12T15:29:33.441Z",
folderserverrelativeurl: "/sites/eelhrportaldev/DataExtractArchive/2021/01-Jan/EES_1a26258e1d1c4cce9042578580ab5bdf",
by: "Phillip Allan-Harding",
files: [
{ name: "Users1.csv", url: "Users1.csv" },
{ name: "Users2.csv", url: "Users2.csv" },
{ name: "Users3.csv", url: "Users3.csv" },
{ name: "Users4.csv", url: "Users4.csv" }
]
};
const propertyValue = JSON.stringify(extractOperation);
const siteUrl = `https://platinumdogsconsulting.sharepoint.com${web.ServerRelativeUrl}`;
//const setFolderPropResponse = await setListFolderProperty(siteUrl, site.Id, web.Id, list.Id, list.RootFolder.UniqueId, "DataExtractSessionEES", propertyValue);
//console.log("Set Folder Prop", setFolderPropResponse);
})().catch(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment