JavaScript to list K2 objects

  • 13 July 2023
  • 0 replies
  • 59 views

Userlevel 3
Badge +5

Thought this script might be helpful. it lists all objects in K2, and then copy and paste them into Excel. It allows you to check versions of objects and makes it effortless to see who has an item checked out.

In K2 Designer, press F12 for DevTools and paste the code into the console. It will recursively process all categories and at the end, console log the details. Click the Copy link at the end of the output. The data will paste into Excel in columns.

Edit the skips array to exclude any category that’s not relevant to process.

Edit the types array to specify the types of objects to list.

If you know the id of a category folder, you can process just that category by changing the 1 in getArtefacts(1) to the relevant id.

async function getCategory(f) {
let url = document.location.origin + '/Designer/AppStudio/AJAXCall.ashx'
let res = await $.ajax({
data: $.param(f),
global: !0,
url,
type: 'POST',
async: true,
})
return res
}

async function getArtefacts(categoryId) {
let types = ['View', 'Form', 'Workflow', 'smartobject', 'StyleProfile']
let skips = ['System', 'Workflow', 'Workflow Notifications', 'Workflow Reports', 'Test', 'Task Allocation', 'Sandbox']
let itemArray = ''
let f = {
action: 'list',
datatype: 'all',
filter: 'category',
dataType: 'xml',
typefilter: 'smartobject|view|form|styleprofile|workflow|category|systemobject',
systemobjects: false
}

async function processCategory(categoryId) {
let dateLocale = 'en-AU'
f.catid = categoryId

let httpResponse = await getCategory(f)

let breadcrumb = httpResponse.querySelector('breadcrumbs').text
let catName = breadcrumb.substring(breadcrumb.lastIndexOf('>') + 1).trim()

console.log(`Processing category: ${catName} ID: ${categoryId}`, httpResponse)

if (!skips.includes(catName)) {
for (let i = 0; i < types.length; i++) {
let artefacts = httpResponse.querySelectorAll(types[i])
if (artefacts && artefacts.length) {
artefacts.forEach((artefact) => {
if (types[i]==='smartobject') {
let mDate = Date.parse(artefact.children[2].attributes['DateValue'].text)
let cDate = Date.parse(artefact.children[3].attributes['DateValue'].text)
itemArray += categoryId + '\t' + breadcrumb + '\t' + catName + '\tSmartObject\t' +
artefact.children[0].text + '\t' + artefact.children[0].text + '\t\t' +
artefact.children[2].text + '\t' + mDate.toLocaleDateString(dateLocale) + ' ' + mDate.toLocaleTimeString(dateLocale) + '\t' +
artefact.children[3].text + '\t' + cDate.toLocaleDateString(dateLocale) + ' ' + cDate.toLocaleTimeString(dateLocale) + '\t' +
artefact.children[4].text + '\t' + artefact.children[5].text + '\r\n'
} else {
let mDate = Date.parse(artefact.children[4].attributes['DateValue'].text)
let cDate = Date.parse(artefact.children[5].attributes['DateValue'].text)
itemArray += categoryId + '\t' + breadcrumb + '\t' + catName + '\t' + types[i] + '\t' +
artefact.children[1].text + '\t' + artefact.children[0].text + '\t' + artefact.children[3].text + '\t' +
artefact.children[4].text + '\t' + mDate.toLocaleDateString(dateLocale) + ' ' + mDate.toLocaleTimeString(dateLocale) + '\t' +
artefact.children[5].text + '\t' + cDate.toLocaleDateString(dateLocale) + ' ' + cDate.toLocaleTimeString(dateLocale) + '\t' +
artefact?.attributes['Guid']?.text + '\t' + artefact.attributes['Version'].text + '\r\n'
}
})
}
}

let categories = httpResponse.querySelectorAll('categories Category')
for (let i = 0; i < categories.length; i++) {
if (categories[i].attributes['ID']?.text && !skips.includes(catName)) {
await processCategory(categories[i].attributes['ID']?.text)
}
}
}
}

await processCategory(categoryId)
console.log('CategoryId\tFullCategory\tCategory\tType\tDisplayName\tSystemName\tStatus\tModifiedBy\tModifiedDate\tCreatedBy\tCreateDate\tGUID\tVersion\r\n' + itemArray)
}
getArtefacts(1)

 


0 replies

Be the first to reply!

Reply