ulteam-scripts UForm

UForm class

Helper methods for working with standard SharePoint list forms. For correct working you shoud override standard CSR template using UOverride.init method.

Sample

import { Delay } from "../../common/Delay";
import { IUChoiceField, UForm, UOverride } from "../../sp";

export class TestJSLink {
  public static async run() {
    const override: UOverride = new UOverride({
      addLoading: true,
      hideInDispForm: ['Title'],
      hideInEditForm: ['Title'],
      hideInNewForm: ['Title'],
      Templates: { Fields: {
        'Title': {
          'NewForm': UOverride.setTextField,
          'EditForm': UOverride.setTextField
        },
      }}
    });

    override.init();

    // wait 2 seconds (just for test loading circle)
    await Delay.go(2000);
    // --- your async 'onload' code (for example, REST API calls)
    
    // unblock form controls
    UForm.setFormDisabled(false);
    
    const choices: IUChoiceField[] = [
      {
        value: 'Choice #1'
      },
      {
        value: 'Choice #2'
      },
      {
        value: 'Choice #3'
      }
    ];
    // set text field as multi choice
    UForm.setTextFieldAsMultiChoice('TextField', false, ',', choices, 'Choice #1', (key: string, newValue: string, container: HTMLDivElement) => {
      console.log(key, newValue, container);
    });
    
    // disable one choice
    await Delay.go(2000);
    choices[0].disabled = true;
    UForm.setTextFieldAsMultiChoice('TextField', false, ',', choices, 'Choice #2', (key: string, newValue: string, container: HTMLDivElement) => {
      console.log(key, newValue, container);
    });

    // check in browser console what generated in UForm.formData object
    // this object generated in UOverride.init method
    console.log(UForm.formData);
  }
}

Properties

Name Static Description
formData: IUFormData Static Current item field values & field schema.
For initialization this property you shoud override standard CSR template using UOverride.init method.
formType: UFormType Static Opened form type.

Methods

Name Returns Static Description
addFieldListener(key: string, callback: undefined | (key: string, newValue: string | ISPClientPeoplePickerEntity[]) => void) void Static Add field control listener for ‘change’ event.
key: Field internal name
callback: Callback function for custom action after field control value was changed
closestElement(element: HTMLElement, tagName: string) HTMLElement | null Static Get closest parent element.
element: Child element
tagName: Closest parent tag name
getAuthorId() number Static Get form Author Id.
getCustomControlId(key: string) string | undefined Static Get custom control id by customRenderType prop in fieldSchema.
key: Field internal name
getEditorId() number Static Get form Editor Id.
getFieldControl(key: string) HTMLElement | null Static Get field control in Edit/New form.
key: Field internal name
getFieldControlId(key: string) string | undefined Static Get field control id in Edit/New form.
key: Field internal name
getFieldControlIdBySchema(schema: FieldSchema_InForm) string | undefined Static Get field control id in Edit/New form.
schema: Field schema
getFieldRowElement(key: string) HTMLElement | null Static Get form field row (tr tag in form table)
key: Field internal name
getFieldSchema(key: string) IUFieldSchema | undefined Static Get field schema
key: Field internal name
getFormType() UFormType Static Get SharePoint list form type by location.pathname.
getItemValue(key: string) string Static Get field value in SharePoint form and update this.item object
key: Field internal name
getUserFieldValueId(key: string) number | undefined Static Get user field (not multiple) value id.
key: Field internal name
hideFieldRow(key: string, hide: boolean) void Static Set field row hidden.
key: Field internal name
hide: Whether hide field row or not
hideTimeInDatePicker(key: string, hide: boolean) void Static Hide/show time in SharePoint date picker control.
key: Field internal name
hide: Whether hide time or not
setErrorMessage(key: string, message: string, show: boolean) void Static Set error message below field control (like standard SharePoint field value error)
key: Field internal name
message: Error message
show: Whether show message or not
setFieldSchemaType(key: string, type: UFieldRenderType) void Static Set customRenderType prop in this.item.fieldSchema array
key: Field internal name
type: Custom render type
setFormDisabled(isDisabled: boolean) void Static Disable/enable all form controls and buttons
isDisabled: Whether disable form or not
setItemValue(key: string, value: any) void Static Set new field value (include custom controls) in SharePoint form and update this.item object
key: Field internal name
value: New field value
setRequiredStar(key: string, addStar: boolean, starTitle: string) void Static Add or delete required star to field label
key: Field internal name
addStar: Whether add star or not
starTitle: Star element title attribute
setSaveBtn(preSaveAction: () => boolean) void Static Add custom action on the save button.
preSaveAction: If function returns true then run standard SharePoint functionality on the save button.
setTextFieldAsDisplayText(key: string, defaultValue: undefined | string) void Static Set text field as text in display form.
key: Field internal name
defaultValue: Set default value. If value is undefined then value will not changed
setTextFieldAsDropdown(key: string, disabled: boolean, loading: undefined | false | true, options: IULookupField[], defaultValue: undefined | string) void Static Set text field as dropdown.
key: Field internal name
disabled: Whether control disabled or not
loading: **
options: Dropdown options
defaultValue: Set default option for dropdown
setTextFieldAsMultiChoice(key: string, loading: undefined | false | true, separator: string, choices: IUChoiceField[], defaultValue: undefined | string, callback: undefined | (key: string, newValue: string, container: HTMLDivElement) => void) void Static Set SharePoint text field as multi choice (checkboxes)
key: Field internal name
loading: Whether show loading circle or not
separator: Value separator for SharePoint text field value
choices: Array of all choices
defaultValue: Default selected choices. For select several choices, use ‘separator’
callback: Add your action after ‘change’ event
setTextFieldAsTable(key: string, columnsSchema: IUFieldTableColumnSchema[], isEdit: undefined | false | true, columnSeparator: undefined | string, rowSeparator: undefined | string, tableConsts: IUFieldTableConsts) void Static Set text field as table.
key: Field internal name
columnsSchema: Table columns
isEdit: Whether table on edit mode or not
columnSeparator: Column separator in SP field
rowSeparator: Row separator in SP field
tableConsts: Table consts
unmountCustomControl(key: string, resetValue: boolean) void Static Delete custom control and show standard control.
key: Field internal name
resetValue: Whether clear current field value or not

Updated: