Created
January 7, 2020 14:59
-
-
Save kaffeskogen/16d72b9013043bfa15abd854871b4cfa 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...'); | |
| await new Promise(done => SP.SOD.executeFunc('sp.js', 'SP.ClientContext', done)); | |
| 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); | |
| const ctx = SP.ClientContext.get_current(); | |
| const web = ctx.get_web(); | |
| const list = web.get_lists().getByTitle('Anställningar'); | |
| const items = list.getItems(SP.CamlQuery.createAllItemsQuery()); | |
| ctx.load(items); | |
| await ctx.execute(); | |
| const itemArr = items.toArray(); | |
| for (const item of itemArr) { | |
| try { | |
| const idGuid = item.get_item('IDGUID'); | |
| const nearestBossUserName = item.get_item('NearestBoss').get_lookupValue(); | |
| if (!idGuid || !nearestBossUserName) { | |
| console.log('idguid or nearestBoss is not valid'); | |
| continue; | |
| } | |
| const nearestBoss = web.ensureUser(nearestBossUserName); | |
| const hrGroup = web.get_siteGroups().getByName('HR'); | |
| const listItem = web.getFolderByServerRelativeUrl('/sites/hr/Documents/' + idGuid).get_listItemAllFields(); | |
| const roleDef = web.get_roleDefinitions().getByName("Delta"); | |
| const collRoleDefinitionBinding = SP.RoleDefinitionBindingCollection.newObject(ctx); | |
| collRoleDefinitionBinding.add(roleDef); | |
| listItem.resetRoleInheritance(); | |
| listItem.breakRoleInheritance(false, false); | |
| const roleAssignements = listItem.get_roleAssignments(); | |
| roleAssignements.add(hrGroup, collRoleDefinitionBinding); | |
| roleAssignements.add(nearestBoss, collRoleDefinitionBinding); | |
| console.log(`Updating ${idGuid} with ${nearestBossUserName}`) | |
| await ctx.execute(); | |
| } catch(e) { | |
| console.error(item.get_item('IDGUID'), e); | |
| } | |
| } | |
| console.log('%cDone!', 'color: green;'); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment