import { IContext , IFields } from '../Interfaces' ; import { GeneralHelper } from './GeneralHelper' ; import { ISPField , ISPFieldLookupValue , IPrincipal , ITerm } from '../SPEntities' ; import * as Constants from '../Constants' ; import { ListItemAccessor } from '@microsoft/sp-listview-extensibility' ; import { SPField } from '@microsoft/sp-page-context' ; import { sp } from '@pnp/sp' ; import '@pnp/sp/fields' ; import { SPHttpClient } from '@microsoft/sp-http' ; import { IFieldInfo } from '@pnp/sp/fields' ; import '@pnp/sp/site-users/web' ; import '@pnp/sp/webs' ; declare var window ; interface IFieldLookupInfo extends IFieldInfo { LookupWebId : string ; LookupList : string ; } /**
 * 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 fieldValue param listItem param context public static getFieldText ( fieldValue , listItem , context ) { return new Promise < string > ( resolve => { const field = context . field ; if ( ! field ) { resolve ( '' ) ; return ; } const fieldName = field . internalName ; const fieldType = field . fieldType ; const strFieldValue = fieldValue ? fieldValue . toString ( ) : '' ; switch ( fieldType ) { case 'Note' : SPHelper . getFieldProperty ( field . id . toString ( ) , "RichText" , context , false ) . then ( richText => { const isRichText = richText === 'TRUE' ; if ( isRichText ) { resolve ( GeneralHelper . getTextFromHTML ( strFieldValue ) ) ; } resolve ( fieldValue ) ; } ) ; break ; case 'DateTime' : const friendlyDisplay = SPHelper . getRowItemValueByName ( listItem , `${ fieldName }.FriendlyDisplay` ) ; resolve ( friendlyDisplay ? GeneralHelper . getRelativeDateTimeString ( friendlyDisplay ) : strFieldValue ) ; break ; case 'User' : case 'UserMulti' : const titles = [ ] ; const users = < IPrincipal [ ] > fieldValue ; if ( ! users ) { resolve ( '' ) ; } for ( let i = 0 , len = users . length ; i < len ; i ++ ) { titles . push ( users [ i ] . title ) ; } resolve ( titles . join ( '\n' ) ) ; break ; case "Lookup" : case "LookupMulti" : const lookupValues = fieldValue as ISPFieldLookupValue [ ] ; if ( ! lookupValues ) { resolve ( '' ) ; } const lookupTexts = [ ] ; for ( let i = 0 , len = lookupValues . length ; i < len ; i ++ ) { lookupTexts . push ( lookupValues [ i ] . lookupValue ) ; } resolve ( lookupTexts . join ( '\n' ) ) ; break ; case 'URL' : SPHelper . getFieldProperty ( field . id . toString ( ) , 'Format' , context , true ) . then ( format => { const isImage = format === 'Image' ; if ( isImage ) { resolve ( '' ) ; } resolve ( SPHelper . getRowItemValueByName ( listItem , `${ fieldName }.desc` ) ) ; } ) ; break ; case 'Taxonomy' : case 'TaxonomyFieldType' : case 'TaxonomyFieldTypeMulti' : const terms = Array . isArray ( fieldValue ) ? < ITerm [ ] > fieldValue : < ITerm [ ] > [ fieldValue ] ; if ( ! terms ) { resolve ( '' ) ; } const termTexts = [ ] ; for ( let i = 0 , len = terms . length ; i < len ; i ++ ) { termTexts . push ( terms [ i ] . Label ) ; } resolve ( termTexts . join ( '\n' ) ) ; break ; case 'Attachments' : resolve ( '' ) ; break ; case 'Computed' : const storedName = this . getStoredFieldName ( fieldName ) ; if ( storedName === 'URL' ) { resolve ( this . getRowItemValueByName ( listItem , 'URL.desc' ) || strFieldValue ) ; } resolve ( strFieldValue ) ; break ; default : resolve ( strFieldValue ) ; } } ) ; } 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 ; } sp . setup ( { spfxContext : context } ) ; 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 . setup ( { spfxContext : context } ) ; 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 . setup ( { spfxContext : context } ) ; 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 ) { sp . setup ( { spfxContext : 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(accountName=@v)?@v='" + 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 ) { try { if ( window . sessionStorage ) { window . sessionStorage . setItem ( Constants . LoadedViewFieldsKey , JSON . stringify ( loadedViewFields ) ) ; } } catch ( error ) { } } private static _getLoadedViewFieldsFromStorage ( ) { try { if ( window . sessionStorage ) { const loadedViewFields = sessionStorage . getItem ( Constants . LoadedViewFieldsKey ) ; if ( loadedViewFields ) { return JSON . parse ( loadedViewFields ) ; } } else { return null ; } } catch ( error ) { return null ; } } }