import { IFieldInfo , IFieldInfoCalculated , IFieldInfoChoice , IFieldInfoCurrency , IFieldInfoDate , IFieldInfoLookup , IFieldInfoMMS , IFieldInfoNote , IFieldInfoNumber , IFieldInfoText , IFieldInfoUrl , IFieldInfoUser } from "../../@types/helper" ; import { ContextInfo , Web } from "../lib" ; import { SPTypes } from ".." ; import { SPCfgFieldType } from "./spCfg" ; /**
 * Field Schema XML
 * Helper class for generating the field schema xml
 */ export const FieldSchemaXML = ( fieldInfo ) : PromiseLike < string > => { let _resolve = null ; let createBoolean = ( fieldInfo , props ) => { let schemaXml = null ; props [ "Type" ] = "Boolean" ; schemaXml = "<Field " + toString ( props ) + ">" ; if ( fieldInfo . defaultValue ) { schemaXml += "<Default>" + fieldInfo . defaultValue + "</Default>" ; } schemaXml += "</Field>" ; _resolve ( schemaXml ) ; } ; let createCalculated = ( fieldInfo , props ) => { let schemaXml = null ; props [ "Type" ] = "Calculated" ; switch ( fieldInfo . resultType ) { case SPTypes . FieldResultType . Boolean : props [ "ResultType" ] = "Boolean" ; break ; case SPTypes . FieldResultType . Currency : props [ "ResultType" ] = "Currency" ; if ( fieldInfo . lcid > 0 ) { props [ "LCID" ] = fieldInfo . lcid ; } break ; case SPTypes . FieldResultType . DateOnly : props [ "Format" ] = "DateOnly" ; props [ "ResultType" ] = "DateTime" ; break ; case SPTypes . FieldResultType . DateTime : props [ "Format" ] = "DateTime" ; props [ "ResultType" ] = "DateTime" ; break ; case SPTypes . FieldResultType . Number : props [ "ResultType" ] = "Number" ; if ( fieldInfo . decimals >= 0 ) { props [ "Decimals" ] = fieldInfo . decimals ; } if ( fieldInfo . numberType == SPTypes . FieldNumberType . Percentage ) { props [ "Percentage" ] = "TRUE" ; } break ; default : props [ "ResultType" ] = "Text" ; break ; } schemaXml = "<Field " + toString ( props ) + ">" ; if ( fieldInfo . formula ) { schemaXml += "<Formula>" + fieldInfo . formula + "</Formula>" ; } if ( fieldInfo . fieldRefs ) { schemaXml += "<FieldRefs>" ; for ( let i = 0 ; i < fieldInfo . fieldRefs . length ; i ++ ) { schemaXml += "<FieldRef Name=\"" + fieldInfo . fieldRefs [ i ] + "\" />" ; } schemaXml += "</FieldRefs>" ; } schemaXml += "</Field>" ; _resolve ( schemaXml ) ; } ; let createChoice = ( fieldInfo , props ) => { let schemaXml = null ; props [ "Type" ] = fieldInfo . multi ? "MultiChoice" : "Choice" ; if ( typeof ( fieldInfo . fillInChoice ) === "boolean" ) { props [ "FillInChoice" ] = fieldInfo . fillInChoice ? "TRUE" : "FALSE" ; } switch ( fieldInfo . format ) { case SPTypes . ChoiceFormatType . Dropdown : props [ "Format" ] = "Dropdown" ; break ; case SPTypes . ChoiceFormatType . RadioButtons : props [ "Format" ] = "RadioButtons" ; break ; } schemaXml = "<Field " + toString ( props ) + ">" ; if ( fieldInfo . defaultValue ) { schemaXml += "<Default>" + fieldInfo . defaultValue + "</Default>" ; } if ( fieldInfo . choices ) { schemaXml += "<CHOICES>" ; for ( let i = 0 ; i < fieldInfo . choices . length ; i ++ ) { schemaXml += "<CHOICE>" + fieldInfo . choices [ i ] + "</CHOICE>" ; } schemaXml += "</CHOICES>" ; } schemaXml += "</Field>" ; _resolve ( schemaXml ) ; } ; let createCurrency = ( fieldInfo , props ) => { let schemaXml = null ; props [ "Type" ] = "Currency" ; if ( fieldInfo . decimals >= 0 ) { props [ "Decimals" ] = fieldInfo . decimals ; } if ( fieldInfo . lcid > 0 ) { props [ "LCID" ] = fieldInfo . lcid ; } if ( fieldInfo . max != null ) { props [ "Max" ] = fieldInfo . max ; } if ( fieldInfo . min != null ) { props [ "Min" ] = fieldInfo . min ; } schemaXml = "<Field " + toString ( props ) + " />" ; _resolve ( schemaXml ) ; } ; let createDate = ( fieldInfo , props ) => { let schemaXml = null ; props [ "Type" ] = "DateTime" ; props [ "Format" ] = fieldInfo . format == SPTypes . DateFormat . DateTime ? "DateTime" : "DateOnly" ; switch ( fieldInfo . displayFormat ) { case SPTypes . FriendlyDateFormat . Disabled : props [ "FriendlyDisplayFormat" ] = "Disabled" ; break ; case SPTypes . FriendlyDateFormat . Relative : props [ "FriendlyDisplayFormat" ] = "Relative" ; break ; case SPTypes . FriendlyDateFormat . Unspecified : props [ "FriendlyDisplayFormat" ] = "Unspecified" ; break ; } schemaXml = "<Field " + toString ( props ) + ">" ; if ( fieldInfo . defaultToday ) { schemaXml += "<Default>[today]</Default>" ; } else if ( fieldInfo . defaultValue ) { schemaXml += "<Default>" + fieldInfo . defaultValue + "</Default>" ; } if ( fieldInfo . defaultFormula ) { schemaXml += "<DefaultFormula>" + fieldInfo . defaultFormula + "</DefaultFormula>" ; } schemaXml += "</Field>" ; _resolve ( schemaXml ) ; } ; let createGeolocation = ( fieldInfo , props ) => { let schemaXml = null ; props [ "Type" ] = "Geolocation" ; schemaXml = "<Field " + toString ( props ) + " />" ; _resolve ( schemaXml ) ; } ; let createGuid = ( fieldInfo , props ) => { let schemaXml = null ; props [ "Type" ] = "Guid" ; schemaXml = "<Field " + toString ( props ) + " />" ; _resolve ( schemaXml ) ; } ; let createLookup = ( fieldInfo , props ) => { props [ "Type" ] = fieldInfo . multi ? "LookupMulti" : "Lookup" ; switch ( fieldInfo . relationshipBehavior ) { case SPTypes . RelationshipDeleteBehaviorType . Cascade : props [ "RelationshipDeleteBehavior" ] = "Cascade" ; break ; case SPTypes . RelationshipDeleteBehaviorType . None : props [ "RelationshipDeleteBehavior" ] = "None" ; break ; case SPTypes . RelationshipDeleteBehaviorType . Restrict : props [ "RelationshipDeleteBehavior" ] = "Restrict" ; break ; } if ( fieldInfo . fieldRef ) { props [ "FieldRef" ] = fieldInfo . fieldRef ; } if ( fieldInfo . multi ) { props [ "Mult" ] = "TRUE" ; } props [ "ShowField" ] = fieldInfo . showField || "Title" ; if ( fieldInfo . listName ) { Web ( fieldInfo . webUrl , { disableCache : true } ) . Lists ( fieldInfo . listName ) . query ( { Expand : [ "ParentWeb" ] } ) . execute ( list => { props [ "List" ] = "{" + list . Id + "}" ; if ( fieldInfo . webUrl ) { props [ "WebId" ] = list . ParentWeb . Id ; } _resolve ( "<Field " + toString ( props ) + " />" ) ; } ) ; } else { props [ "List" ] = "{" + fieldInfo . listId . replace ( /[\{\}]/g , "" ) + "}" ; _resolve ( "<Field " + toString ( props ) + " />" ) ; } } ; let createMMS = ( fieldInfo , props ) => { let valueProps = { ID : "{" + ContextInfo . generateGUID ( ) + "}" , Name : fieldInfo . name + "_0" , StaticName : fieldInfo . name + "_0" , DisplayName : fieldInfo . title + " Value" , Type : "Note" , Hidden : "TRUE" , Required : "FALSE" , ShowInViewForms : "FALSE" , CanToggleHidden : "TRUE" } ; let schemaXmlValue = "<Field " + toString ( valueProps ) + " />" ; props [ "Type" ] = "TaxonomyFieldType" ; props [ "ShowField" ] = "Term" + ( fieldInfo . locale ? fieldInfo . locale . toString ( ) : "1033" ) ; let schemaXml = [ "<Field " + toString ( props ) + ">" , "<Customization>" , "<ArrayOfProperty>" , "<Property>" , "<Name>TextField</Name>" , "<Value xmlns:q6=\"http://www.w3.org/2001/XMLSchema\" p4:type=\"q6:string\" xmlns:p4=\"http://www.w3.org/2001/XMLSchema-instance\">" + valueProps . ID + "</Value>" , "</Property>" , "</ArrayOfProperty>" , "</Customization>" , "</Field>" ] . join ( "" ) ; _resolve ( [ schemaXmlValue , schemaXml ] ) ; } ; let createNote = ( fieldInfo , props ) => { let schemaXml = null ; props [ "Type" ] = "Note" ; if ( fieldInfo . appendFl ) { props [ "AppendOnly" ] = "TRUE" ; } if ( fieldInfo . noteType == SPTypes . FieldNoteType . EnhancedRichText || fieldInfo . noteType == SPTypes . FieldNoteType . RichText ) { props [ "RichText" ] = "TRUE" ; } if ( fieldInfo . noteType == SPTypes . FieldNoteType . EnhancedRichText ) { props [ "RichTextMode" ] = "FullHtml" ; } if ( fieldInfo . numberOfLines > 0 ) { props [ "NumLines" ] = fieldInfo . numberOfLines ; } schemaXml = "<Field " + toString ( props ) + " />" ; _resolve ( schemaXml ) ; } ; let createNumber = ( fieldInfo , props ) => { let schemaXml = null ; props [ "Type" ] = "Number" ; if ( fieldInfo . decimals >= 0 ) { props [ "Decimals" ] = fieldInfo . decimals ; } if ( fieldInfo . max != null ) { props [ "Max" ] = fieldInfo . max ; } if ( fieldInfo . min != null ) { props [ "Min" ] = fieldInfo . min ; } if ( fieldInfo . numberType == SPTypes . FieldNumberType . Integer ) { props [ "Decimals" ] = 0 ; } if ( fieldInfo . numberType == SPTypes . FieldNumberType . Percentage ) { props [ "Percentage" ] = "TRUE" ; } schemaXml = "<Field " + toString ( props ) + ">" ; if ( fieldInfo . defaultValue ) { schemaXml += "<Default>" + fieldInfo . defaultValue + "</Default>" ; } schemaXml += "</Field>" ; _resolve ( schemaXml ) ; } ; let createText = ( fieldInfo , props ) => { let schemaXml = null ; props [ "Type" ] = "Text" ; if ( fieldInfo . maxLength != null ) { props [ "MaxLength" ] = fieldInfo . maxLength ; } schemaXml = "<Field " + toString ( props ) + ">" ; if ( fieldInfo . defaultValue ) { schemaXml += "<Default>" + fieldInfo . defaultValue + "</Default>" ; } schemaXml += "</Field>" ; _resolve ( schemaXml ) ; } ; let createUrl = ( fieldInfo , props ) => { let schemaXml = null ; props [ "Type" ] = "URL" ; props [ "Format" ] = fieldInfo . format == SPTypes . UrlFormatType . Image ? "Image" : "Hyperlink" ; schemaXml = "<Field " + toString ( props ) + " />" ; _resolve ( schemaXml ) ; } ; let createUser = ( fieldInfo , props ) => { let schemaXml = null ; props [ "Type" ] = "User" ; if ( fieldInfo . multi ) { props [ "Mult" ] = "TRUE" ; } if ( fieldInfo . selectionMode != null ) { props [ "UserSelectionMode" ] = fieldInfo . selectionMode ; } if ( fieldInfo . selectionScope != null ) { props [ "UserSelectionScope" ] = fieldInfo . selectionScope ; } if ( fieldInfo . showField != null ) { props [ "ShowField" ] = fieldInfo . showField ; } schemaXml = "<Field " + toString ( props ) + " />" ; _resolve ( schemaXml ) ; } ; let toString = ( props ) => { let properties = "" ; for ( let key in props ) { let value = props [ key ] ; properties += ( properties ? " " : "" ) + key + "=\"" + props [ key ] + "\"" ; } return properties ; } ; return new Promise ( ( resolve , reject ) => { _resolve = resolve ; if ( fieldInfo . schemaXml ) { resolve ( fieldInfo . schemaXml ) ; } else { let props = { } ; props [ "ID" ] = "{" + ContextInfo . generateGUID ( ) + "}" ; props [ "Name" ] = fieldInfo . name ; props [ "StaticName" ] = fieldInfo . name ; props [ "DisplayName" ] = fieldInfo . title || fieldInfo . name ; if ( typeof ( fieldInfo . allowDeletion ) !== "undefined" ) { props [ "AllowDeletion" ] = fieldInfo . allowDeletion ? "TRUE" : "FALSE" ; } if ( typeof ( fieldInfo . description ) !== "undefined" ) { props [ "Description" ] = fieldInfo . description ; } if ( typeof ( fieldInfo . enforceUniqueValues ) !== "undefined" ) { props [ "EnforceUniqueValues" ] = fieldInfo . enforceUniqueValues ? "TRUE" : "FALSE" ; } if ( typeof ( fieldInfo . group ) !== "undefined" ) { props [ "Group" ] = fieldInfo . group ; } if ( typeof ( fieldInfo . jslink ) !== "undefined" ) { props [ "JSLink" ] = fieldInfo . jslink ; } if ( typeof ( fieldInfo . hidden ) !== "undefined" ) { props [ "Hidden" ] = fieldInfo . hidden ? "TRUE" : "FALSE" ; } if ( typeof ( fieldInfo . indexed ) !== "undefined" ) { props [ "Indexed" ] = fieldInfo . indexed ? "TRUE" : "FALSE" ; } if ( typeof ( fieldInfo . readOnly ) !== "undefined" ) { props [ "ReadOnly" ] = fieldInfo . readOnly ? "TRUE" : "FALSE" ; } if ( typeof ( fieldInfo . required ) !== "undefined" ) { props [ "Required" ] = fieldInfo . required ? "TRUE" : "FALSE" ; } if ( typeof ( fieldInfo . showInDisplayForm ) !== "undefined" ) { props [ "ShowInDisplayForm" ] = fieldInfo . showInDisplayForm ? "TRUE" : "FALSE" ; } if ( typeof ( fieldInfo . showInEditForm ) !== "undefined" ) { props [ "ShowInEditForm" ] = fieldInfo . showInEditForm ? "TRUE" : "FALSE" ; } if ( typeof ( fieldInfo . showInListSettings ) !== "undefined" ) { props [ "ShowInListSettings" ] = fieldInfo . showInListSettings ? "TRUE" : "FALSE" ; } if ( typeof ( fieldInfo . showInNewForm ) !== "undefined" ) { props [ "ShowInNewForm" ] = fieldInfo . showInNewForm ? "TRUE" : "FALSE" ; } if ( typeof ( fieldInfo . showInViewForms ) !== "undefined" ) { props [ "ShowInViewForms" ] = fieldInfo . showInViewForms ? "TRUE" : "FALSE" ; } if ( typeof ( fieldInfo . sortable ) !== "undefined" ) { props [ "Sortable" ] = fieldInfo . sortable ? "TRUE" : "FALSE" ; } switch ( fieldInfo . type ) { case SPCfgFieldType . Boolean : createBoolean ( fieldInfo , props ) ; break ; case SPCfgFieldType . Calculated : createCalculated ( fieldInfo , props ) ; break ; case SPCfgFieldType . Choice : createChoice ( fieldInfo , props ) ; break ; case SPCfgFieldType . Currency : createCurrency ( fieldInfo , props ) ; break ; case SPCfgFieldType . Date : createDate ( fieldInfo , props ) ; break ; case SPCfgFieldType . Geolocation : createGeolocation ( fieldInfo , props ) ; break ; case SPCfgFieldType . Guid : createGuid ( fieldInfo , props ) ; break ; case SPCfgFieldType . Lookup : createLookup ( fieldInfo , props ) ; break ; case SPCfgFieldType . MMS : createMMS ( fieldInfo , props ) ; break ; case SPCfgFieldType . Note : createNote ( fieldInfo , props ) ; break ; case SPCfgFieldType . Number : createNumber ( fieldInfo , props ) ; break ; case SPCfgFieldType . Text : createText ( fieldInfo , props ) ; break ; case SPCfgFieldType . Url : createUrl ( fieldInfo , props ) ; break ; case SPCfgFieldType . User : createUser ( fieldInfo , props ) ; break ; default : createText ( fieldInfo , props ) ; break ; } } } ) ; } ;