import { IContext , IFields } from '../Interfaces' ; import { GeneralHelper } from './GeneralHelper' ; import { ISPField } from '../SPEntities' ; import * as Constants from '../Constants' ; import { ListItemAccessor } from '@microsoft/sp-listview-extensibility' ; import { sp } from '@pnp/sp' ; import { SPHttpClient } from '@microsoft/sp-http' ; declare var window ; /**
 * Helper class to work with SharePoint objects and entities
 */ export class SPHelper { param columnName public static getStoredFieldName ( columnName ) { if ( ! columnName ) return '' ; return Constants . FieldNamesMapping [ columnName ] ? Constants . FieldNamesMapping [ columnName ] . storedName : columnName ; } param fieldId param propertyName param context param fromSchemaXml public static getFieldProperty ( fieldId , propertyName , context , fromSchemaXml ) { return new Promise < any > ( resolve => { let loadedViewFields = SPHelper . _getLoadedViewFieldsFromStorage ( ) ; const viewId = SPHelper . getPageViewId ( context ) ; if ( ! loadedViewFields ) { loadedViewFields = { } ; } if ( ! loadedViewFields [ viewId ] ) { loadedViewFields [ viewId ] = { } ; } let field = loadedViewFields [ viewId ] [ fieldId ] ; if ( ! field ) { field = { Id : fieldId } ; } if ( GeneralHelper . isDefined ( field [ propertyName ] ) ) { resolve ( field [ propertyName ] ) ; return ; } if ( fromSchemaXml ) { SPHelper . getFieldSchemaXmlById ( fieldId , context . pageContext . list . title , context ) . then ( schemaXml => { let fieldValue ; const xml = GeneralHelper . parseXml ( schemaXml ) ; const fieldEls = xml . getElementsByTagName ( 'Field' ) ; if ( fieldEls . length ) { const fieldEl = fieldEls [ 0 ] ; fieldValue = fieldEl . getAttribute ( propertyName ) ; if ( ! GeneralHelper . isDefined ( fieldValue ) ) { fieldValue = fieldEl . textContent ; } } if ( ! GeneralHelper . isDefined ( fieldValue ) ) { fieldValue = '' ; } field [ propertyName ] = fieldValue ; SPHelper . _updateFieldInSessionStorage ( field , context ) ; } , ( error ) => { resolve ( '' ) ; } ) ; } else { sp . web . lists . getByTitle ( context . pageContext . list . title ) . fields . getById ( fieldId ) . select ( propertyName ) . get ( ) . then ( f => { field [ propertyName ] = f [ propertyName ] ; loadedViewFields [ viewId ] [ field . Id ] = field ; SPHelper . _updateSessionStorageLoadedViewFields ( loadedViewFields ) ; resolve ( field ) ; } , ( error ) => { resolve ( '' ) ; } ) ; } } ) ; } param fieldId param context public static getLookupFieldListDispFormUrl ( fieldId , context ) { return new Promise < any > ( ( resolve , reject ) => { let loadedViewFields = SPHelper . _getLoadedViewFieldsFromStorage ( ) ; const viewId = SPHelper . getPageViewId ( context ) ; if ( ! loadedViewFields ) { loadedViewFields = { } ; } if ( ! loadedViewFields [ viewId ] ) { loadedViewFields [ viewId ] = { } ; } let field = loadedViewFields [ viewId ] [ fieldId ] ; if ( ! field ) { field = { Id : fieldId } ; } if ( GeneralHelper . isDefined ( field . LookupDisplayUrl ) ) { resolve ( field . LookupDisplayUrl ) ; return ; } sp . web . lists . getByTitle ( context . pageContext . list . title ) . fields . getById ( fieldId ) . select ( 'LookupWebId' , 'LookupList' ) . get ( ) . then ( f => { sp . site . openWebById ( f [ "LookupWebId" ] ) . then ( openedWeb => { openedWeb . web . select ( 'Url' ) . get ( ) . then ( w => { field . LookupDisplayUrl = `${ w . Url }/_layouts/15/listform.aspx?PageType=4&ListId=${ f [ "LookupList" ] }` ; SPHelper . _updateFieldInSessionStorage ( field , context ) ; resolve ( field . LookupDisplayUrl ) ; } , ( error ) => { reject ( error ) ; } ) ; } ) ; } ) ; } ) ; } param listItem param itemName public static getRowItemValueByName ( listItem , itemName ) { const _values = ( < any > listItem ) . _values ; if ( _values ) { return ( _values as Map < string , any > ) . get ( itemName ) ; } else { return null ; } } param fieldId param listTitle param context public static getFieldSchemaXmlById ( fieldId , listTitle , context ) { return new Promise < string > ( ( resolve ) => { let loadedViewFields = SPHelper . _getLoadedViewFieldsFromStorage ( ) ; const viewId = SPHelper . getPageViewId ( context ) ; if ( ! loadedViewFields ) { loadedViewFields = { } ; } if ( ! loadedViewFields [ viewId ] ) { loadedViewFields [ viewId ] = { } ; } let field = loadedViewFields [ viewId ] [ fieldId ] ; if ( ! field ) { field = { Id : fieldId } ; } if ( GeneralHelper . isDefined ( field . SchemaXml ) ) { resolve ( field . SchemaXml ) ; return ; } sp . web . lists . getByTitle ( listTitle ) . fields . getById ( fieldId ) . select ( 'SchemaXml' ) . get ( ) . then ( ( f ) => { field . SchemaXml = f && f . SchemaXml ; loadedViewFields [ viewId ] [ field . Id ] = field ; SPHelper . _updateSessionStorageLoadedViewFields ( loadedViewFields ) ; resolve ( f ? f . SchemaXml : '' ) ; } , ( error ) => { resolve ( '' ) ; } ) ; } ) ; } param context public static getPageViewId ( context ) { const urlParams = new URLSearchParams ( location . search ) ; let viewIdQueryParam = urlParams . get ( 'viewid' ) ; if ( viewIdQueryParam && viewIdQueryParam . indexOf ( '{' ) !== 0 ) { viewIdQueryParam = `{${ viewIdQueryParam }}` ; } return viewIdQueryParam || context . pageContext . legacyPageContext . viewId ; } param id param context public static async getUserById ( id , context ) { return sp . web . getUserById ( id ) . get ( ) ; } param loginName param context public static async getUserProperties ( loginName , context ) { let url ; url = context . pageContext . web . absoluteUrl ; url = GeneralHelper . trimSlash ( url ) ; url += `/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor('${ encodeURIComponent ( loginName ) }')` ; return context . spHttpClient . get ( url , SPHttpClient . configurations . v1 ) . then ( ( response ) : Promise < any > => { return response . json ( ) ; } ) . then ( ( value ) => { return value ; } ) ; } private static _updateFieldInSessionStorage ( field , context ) { let loadedViewFields = SPHelper . _getLoadedViewFieldsFromStorage ( ) ; if ( ! loadedViewFields ) { loadedViewFields = { } ; } const viewId = SPHelper . getPageViewId ( context ) ; if ( ! loadedViewFields [ viewId ] ) { loadedViewFields [ viewId ] = { } ; } loadedViewFields [ viewId ] [ field . Id ] = field ; SPHelper . _updateSessionStorageLoadedViewFields ( loadedViewFields ) ; } private static _updateSessionStorageLoadedViewFields ( loadedViewFields ) { const sessionStorage = window . sessionStorage ; sessionStorage . setItem ( Constants . LoadedViewFieldsKey , JSON . stringify ( loadedViewFields ) ) ; } private static _getLoadedViewFieldsFromStorage ( ) { const loadedViewFields = sessionStorage . getItem ( Constants . LoadedViewFieldsKey ) ; if ( loadedViewFields ) { return JSON . parse ( loadedViewFields ) ; } return null ; } }