import { IFieldInfoLookup , ISPCfgFieldInfo , ISPCfgContentTypeInfo , ISPCfgListInfo , ISPCfgViewInfo , ISPConfig , ISPConfigProps } from "../../@types/helper" ; import { SP } from "gd-sprest-def" ; import { ContextInfo , Site , Web } from "../lib" ; import { SPTypes } from ".." ; import { createContentType , setContentTypeFields , Executor , FieldSchemaXML , SPCfgType , SPCfgFieldType } from "." ; export * from "./spCfgTypes" ; /**
 * SharePoint Configuration
 */ export const SPConfig = ( cfg , webUrl ? ) : ISPConfig => { let _cfgType ; let _requestDigest = null ; let _targetName ; /**
     * Methods
     */ let createContentTypes = ( contentTypes , cfgContentTypes , list ? ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( cfgContentTypes == null || cfgContentTypes . length == 0 ) { resolve ( ) ; return ; } let getParentCT = ( ctName , url ) : PromiseLike < { Id : string ; Url : string ; } > => { return new Promise ( ( resolve , reject ) => { Web ( url , { disableCache : true } ) . ContentTypes ( ) . query ( { Filter : "Name eq '" + ctName + "'" } ) . execute ( cts => { if ( cts . results [ 0 ] ) { resolve ( { Id : cts . results [ 0 ] . Id . StringValue , Url : url } ) ; } else if ( url != ContextInfo . siteServerRelativeUrl ) { getParentCT ( ctName , ContextInfo . siteServerRelativeUrl ) . then ( resolve , reject ) ; } else { reject ( ) ; } } , reject ) ; } ) ; } ; Executor < ISPCfgContentTypeInfo > ( cfgContentTypes , cfg => { return new Promise ( ( resolve , reject ) => { let ct = isInCollection ( "Name" , cfg . Name , contentTypes . results ) ; if ( ct ) { console . log ( "[gd-sprest]" + ( list ? "[" + list . Title + " List]" : "" ) + "[Content Type] The content type '" + cfg . Name + "' already exists." ) ; cfg . ContentType = ct ; resolve ( cfg ) ; return ; } console . log ( "[gd-sprest]" + ( list ? "[" + list . Title + " List]" : "" ) + "[Content Type] Creating the '" + cfg . Name + "' content type." ) ; if ( cfg . ParentName ) { getParentCT ( cfg . ParentName , cfg . ParentWebUrl || webUrl ) . then ( parentInfo => { createContentType ( { Description : cfg . Description , Group : cfg . Group , Name : cfg . Name } , parentInfo , webUrl , list ? list . Title : null ) . then ( ct => { console . log ( "[gd-sprest]" + ( list ? "[" + list . Title + " List]" : "" ) + "[Content Type] The content type '" + cfg . Name + "' was created successfully." ) ; cfg . ContentType = ct ; cfg . onCreated ? cfg . onCreated ( ct , list ) : null ; resolve ( cfg ) ; } , error => { console . log ( "[gd-sprest]" + ( list ? "[" + list . Title + " List]" : "" ) + "[Content Type] The content type '" + cfg . Name + "' failed to be created." , error ) ; reject ( error ) ; } ) ; } , ( ) => { console . log ( "[gd-sprest]" + ( list ? "[" + list . Title + " List]" : "" ) + "[Content Type] The parent content type '" + cfg . ParentName + "' was not found." ) ; reject ( ct . response ) ; } ) ; } else { contentTypes . add ( { Description : cfg . Description , Group : cfg . Group , Name : cfg . Name , Id : { __metadata : { type : "SP.ContentTypeId" } , StringValue : cfg . Id || "0x0100" + ContextInfo . generateGUID ( ) . replace ( /-/g , "" ) } as any } ) . execute ( ( ct ) => { console . log ( "[gd-sprest]" + ( list ? "[" + list . Title + " List]" : "" ) + "[Content Type] The content type '" + cfg . Name + "' was created successfully." ) ; cfg . ContentType = ct ; cfg . onCreated ? cfg . onCreated ( ct , list ) : null ; resolve ( cfg ) ; } , error => { console . log ( "[gd-sprest]" + ( list ? "[" + list . Title + " List]" : "" ) + "[Content Type] The content type '" + cfg . Name + "' failed to be created." ) ; console . error ( "[gd-sprest]" + ( list ? "[" + list . Title + " List]" : "" ) + "[Content Type] Error: " + error . response ) ; reject ( error . response ) ; } ) ; } } ) ; } ) . then ( ( ) => { Executor ( cfgContentTypes , cfgContentType => { return new Promise ( ( resolve , reject ) => { let cfgUpdate = { } as any ; let updateFl = false ; if ( cfgContentType . ContentType == null ) { resolve ( null ) ; return ; } console . log ( "[gd-sprest]" + ( list ? "[" + list . Title + " List]" : "" ) + "[Content Type] Updating the field references for: " + cfgContentType . Name ) ; setContentTypeFields ( { fields : cfgContentType . FieldRefs as any , id : cfgContentType . ContentType . Id . StringValue , listName : list ? list . Title : null , webUrl } ) . then ( ( ) => { /**
                             * See if we need to update the properties
                             */ if ( cfgContentType . Description != null && cfgContentType . ContentType . Description != cfgContentType . Description ) { cfgUpdate . Description = cfgContentType . Description ; updateFl = true ; } if ( cfgContentType . Group != null && cfgContentType . ContentType . Group != cfgContentType . Group ) { cfgUpdate . Group = cfgContentType . Group ; updateFl = true ; } if ( cfgContentType . JSLink != null && cfgContentType . ContentType . JSLink != cfgContentType . JSLink ) { cfgUpdate . JSLink = cfgContentType . JSLink ; updateFl = true ; } if ( cfgContentType . Name != null && cfgContentType . ContentType . Name != cfgContentType . Name ) { cfgUpdate . Name = cfgContentType . Name ; updateFl = true ; } if ( updateFl ) { console . log ( "[gd-sprest]" + ( list ? "[" + list . Title + " List]" : "" ) + "[Content Type][" + cfgContentType . ContentType . Name + "] Updating the content type." ) ; cfgContentType . ContentType . update ( cfgUpdate ) . execute ( ( ) => { console . log ( "[gd-sprest]" + ( list ? "[" + list . Title + " List]" : "" ) + "[Content Type][" + cfgContentType . ContentType . Name + "] Update request completed." ) ; cfgContentType . onUpdated ? cfgContentType . onUpdated ( cfgContentType . ContentType ) : null ; resolve ( null ) ; } , reject ) ; } else { cfgContentType . onUpdated ? cfgContentType . onUpdated ( cfgContentType . ContentType ) : null ; resolve ( null ) ; } } , reject ) ; } ) ; } ) . then ( resolve ) ; } , reject ) ; } ) ; } ; let createFields = ( fields , cfgFields , list ? ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) : PromiseLike < void > => { let newFields = [ ] ; if ( cfgFields == null || cfgFields . length == 0 ) { resolve ( ) ; return ; } Executor < ISPCfgFieldInfo > ( cfgFields , cfg => { return new Promise ( ( resolve , reject ) => { let field = isInCollection ( "InternalName" , cfg . name , fields . results ) ; if ( field ) { console . log ( "[gd-sprest][Field] The field '" + cfg . name + "' already exists." ) ; cfg . onUpdated ? cfg . onUpdated ( field , list ) : null ; resolve ( null ) ; } else { console . log ( "[gd-sprest][Field] Creating the '" + cfg . name + "' field." ) ; let cfgLookup = cfg as IFieldInfoLookup ; if ( cfgLookup . type == SPCfgFieldType . Lookup && cfgLookup . fieldRef ) { let fieldRef = isInCollection ( "InternalName" , cfgLookup . fieldRef , fields . results ) || isInCollection ( "InternalName" , cfgLookup . fieldRef , newFields ) ; if ( fieldRef ) { cfgLookup . fieldRef = ( fieldRef as SP . Field ) . Id ; } } FieldSchemaXML ( cfg ) . then ( response => { let schemas = typeof ( response ) === "string" ? [ response ] : response as any ; for ( let i = 0 ; i < schemas . length ; i ++ ) { fields . createFieldAsXml ( schemas [ i ] ) . execute ( ( field ) => { if ( field . InternalName ) { console . log ( "[gd-sprest][Field] The field '" + field . InternalName + "' was created successfully." ) ; newFields . push ( field ) ; cfg . onCreated ? cfg . onCreated ( field , list ) : null ; resolve ( null ) ; } else { console . log ( "[gd-sprest][Field] The field '" + cfg . name + "' failed to be created." ) ; console . error ( "[gd-sprest][Field] Error: " + field . response ) ; reject ( ) ; } } ) ; } } ) ; } } ) ; } ) . then ( resolve ) ; } ) ; } ; let createLists = ( lists , cfgLists ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { Executor < ISPCfgListInfo > ( cfgLists , cfgList => { return new Promise ( ( resolve ) => { if ( _cfgType && _targetName ) { if ( cfgList . ListInformation . Title . toLowerCase ( ) != _targetName ) { resolve ( null ) ; return ; } } let list = isInCollection ( "Title" , cfgList . ListInformation . Title , lists . results ) ; if ( list ) { console . log ( "[gd-sprest][List] The list '" + cfgList . ListInformation . Title + "' already exists." ) ; resolve ( null ) ; return ; } console . log ( "[gd-sprest][List] Creating the '" + cfgList . ListInformation . Title + "' list." ) ; let listInfo = cfgList . ListInformation ; let listName = listInfo . Title ; listInfo . Title = listName . replace ( / /g , "" ) ; lists . add ( listInfo ) . execute ( ( list ) => { listInfo . Title = listName ; if ( list . Id ) { if ( list . Title != listName ) { list . update ( { Title : listName } ) . execute ( ( ) => { console . log ( "[gd-sprest][List] The list '" + list . Title + "' was created successfully." ) ; resolve ( null ) ; } ) ; } else { console . log ( "[gd-sprest][List] The list '" + list . Title + "' was created successfully." ) ; resolve ( null ) ; } cfgList . onCreated ? cfgList . onCreated ( list ) : null ; } else { console . log ( "[gd-sprest][List] The list '" + listInfo . Title + "' failed to be created." ) ; console . log ( "[gd-sprest][List] Error: '" + list . response ) ; resolve ( null ) ; } } , reject ) ; } ) ; } ) . then ( ( ) => { updateLists ( cfgLists ) . then ( ( ) => { resolve ( ) ; } ) ; } ) ; } ) ; } ; let createUserCustomActions = ( customActions , cfgCustomActions ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( _cfgType ) { if ( _cfgType != SPCfgType . SiteUserCustomActions || _cfgType != SPCfgType . WebUserCustomActions ) { resolve ( ) ; return ; } } if ( cfgCustomActions == null || cfgCustomActions . length == 0 ) { resolve ( ) ; return ; } Executor < SP . UserCustomActionProps > ( cfgCustomActions , cfg => { if ( _cfgType && _targetName ) { if ( cfg . Name . toLowerCase ( ) != _targetName || cfg . Title . toLowerCase ( ) != _targetName ) { return ; } } if ( isInCollection ( "Name" , cfg . Name , customActions . results ) ) { console . log ( "[gd-sprest][Custom Action] The custom action '" + cfg . Name + "' already exists." ) ; } else { if ( cfg . Rights ) { cfg . Rights = updateBasePermissions ( cfg . Rights ) as any ; } customActions . add ( cfg ) . execute ( ( ca ) => { if ( ca . existsFl ) { console . log ( "[gd-sprest][Custom Action] The custom action '" + ca . Name + "' was created successfully." ) ; } else { console . log ( "[gd-sprest][Custom Action] The custom action '" + ca . Name + "' failed to be created." ) ; console . log ( "[gd-sprest][Custom Action] Error: " + ca . response ) ; } } , reject , true ) ; } } ) . then ( resolve ) ; } ) ; } ; let createViews = ( list , views , cfgViews ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( cfgViews == null || cfgViews . length == 0 ) { resolve ( ) ; return ; } Executor < ISPCfgViewInfo > ( cfgViews , cfg => { let view = isInCollection ( "Title" , cfg . ViewName , views . results ) ; if ( view ) { console . log ( "[gd-sprest][View] The view '" + cfg . ViewName + "' already exists." ) ; } else { views . add ( { Title : cfg . ViewName , ViewQuery : cfg . ViewQuery } ) . execute ( ( view ) => { if ( view . existsFl ) { console . log ( "[gd-sprest][View] The view '" + cfg . ViewName + "' was created successfully." ) ; cfg . onCreated ? cfg . onCreated ( view , list ) : null ; } else { console . log ( "[gd-sprest][View] The view '" + cfg . ViewName + "' failed to be created." ) ; console . log ( "[gd-sprest][View] Error: " + view . response ) ; } } , reject , true ) ; } } ) . then ( ( ) => { updateViews ( list , views , cfgViews ) . then ( ( ) => { resolve ( ) ; } ) ; } ) ; } ) ; } ; let createWebParts = ( ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { let cfgWebParts = cfg . WebPartCfg ; if ( cfgWebParts == null || cfgWebParts . length == 0 ) { resolve ( ) ; return ; } console . log ( "[gd-sprest][WebPart] Creating the web parts." ) ; Web ( webUrl , { disableCache : true , requestDigest : _requestDigest } ) . getCatalog ( SPTypes . ListTemplateType . WebPartCatalog ) . RootFolder ( ) . query ( { Expand : [ "Files" ] } ) . execute ( folder => { let ctr = 0 ; for ( let i = 0 ; i < cfgWebParts . length ; i ++ ) { let cfgWebPart = cfgWebParts [ i ] ; if ( _cfgType && _targetName ) { if ( cfgWebPart . FileName . toLowerCase ( ) != _targetName ) { continue ; } } let postExecute = ( ) => { if ( ++ ctr >= cfgWebParts . length ) { resolve ( ) ; } } ; let file = isInCollection ( "Name" , cfgWebPart . FileName , folder . Files . results ) ; if ( file . Name ) { console . log ( "[gd-sprest][WebPart] The webpart '" + cfgWebPart . FileName + "' already exists." ) ; cfgWebPart . onUpdated ? cfgWebPart . onUpdated ( file ) : null ; postExecute ( ) ; } else { let xml = cfgWebPart . XML . trim ( ) ; let buffer = new ArrayBuffer ( xml . length * 2 ) ; let bufferView = new Uint16Array ( buffer ) ; for ( let j = 0 ; j < xml . length ; j ++ ) { bufferView [ j ] = xml . charCodeAt ( j ) ; } folder . Files . add ( cfgWebPart . FileName , true , buffer ) . execute ( ( file ) => { if ( cfgWebPart . Group ) { Web ( ContextInfo . siteServerRelativeUrl , { disableCache : true } ) . getCatalog ( SPTypes . ListTemplateType . WebPartCatalog ) . Items ( ) . query ( { Filter : "FileLeafRef eq '" + cfgWebPart . FileName + "'" } ) . execute ( ( items ) => { items . results [ 0 ] . update ( { Group : cfgWebPart . Group } ) . execute ( postExecute ) ; } ) ; } console . log ( "[gd-sprest][WebPart] The '" + file . Name + "' webpart file was uploaded successfully." ) ; cfgWebPart . onCreated ? cfgWebPart . onCreated ( file ) : null ; } , ( ) => { console . log ( "[gd-sprest][WebPart] The '" + file . Name + "' webpart file upload failed." ) ; resolve ( ) ; } ) ; } } } , reject ) ; } ) ; } ; let isInCollection = ( key , value , collection ) => { if ( value ) { let valueLower = value ? value . toLowerCase ( ) : "" ; for ( let i = 0 ; i < collection . length ; i ++ ) { let keyValue = collection [ i ] [ key ] ; keyValue = keyValue ? keyValue . toLowerCase ( ) : "" ; if ( valueLower == keyValue ) { return collection [ i ] ; } } } return false ; } ; let removeContentTypes = ( contentTypes , cfgContentTypes ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( cfgContentTypes == null || cfgContentTypes . length == 0 ) { resolve ( ) ; return ; } Executor < ISPCfgContentTypeInfo > ( cfgContentTypes , cfg => { let ct = isInCollection ( "Name" , cfg . Name , contentTypes . results ) ; if ( ct ) { ct . delete ( ) . execute ( ( ) => { console . log ( "[gd-sprest][Content Type] The content type '" + ct . Name + "' was removed." ) ; } , reject , true ) ; } } ) . then ( resolve ) ; } ) ; } ; let removeFields = ( fields , cfgFields ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( cfgFields == null || cfgFields . length == 0 ) { resolve ( ) ; return ; } Executor < ISPCfgFieldInfo > ( cfgFields , cfg => { let field = isInCollection ( "InternalName" , cfg . name , fields . results ) ; if ( field ) { field . delete ( ) . execute ( ( ) => { console . log ( "[gd-sprest][Field] The field '" + field . InternalName + "' was removed." ) ; } , reject , true ) ; } } ) . then ( resolve ) ; } ) ; } ; let removeLists = ( lists , cfgLists ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( _cfgType ) { if ( _cfgType != SPCfgType . Lists ) { resolve ( ) ; return ; } } if ( cfgLists == null || cfgLists . length == 0 ) { resolve ( ) ; return ; } Executor < ISPCfgListInfo > ( cfgLists , cfg => { if ( _cfgType && _targetName ) { if ( cfg . ListInformation . Title . toLowerCase ( ) != _targetName ) { return ; } } let list = isInCollection ( "Title" , cfg . ListInformation . Title , lists . results ) ; if ( list ) { list . delete ( ) . execute ( ( ) => { console . log ( "[gd-sprest][List] The list '" + list . Title + "' was removed." ) ; } , reject , true ) ; } } ) . then ( resolve ) ; } ) ; } ; let removeUserCustomActions = ( customActions , cfgCustomActions ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( _cfgType ) { if ( _cfgType != SPCfgType . SiteUserCustomActions || _cfgType != SPCfgType . WebUserCustomActions ) { resolve ( ) ; return ; } } if ( cfgCustomActions == null || cfgCustomActions . length == 0 ) { resolve ( ) ; return ; } Executor < SP . UserCustomActionProps > ( cfgCustomActions , cfg => { if ( _cfgType && _targetName ) { if ( cfg . Name . toLowerCase ( ) != _targetName || cfg . Title . toLowerCase ( ) != _targetName ) { return ; } } let ca = isInCollection ( "Name" , cfg . Name , customActions . results ) ; if ( ca ) { ca . delete ( ) . execute ( ( ) => { console . log ( "[gd-sprest][Custom Action] The custom action '" + ca . Name + "' was removed." ) ; } , reject , true ) ; } } ) . then ( resolve ) ; } ) ; } ; let removeWebParts = ( site ) : PromiseLike < void > => { let cfgWebParts = cfg . WebPartCfg ; return new Promise ( ( resolve , reject ) => { if ( _cfgType ) { if ( _cfgType != SPCfgType . WebParts ) { resolve ( ) ; return ; } } if ( cfgWebParts == null || cfgWebParts . length == 0 ) { resolve ( ) ; return ; } console . log ( "[gd-sprest][WebPart] Removing the web parts." ) ; site . RootWeb ( ) . getCatalog ( SPTypes . ListTemplateType . WebPartCatalog ) . RootFolder ( ) . Files ( ) . execute ( files => { for ( let i = 0 ; i < cfgWebParts . length ; i ++ ) { let cfgWebPart = cfgWebParts [ i ] ; if ( _cfgType && _targetName ) { if ( cfgWebPart . FileName . toLowerCase ( ) != _targetName ) { continue ; } } let file = isInCollection ( "Name" , cfgWebPart . FileName , files . results ) ; if ( file ) { file . delete ( ) . execute ( ( ) => { console . log ( "[gd-sprest][WebPart] The webpart '" + file . Name + "' file was removed." ) ; } , true ) ; } } resolve ( ) ; } , reject ) ; } ) ; } ; let setRequestDigest = ( ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( webUrl ) { ContextInfo . getWeb ( webUrl ) . execute ( webInfo => { _requestDigest = webInfo . GetContextWebInformation . FormDigestValue ; resolve ( ) ; } , reject ) ; } else { resolve ( ) ; } } ) ; } ; let updateBasePermissions = ( values ) => { let high = values . High ; let low = values . Low ; for ( let i = 0 ; i < values [ "length" ] ; i ++ ) { let value = values [ i ] ; if ( value == 65 ) { low = 65535 ; high = 32767 ; break ; } else if ( value == 0 ) { low = 0 ; high = 0 ; } else { let bit = value - 1 ; let bitValue = 1 ; if ( bit < 0 ) { continue ; } if ( bit < 32 ) { bitValue = bitValue << bit ; low |= bitValue ; } else { bitValue = bitValue << ( bit - 32 ) ; high |= bitValue ; } } } return { Low : low . toString ( ) , High : high . toString ( ) } ; } ; let updateLists = ( cfgLists ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { let request = ( idx , resolve ) => { let cfgList = cfgLists [ idx ] ; if ( _targetName ) { if ( cfgList . ListInformation . Title . toLowerCase ( ) != _targetName ) { request ( idx + 1 , resolve ) ; return ; } } if ( cfgList ) { Web ( webUrl , { disableCache : true , requestDigest : _requestDigest } ) . Lists ( cfgList . ListInformation . Title ) . query ( { Expand : [ "ContentTypes" , "Fields" , "UserCustomActions" , "Views" ] } ) . execute ( list => { updateListTitleField ( list , cfgList ) . then ( ( ) => { createFields ( list . Fields , cfgList . CustomFields , list as any ) . then ( ( ) => { createContentTypes ( list . ContentTypes , cfgList . ContentTypes , list as any ) . then ( ( ) => { createViews ( list as any , list . Views , cfgList . ViewInformation ) . then ( ( ) => { createUserCustomActions ( list . UserCustomActions , cfgList . UserCustomActions ) . then ( ( ) => { cfgList . onUpdated ? cfgList . onUpdated ( list as any ) : null ; request ( idx + 1 , resolve ) ; } , reject ) ; } , reject ) ; } , reject ) ; } , reject ) ; } , reject ) ; } , reject ) ; } else { resolve ( ) ; } } ; request ( 0 , resolve ) ; } ) ; } ; let updateListTitleField = ( list , cfgList ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( cfgList . TitleFieldDisplayName ) { list . Fields . getByInternalNameOrTitle ( "Title" ) . update ( { Title : cfgList . TitleFieldDisplayName } ) . execute ( ( ) => { console . log ( "[gd-sprest][List] The 'Title' field's display name was updated to '" + cfgList . TitleFieldDisplayName + "'." ) ; resolve ( ) ; } , reject ) ; } else { resolve ( ) ; } } ) ; } ; let updateViews = ( list , views , cfgViews ) : PromiseLike < void > => { return new Promise ( ( resolve ) => { Executor < ISPCfgViewInfo > ( cfgViews , cfg => { return new Promise ( ( resolve ) => { let view = views . getByTitle ( cfg . ViewName ) ; if ( cfg . ViewFields && cfg . ViewFields . length > 0 ) { console . log ( "[gd-sprest][View] Updating the view fields for the '" + cfg . ViewName + "' view." ) ; view . ViewFields ( ) . removeAllViewFields ( ) . execute ( true ) ; for ( let i = 0 ; i < cfg . ViewFields . length ; i ++ ) { view . ViewFields ( ) . addViewField ( cfg . ViewFields [ i ] ) . execute ( true ) ; } } if ( cfg . JSLink || cfg . ViewQuery ) { let props = { } ; console . log ( "[gd-sprest][View] Updating the view properties for the '" + cfg . ViewName + "' view." ) ; cfg . JSLink ? props [ "JSLink" ] = cfg . JSLink : null ; cfg . ViewQuery ? props [ "ViewQuery" ] = cfg . ViewQuery : null ; view . update ( props ) . execute ( true ) ; } view . done ( ( ... args ) => { console . log ( "[gd-sprest][View] The updates for the '" + cfg . ViewName + "' view has completed." ) ; cfg . onUpdated ? cfg . onUpdated ( view as any , list ) : null ; resolve ( null ) ; } ) ; } ) ; } ) . then ( resolve ) ; } ) ; } ; let uninstallSite = ( ) : PromiseLike < SP . ISiteQuery > => { return new Promise ( ( resolve , reject ) => { if ( ( cfg . CustomActionCfg != null && cfg . CustomActionCfg . Site != null ) || cfg . WebPartCfg != null ) { console . log ( "[gd-sprest][uninstall] Loading the site information..." ) ; Site ( webUrl , { disableCache : true , requestDigest : _requestDigest } ) . query ( { Expand : [ "UserCustomActions" ] } ) . execute ( site => { removeUserCustomActions ( site . UserCustomActions , cfg . CustomActionCfg ? cfg . CustomActionCfg . Site : [ ] ) . then ( ( ) => { removeWebParts ( site as any ) . then ( ( ) => { resolve ( site ) ; } , reject ) ; } ) ; } , reject ) ; } else { resolve ( null ) ; } } ) ; } ; let uninstallWeb = ( ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { let Expand = [ ] ; console . log ( "[gd-sprest][uninstall] Loading the web information..." ) ; if ( cfg . ContentTypes ) { Expand . push ( "ContentTypes" ) ; } if ( cfg . CustomActionCfg ) { Expand . push ( "UserCustomActions" ) ; } if ( cfg . Fields ) { Expand . push ( "Fields" ) ; } if ( cfg . ListCfg ) { Expand . push ( "Lists" ) ; } Web ( webUrl , { disableCache : true , requestDigest : _requestDigest } ) . query ( { Expand } ) . execute ( web => { removeUserCustomActions ( web . UserCustomActions , cfg . CustomActionCfg ? cfg . CustomActionCfg . Web : null ) . then ( ( ) => { removeLists ( web . Lists , cfg . ListCfg ) . then ( ( ) => { removeContentTypes ( web . ContentTypes , cfg . ContentTypes ) . then ( ( ) => { removeFields ( web . Fields , cfg . Fields ) . then ( ( ) => { resolve ( ) ; } , reject ) ; } , reject ) ; } , reject ) ; } , reject ) ; } , reject ) ; } ) ; } ; /**
     * Public Interface
     */ return { _configuration : cfg , install : ( ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { setRequestDigest ( ) . then ( ( ) => { console . log ( "[gd-sprest] Installing the web assets..." ) ; let web = Web ( webUrl , { disableCache : true , requestDigest : _requestDigest } ) ; let createSiteFields = ( ) => { return new Promise ( ( resolve , reject ) => { if ( cfg . Fields && cfg . Fields . length > 0 ) { console . log ( "[gd-sprest][Fields] Starting the requests." ) ; web . Fields ( ) . execute ( fields => { createFields ( fields , cfg . Fields ) . then ( ( ) => { console . log ( "[gd-sprest][Fields] Completed the requests." ) ; resolve ( null ) ; } , reject ) ; } , reject ) ; } else { resolve ( null ) ; } } ) ; } ; let createSiteContentTypes = ( ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( cfg . ContentTypes && cfg . ContentTypes . length > 0 ) { console . log ( "[gd-sprest][Content Types] Starting the requests." ) ; web . ContentTypes ( ) . execute ( contentTypes => { createContentTypes ( contentTypes , cfg . ContentTypes ) . then ( ( ) => { console . log ( "[gd-sprest][Content Types] Completed the requests." ) ; resolve ( ) ; } , reject ) ; } , reject ) ; } else { resolve ( ) ; } } ) ; } ; let createSiteLists = ( ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( cfg . ListCfg && cfg . ListCfg . length ) { console . log ( "[gd-sprest][Lists] Starting the requests." ) ; web . Lists ( ) . execute ( lists => { createLists ( lists , cfg . ListCfg ) . then ( ( ) => { console . log ( "[gd-sprest][Lists] Completed the requests." ) ; resolve ( ) ; } , reject ) ; } , reject ) ; } else { resolve ( ) ; } } ) ; } ; let createSiteWebParts = ( ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( cfg . WebPartCfg && cfg . WebPartCfg . length > 0 ) { console . log ( "[gd-sprest][WebParts] Starting the requests." ) ; createWebParts ( ) . then ( ( ) => { console . log ( "[gd-sprest][WebParts] Completed the requests." ) ; resolve ( ) ; } , reject ) ; } else { resolve ( ) ; } } ) ; } ; let createSiteCollectionCustomActions = ( ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( cfg . CustomActionCfg && cfg . CustomActionCfg . Site ) { console . log ( "[gd-sprest][Site Custom Actions] Starting the requests." ) ; Site ( webUrl , { disableCache : true , requestDigest : _requestDigest } ) . UserCustomActions ( ) . execute ( customActions => { createUserCustomActions ( customActions , cfg . CustomActionCfg . Site ) . then ( ( ) => { console . log ( "[gd-sprest][Site Custom Actions] Completed the requests." ) ; resolve ( ) ; } , reject ) ; } , reject ) ; } else { resolve ( ) ; } } ) ; } ; let createSiteCustomActions = ( ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { if ( cfg . CustomActionCfg && cfg . CustomActionCfg . Web ) { console . log ( "[gd-sprest][Web Custom Actions] Starting the requests." ) ; web . UserCustomActions ( ) . execute ( customActions => { createUserCustomActions ( customActions , cfg . CustomActionCfg . Web ) . then ( ( ) => { console . log ( "[gd-sprest][Web Custom Actions] Completed the requests." ) ; resolve ( ) ; } ) ; } , reject ) ; } else { resolve ( ) ; } } ) ; } ; createSiteFields ( ) . then ( ( ) => { createSiteContentTypes ( ) . then ( ( ) => { createSiteLists ( ) . then ( ( ) => { createSiteWebParts ( ) . then ( ( ) => { createSiteCollectionCustomActions ( ) . then ( ( ) => { createSiteCustomActions ( ) . then ( ( ) => { console . log ( "[gd-sprest] The configuration script completed, but some requests may still be running." ) ; resolve ( ) ; } , reject ) ; } , reject ) ; } , reject ) ; } , reject ) ; } , reject ) ; } , reject ) ; } , reject ) ; } ) ; } , uninstall : ( ) : PromiseLike < void > => { return new Promise ( ( resolve , reject ) => { setRequestDigest ( ) . then ( ( ) => { uninstallWeb ( ) . then ( ( ) => { uninstallSite ( ) . then ( ( ) => { console . log ( "[gd-sprest] The configuration script completed, but some requests may still be running." ) ; resolve ( ) ; } , reject ) ; } , reject ) ; } ) ; } ) ; } } ; } ;