import { IWebPartContext } from '@microsoft/sp-webpart-base' ; import { SPPermission } from '@microsoft/sp-page-context' ; import { sp } from '../../pnp-preset' ; import { IFields , IFieldAddResult , DateTimeFieldFormatType , CalendarType , DateTimeFieldFriendlyFormatType , ChoiceFieldFormatType , FieldUserSelectionMode , UrlFieldFormatType } from '@pnp/sp/fields' ; export namespace SharePointUtilityModule { export class SharePointUtility { public static checkCurrentUserIsAbleToManageList ( permissions ) { var isAbleToProvision = permissions . hasPermission ( SPPermission . manageLists ) && permissions . hasPermission ( SPPermission . managePermissions ) ; console . log ( "Current user permission: { High:" + permissions . value . High + ",Low:" + permissions . value . Low + "}" ) ; console . log ( "Current user is" + ( isAbleToProvision ? " " : "not " ) + "able to manage lists and permissions." ) ; return isAbleToProvision ; } public static checkListExists ( listTitle ) { return sp . web . lists . getByTitle ( listTitle ) ( ) . then ( _ => true , _ => false ) . catch ( _ => false ) ; } public static createList ( listTitle , listDescription , baseTemplate , enableApproval = true , enableVersioning = false ) { console . log ( `create list ${ listTitle }` ) ; return sp . web . lists . add ( listTitle , listDescription , baseTemplate , true , { EnableModeration : enableApproval , EnableVersioning : enableVersioning } ) . then ( listResponse => listResponse . data ) ; } public static createListField ( listGuid , title , staticName , required , fieldType , fieldTypeKind , more ? , lookuplistGuid ? , lookupFieldName ? ) { console . log ( `create list field ${ title }` ) ; const fieldProperties = { FieldTypeKind : fieldTypeKind , Required : required } ; if ( more != null ) { for ( let i = 0 ; i < Object . getOwnPropertyNames ( more ) . length ; i ++ ) { const propertyName = Object . getOwnPropertyNames ( more ) [ i ] ; fieldProperties [ propertyName ] = more [ propertyName ] ; } } const fields = sp . web . lists . getById ( listGuid ) . fields ; let fieldAddResult ; switch ( fieldTypeKind ) { case 2 : fieldAddResult = fields . addText ( title , undefined , fieldProperties ) ; break ; case 3 : const richText = typeof fieldProperties . RichText !== 'undefined' ? fieldProperties . RichText : false ; delete fieldProperties . RichText ; fieldAddResult = fields . addMultilineText ( title , undefined , richText ) ; break ; case 4 : fieldAddResult = fields . addDateTime ( title , DateTimeFieldFormatType . DateTime , CalendarType . Gregorian , DateTimeFieldFriendlyFormatType . Disabled , fieldProperties ) ; break ; case 6 : const choices = typeof fieldProperties . Choices !== 'undefined' ? fieldProperties . Choices : [ ] ; delete fieldProperties . Choices ; fieldAddResult = fields . addChoice ( title , choices , ChoiceFieldFormatType . Dropdown , false , fieldProperties ) ; break ; case 7 : fieldAddResult = fields . addLookup ( title , lookuplistGuid , lookupFieldName , fieldProperties ) ; break ; case 9 : fieldAddResult = fields . addNumber ( title , undefined , undefined , fieldProperties ) ; break ; case 11 : fieldAddResult = fields . addUrl ( title , undefined , fieldProperties ) ; break ; case 20 : fieldAddResult = fields . addUser ( title , FieldUserSelectionMode . PeopleOnly , fieldProperties ) ; break ; default : fieldAddResult = fields . add ( title , fieldType , fieldProperties ) ; } return fieldAddResult . then ( fieldResult => fieldResult . data . Id ) ; } public static updateListField ( listGuid , fieldGuid , fieldType , change ) { console . log ( `Modify a field ${ fieldGuid } in a SharePoint list ${ listGuid }` ) ; return sp . web . lists . getById ( listGuid ) . fields . getById ( fieldGuid ) . update ( change , fieldType ) ; } public static createGroup ( groupTitle ) { console . log ( `create group ${ groupTitle }` ) ; return sp . web . siteGroups . add ( { Title : groupTitle } ) . then ( groupAddResult => groupAddResult . data ) ; } public static createRole ( roleTitle , high , low ) { console . log ( `create role ${ roleTitle }` ) ; return sp . web . roleDefinitions . add ( roleTitle , '' , 255 , { High : high , Low : low } as any ) . then ( roleDefinitionAddResult => roleDefinitionAddResult . data ) ; } public static addRoleAssignment ( principalid , roleId ) { console . log ( `Assign role ${ roleId } to group/user ${ principalid }` ) ; return sp . web . roleAssignments . add ( Number ( principalid ) , Number ( roleId ) ) . then ( _ => Promise . resolve ( true ) , _ => Promise . resolve ( false ) ) ; } public static getRoleId ( roleName ) { return sp . web . roleDefinitions . filter ( `Name eq '${ roleName }'` ) . get ( ) . then ( roleDefinitions => { if ( roleDefinitions && roleDefinitions . length > 0 ) { return Promise . resolve ( roleDefinitions [ 0 ] . Id . toString ( ) ) ; } else { return Promise . resolve ( '' ) ; } } ) ; } public static breakRoleInheritanceOfList ( listTitle ) { return sp . web . lists . getByTitle ( listTitle ) . breakRoleInheritance ( true ) ; } public static setNewPermissionsForGroup ( listTitle , principalid , roleid ) { return sp . web . lists . getByTitle ( listTitle ) . roleAssignments . add ( Number ( principalid ) , Number ( roleid ) ) ; } public static setListSecurity ( listTitle , readSecurity , writeSecurity ) { return sp . web . lists . getByTitle ( listTitle ) . update ( { ReadSecurity : Number ( readSecurity ) , WriteSecurity : Number ( writeSecurity ) } ) . then ( _ => Promise . resolve ( ) ) ; } } }