Skip to content

Instantly share code, notes, and snippets.

@rgriffith
Last active October 12, 2018 14:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgriffith/241afacf7f9a50108da81cd8136a02cf to your computer and use it in GitHub Desktop.
Save rgriffith/241afacf7f9a50108da81cd8136a02cf to your computer and use it in GitHub Desktop.
Uses Cascade CMS REST API to iterate over XHTML/SD Blocks which subscribe to a specific Data Definition and adds a tag to each one.
// To install module, run: `npm i node-fetch`
const fetch = require("node-fetch");
const AUTHENTICATION = {username: '...', password: '...'};
const REST_API_URL = 'https://CASCADE_URL/api/v1/';
const SITE_NAME = 'www.example.com';
const DATA_DEFINITION_NAME = 'foo';
const TAG_TO_PUSH = 'bar';
function postData(restEndpoint, data = {}) {
data.authentication = AUTHENTICATION;
return fetch(REST_API_URL + restEndpoint, {
method: "POST",
headers: {"Content-Type": "application/json; charset=utf-8"},
body: JSON.stringify(data),
})
.then(response => response.json());
}
postData('listSubscribers/structureddatadefinition/'+SITE_NAME+'/'+DATA_DEFINITION_NAME)
.then(data => {
if (!data.subscribers.length) {
console.log('No subscribers found.');
return;
}
data.subscribers.forEach(subscriber => {
if (subscriber.type !== 'block_XHTML_DATADEFINITION') {
console.log('Subscriber was not an XHTML/SD Block, skipping.');
return;
}
console.log('Reading Block with ID: ', subscriber.id);
postData('read/block/' + subscriber.id)
.then(readResponse => {
let blockData = readResponse.asset.xhtmlDataDefinitionBlock;
console.log('Updating Block tags');
blockData.tags.push({name: TAG_TO_PUSH});
postData('edit/block/' + subscriber.id, {
asset: {
xhtmlDataDefinitionBlock: blockData
}
})
.then(editResponse => console.log('Updated Block tags'))
.catch(error => console.error(error));
})
.catch(error => console.error(error));
});
})
.catch(error => console.error(error));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment