Last active
November 30, 2022 13:46
-
-
Save kaffeskogen/0877252d85c04caf5a3aa55314646c1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (async () => { | |
| console.log('starting...'); | |
| const webUrl = '<weburl>' | |
| if (!window.SP) { | |
| for (const src of ['init.js','MicrosoftAjax.js','SP.Runtime.js','SP.js','sp.workflowservices.js']) { | |
| await new Promise(done => { | |
| const script = document.createElement('script'); | |
| script.async = true; | |
| script.onload = () => done(); | |
| script.src = [webUrl, '_layouts/15', src].join('/'); | |
| document.head.appendChild(script); | |
| }) | |
| } | |
| } | |
| console.log('scripts loaded...'); | |
| SP.ClientContext.prototype.execute = function() { | |
| return new Promise((resolve,reject) => | |
| this.executeQueryAsync(resolve, (sender, args) => reject(args.get_message())) | |
| ); | |
| }; | |
| const toArray = function() { | |
| const arr = []; | |
| for (const en = this.getEnumerator(); en.moveNext();) | |
| arr.push(en.get_current()); | |
| return arr; | |
| }; | |
| Object.keys(SP) | |
| .filter(k => k.includes('Collection')) | |
| .forEach(k => SP[k].prototype.toArray = toArray); | |
| Object.keys(SP.WorkflowServices) | |
| .filter(k => k.includes('Collection')) | |
| .forEach(k => SP.WorkflowServices[k].prototype.toArray = toArray); | |
| const ctx = SP.ClientContext.get_current(); | |
| const web = ctx.get_web(); | |
| const wfManager = new SP.WorkflowServices.WorkflowServicesManager(ctx, web); | |
| const wfInstanceService = wfManager.getWorkflowInstanceService(); | |
| const wfSubscriptionService = wfManager.getWorkflowSubscriptionService(); | |
| const subscription = wfSubscriptionService.getSubscription('<subscriptionid>'); | |
| console.log('Getting folders...'); | |
| const list = web.get_lists().getByTitle('<libraryname>'); | |
| ctx.load(list); | |
| const childFolders = list.get_rootFolder().get_folders(); | |
| ctx.load(childFolders, 'Include(Name,Files.Include(ListItemAllFields))'); | |
| await ctx.execute(); | |
| for (const folder of childFolders.toArray()) { | |
| if (['Forms'].includes(folder.get_name())) { | |
| continue; | |
| } | |
| console.log('Looking through folder ' + folder.get_name()); | |
| const folderItems = folder | |
| .get_files() | |
| .toArray() | |
| .map(i => i.get_listItemAllFields()); | |
| console.log('Terminating workflows'); | |
| for (const item of folderItems) { | |
| const itemId = item.get_id(); | |
| console.log(itemId); | |
| const wfInstances = wfInstanceService.enumerateInstancesForListItem(list.get_id().toString(), itemId); | |
| ctx.load(wfInstances); | |
| await ctx.execute() | |
| const instances = wfInstances.toArray(); | |
| for (const instance of instances) { | |
| wfInstanceService.terminateWorkflow(instance); | |
| } | |
| } | |
| await ctx.execute(); | |
| console.log('Starting workflows'); | |
| for (const item of folderItems) { | |
| const itemId = item.get_id(); | |
| console.log(itemId); | |
| wfInstanceService.startWorkflowOnListItem(subscription, itemId, new Object()); | |
| } | |
| await ctx.execute(); | |
| } | |
| console.log('%cDone!', 'color: green;'); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment